我正在尝试播放来自包(来自项目)的音频文件,当用户设置设备的静音模式时,它应该是安静的。我的密码在这里
let audioSession = AVAudioSession.sharedInstance()
ringURl = Bundle.main.url(forResource: "ring", withExtension: "m4r")
do {
try audioSession.setCategory(AVAudioSessionCategorySoloAmbient)
try audioSession.overrideOutputAudioPort(speakerType)
}
do {
player = try AVAudioPlayer(contentsOf: ringURl)
guard let player = player else { return }
player?.prepareToPlay()
player?.play()
} catch let error as NSError {
print(error.description)
}
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}但它的表演
audioSession错误:操作无法完成。(OSStatus错误-50.)
错误而不起作用。请帮帮忙。
发布于 2017-08-18 06:45:14
您的代码有一些结构问题。我帮你清理了一下:
class ViewController: UIViewController {
var audioSession = AVAudioSession.sharedInstance() // we only need to instantiate this once
var player : AVAudioPlayer? // making this a property means player doesn't get released as soon as playSomething exits
@IBAction func playSomething(sender: UIButton!)
{
if let ringURl = Bundle.main.url(forResource: "ring", withExtension: "m4r")
{
do {
try audioSession.setCategory(AVAudioSessionCategorySoloAmbient)
try audioSession.overrideOutputAudioPort(.speaker)
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
do {
let ourPlayer = try AVAudioPlayer(contentsOf: ringURl)
ourPlayer.prepareToPlay()
ourPlayer.play()
self.player = ourPlayer
} catch let error as NSError {
print(error.description)
}
}
}如果您仍然看到"-50“错误,请确保您的.m4r文件包含在您构建的应用程序中。几分钟前,我正在寻找at a related question,所以那里的答案也可能对你有帮助。
https://stackoverflow.com/questions/45740926
复制相似问题