1

I have 10 different buttons and each button has a unique tag associated with it. All buttons are linked to the same IBAction. I have the function to play different different sounds according to the tag associated with the particular button that is clicked.

import AVFoundation

var myAudio: AVAudioPlayer!

let path = Bundle.main.path(forResource: "sound1", ofType: "wav")!
let url = URL(fileURLWithPath: path)
do {
    let sound = try AVAudioPlayer(contentsOf: url)
    myAudio = sound
    sound.play()
    } catch {
        //
    }
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Utkarsh Sharma
  • 61
  • 2
  • 12

3 Answers3

1

do like

@objc func yourbtnActionName(_ sender : UIButton){

    switch sender.tag {
    case 1:
        commonSoundCode(name: "sound1")
        break
    case 2:
        commonSoundCode(name: "yoursecondSoundname")
        break
    default:
        break
    }
   }

then common method as

 func commonSoundCode(name: String){
    let path = Bundle.main.path(forResource: name, ofType: "wav")!
    let url = URL(fileURLWithPath: path)
    do {
        let sound = try AVAudioPlayer(contentsOf: url)
        myAudio = sound
        sound.play()
    } catch {
        //
    }
}

option 2

if your sound files are in the same sequence, for e.g (sound1.wav, sound2.wav......, sound10.wav) then call like

@objc func yourbtnActionName(_ sender : UIButton){
  let path = Bundle.main.path(forResource: "sound\(sender.tag)", ofType: "wav")!
    let url = URL(fileURLWithPath: path)
    do {
        let sound = try AVAudioPlayer(contentsOf: url)
        myAudio = sound
        sound.play()
    } catch {
        //
    }

}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1
import UIKit

//Mark: Do import AVFoundation is must here!

import AVFoundation

class ViewController: UIViewController {
    
//Mark: Create variable for AVAudioEngine! and AVAudioplayermode!
    
    var engine: AVAudioEngine!
    var audioPlayerNode : AVAudioPlayerNode!

//Mark: Create Variable for each tone as AVAudiofile!

    var Cs:AVAudioFile!
    var Ds:AVAudioFile!
    var Es:AVAudioFile!
    var Fs:AVAudioFile!
    var Gs:AVAudioFile!
    var As:AVAudioFile!
    var Bs:AVAudioFile!
    
    override func viewDidLoad() {
        super.viewDidLoad()

// Mark: Load engine
        
        engine = AVAudioEngine()
        
// Mark : Mention each sound
        
        Cs = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
                   Bundle.main.path(forResource: "C", ofType: "wav")!))
        Ds = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
                   Bundle.main.path(forResource: "D", ofType: "wav")!))
        Es = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
                   Bundle.main.path(forResource: "E", ofType: "wav")!))
        Fs = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
                   Bundle.main.path(forResource: "F", ofType: "wav")!))
        Gs = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
                   Bundle.main.path(forResource: "G", ofType: "wav")!))
        As = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
                   Bundle.main.path(forResource: "A", ofType: "wav")!))
        Bs = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
                   Bundle.main.path(forResource: "B", ofType: "wav")!))
    }
    
//Mark: create function(playSound) for connect AVAudioEngine and AVAudioplayermode for playing audio when button clicked
    
    func playSound(audioFile: AVAudioFile)  {
        audioPlayerNode = AVAudioPlayerNode()
        if(audioPlayerNode.isPlaying){
            audioPlayerNode.stop()
        }

        if(engine.isRunning){
            engine.stop()
            engine.reset()
        }

        engine.attach(audioPlayerNode)

    engine.connect(audioPlayerNode, to: engine.mainMixerNode, format: audioFile.processingFormat)
    audioPlayerNode.scheduleFile(audioFile, at: nil, completionHandler: nil)

    // Start the audio engine
    engine.prepare()
    try! engine.start()

    audioPlayerNode.play()
}

//Mark: create required button for actions to connect function(playSound) and mention each sound  to button

    @IBAction func btnC(_ sender: UIButton) {
        playSound(audioFile: Cs)
    }
    @IBAction func btnD(_ sender: UIButton) {
        playSound(audioFile: Ds)
    }
 
    @IBAction func btnE(_ sender: UIButton) {
        playSound(audioFile: Es)
    }
    
    @IBAction func btnF(_ sender: UIButton) {
        playSound(audioFile: Fs)
    }
    @IBAction func btnG(_ sender: UIButton) {
        playSound(audioFile: Gs)
    }
    
    @IBAction func btnA(_ sender: UIButton) {
        playSound(audioFile: As)
    }
    
    @IBAction func btnB(_ sender: UIButton) {
        playSound(audioFile: Bs)
    }
    
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
0

You need to simply play sounds according to the UIButton's tag.

@IBAction func playsound(_ sender: UIButton)
{
    switch sender.tag
    {
    case 0:
        //Sound for Button with tag=0
    case 1:
        //Sound for Button with tag=1
    default:
        //Default Sound
    }
}

In the above code, add more cases according to your UIButton tags.

Please add some more code for a bit more clarity around your question.

PGDev
  • 23,751
  • 6
  • 34
  • 88