gpt4 book ai didi

ios - audioSession 错误 : The operation couldn’t be completed.(OSStatus 错误 -50。)

转载 作者:行者123 更新时间:2023-11-29 11:42:20 34 4
gpt4 key购买 nike

我正在尝试从 bundle (来自项目)播放音频文件,当用户设置设备的静音模式时,它应该是静音的。我的代码在这里

 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 error: The operation couldn’t be completed. (OSStatus error -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 文件包含在您构建的应用程序中。我只是在看at a related question几分钟前讨论过这个问题,所以那里的答案也可能对您有帮助。

关于ios - audioSession 错误 : The operation couldn’t be completed.(OSStatus 错误 -50。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45740926/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com