如何让函数在 Swift 中播放不同的声音

How to make a function play different sounds in Swift

我有 10 个不同的按钮,每个按钮都有一个与之关联的唯一标签。所有按钮都链接到同一个 IBAction。我可以根据与单击的特定按钮关联的标签播放不同的不同声音。

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 {
        //
    }
}

您只需根据 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
    }
}

在上面的代码中,根据你的UIButton tags.

添加更多的案例

请添加更多代码以使您的问题更加清晰。

喜欢

@objc func yourbtnActionName(_ sender : UIButton){

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

然后常用方法为

 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 {
        //
    }
}

选项 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 {
        //
    }

}
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)
    }
    
}