audioSession错误:操作无法’;不可能完成。(OSStatus错误-50。)
我正在尝试播放捆绑包(项目)中的音频文件,当用户设置设备的静默模式时,该文件应该是静默的。我的密码在这里audioSession错误:操作无法’;不可能完成。(OSStatus错误-50。),ios,avplayer,avaudioplayer,Ios,Avplayer,Avaudioplayer,我正在尝试播放捆绑包(项目)中的音频文件,当用户设置设备的静默模式时,该文件应该是静默的。我的密码在这里 let audioSession = AVAudioSession.sharedInstance() ringURl = Bundle.main.url(forResource: "ring", withExtension: "m4r") do { try audioSession.setCategory(AVAudioSessionCategorySoloAmbient) try aud
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。)
错误和不工作。请帮助。您的代码存在一些结构问题。我已经帮你清理了一下:
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文件。几分钟前我刚刚在看这个问题,所以那里的答案可能也会对你有所帮助。看看这个答案,你是只在设备处于静默状态时才会出现错误,还是每次都会出现错误?@MichaelDautermann,每当我尝试播放音频文件时,我都会感觉到。但是如果设置了AudioSessionCategoryPlay并记录了它的工作情况,那么就可以了。但是,当用户设置设备的静音模式时,我需要设置静音。这就是为什么我要尝试AVAudioSessionCategorySoloAmbienti的原因。我已经检查了您的答案,文件包括是否正确。但同样的问题仍在出现。