首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >audioSession错误:操作无法完成。(OSStatus错误-50.)

audioSession错误:操作无法完成。(OSStatus错误-50.)
EN

Stack Overflow用户
提问于 2017-08-17 16:51:21
回答 1查看 2.6K关注 0票数 2

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

代码语言:javascript
复制
 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.)

错误而不起作用。请帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-18 06:45:14

您的代码有一些结构问题。我帮你清理了一下:

代码语言:javascript
复制
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,所以那里的答案也可能对你有帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45740926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档