【问题标题】:audioSession error: The operation couldn’t be completed. (OSStatus error -50.)audioSession 错误:操作无法完成。 (OSStatus 错误 -50。)
【发布时间】:2018-01-26 05:05:30
【问题描述】:

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

 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。)

错误且无法正常工作。请帮忙。

【问题讨论】:

  • 只有在设备静音时才会出现该错误,还是每次都会出现该错误?
  • @MichaelDautermann,我每次尝试播放音频文件时都会遇到。
  • 但是如果设置了 AVAudioSessionCategoryPlayAndRecord 那么它的工作正常。
  • 但是当用户设置设备的静音模式时我需要设置静音,这就是我尝试 AVAudioSessionCategorySoloAmbient 的原因

标签: ios avplayer avaudioplayer


【解决方案1】:

您的代码存在一些结构性问题。我已经为你整理了一下:

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,因此那里的答案也可能对您有所帮助。

【讨论】:

  • 我已检查您的答案,文件包含正确。但仍然存在同样的问题。
猜你喜欢
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 2019-02-18
  • 1970-01-01
相关资源
最近更新 更多