
import AVFoundation var audioPlayer = AVAudioPlayer() func playAudio() { // Set the sound file name & extension var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Flip 02",ofType: "wav")!) // Preperation AVAudioSession.sharedInstance().setcategory(AVAudioSessioncategoryPlayback,error: nil) AVAudioSession.sharedInstance().setActive(true,error: nil) // Play the sound var error: NSError? audioPlayer = AVAudioPlayer(contentsOfURL: alertSound,error: &error) audioPlayer.preparetoPlay() audioPlayer.play()} 代码错误在这里:
代码1:
AVAudioSession.sharedInstance().setcategory(AVAudioSessioncategoryPlayback,error: nil)
错误1:
Extra argument ‘error’ in call
代码2:
AVAudioSession.sharedInstance().setActive(true,error: nil)
错误2:
Extra argument ‘error’ in call
代码3:
audioPlayer = AVAudioPlayer(contentsOfURL: alertSound,error: &error)
错误3:
Cannot find an initializer for type ‘AVAudioPlayer’ that accepts an argument List of type ‘(contentsOfURL: NSURL,error: inout NSError?)’
你的贡献可能会帮助我.谢谢.
解决方法 使用内部的功能捕捉和使用尝试的投掷:var audioPlayer = AVAudioPlayer()func playAudio() { do { if let bundle = NSBundle.mainBundle().pathForResource("Flip 02",ofType: "wav") { let alertSound = NSURL(fileURLWithPath: bundle) try AVAudioSession.sharedInstance().setcategory(AVAudioSessioncategoryPlayback) try AVAudioSession.sharedInstance().setActive(true) try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound) audioPlayer.preparetoPlay() audioPlayer.play() } } catch { print(error) }} 有关完整的Swift 2错误处理说明,请参见this answer.
总结以上是内存溢出为你收集整理的如何在iOS应用程序中使用Swift 2音频?全部内容,希望文章能够帮你解决如何在iOS应用程序中使用Swift 2音频?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)