从Swift 3中的两个扬声器获取声音

时间:2017-07-18 08:53:22

标签: swift audio

我在发言时遇到了一些问题。我用这段代码来实现声音:

 if let soundURL = Bundle.main.url(forResource: "note1", withExtension: "wav") {
        var mySound: SystemSoundID = 0
        AudioServicesCreateSystemSoundID(soundURL as CFURL, &mySound)
        // Play
        AudioServicesPlaySystemSound(mySound);
    }

它应该只能从电脑上的一个扬声器播放吗?

请温柔地对待我,我是一个全新的编程:) 谢谢!

祝你好运

1 个答案:

答案 0 :(得分:0)

对于那些想要它的人来说,Heres是代码:

导入UIKit 导入AudioToolbox

类ViewController:UIViewController {

let soundArray = ["note1", "note2", "note3", "note4", "note5", "note6", "note7"]
override func viewDidLoad() {
    super.viewDidLoad()
}



@IBAction func notePressed(_ sender: UIButton) {

    playingSound(soundFileName: soundArray[sender.tag - 1])


    }

func playingSound(soundFileName : String) {

     if let soundURL = Bundle.main.url(forResource: soundFileName, withExtension: "wav") {
        var mySound: SystemSoundID = 0
        AudioServicesCreateSystemSoundID(soundURL as CFURL, &mySound)
        // Play
        AudioServicesPlaySystemSound(mySound);
    }

}

}