【问题标题】:The Mute Button Won't Mute AVAudioPlayer静音按钮不会静音 AVAudioPlayer
【发布时间】:2012-02-19 03:24:37
【问题描述】:

我需要在我的 iPhone 应用中循环播放一个 .caf 文件。

AVAudioPlayer 看起来很有前途。但是有一个问题。即使我按下 iPhone 上的静音按钮,它也不会停止或静音。

而且我知道在 iOS 5 中无法以编程方式确定静音按钮是否被按下。

所以,我需要知道的可能是关于 AVAudioPlayer 或 AVAudioSession 的一些属性。或者,我可能需要考虑使用其他库来循环播放我的 .caf 文件。

有什么想法吗?

【问题讨论】:

    标签: ios iphone avaudioplayer avaudiosession


    【解决方案1】:

    我意识到这是因为我有这行代码

    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
    

    然后我改成

    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
    

    然后静音按钮将起作用并停止我的 AVAudioPlayer...

    【讨论】:

      【解决方案2】:

      来自docs

      所以基本上你只需要弄清楚哪个音频会话类别适合你的需要。

      【讨论】:

        【解决方案3】:

        swift 3.0 我的 2 美分:

        let sharedSession = AVAudioSession.sharedInstance()
        
        do {
            try sharedSession.setCategory(AVAudioSessionCategoryAmbient)
        } catch _{
            return false
        }
        

        或更简单:

        try? sharedSession.setCategory(AVAudioSessionCategoryAmbient)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-31
          • 1970-01-01
          • 1970-01-01
          • 2011-12-09
          • 1970-01-01
          相关资源
          最近更新 更多