【问题标题】:iOS: How do I detect if music is playing in any background music app?iOS:如何检测音乐是否在任何背景音乐应用程序中播放?
【发布时间】:2012-09-18 11:12:33
【问题描述】:

当内置 iPod 应用程序中播放音乐时,我的游戏目前可以正确处理禁用其自己的 BGM,但它没有检测到 Pandora 等应用程序何时播放音乐。

目前,在我的applicationDidBecomeActive 方法中,我检查[[MPMusicPlayerController iPodMusicPlayer] playbackState] 以确定是否正在播放音乐。这相当于检查像 Pandora 这样的应用是否在后台播放音频?

【问题讨论】:

    标签: ios audio background


    【解决方案1】:

    AudioSessionGetProperty(如 jake_hetfield 的回答中所述)自 iOS 7 起已弃用。

    试试这个使用isOtherAudioPlaying的单行:

    BOOL isOtherAudioPlaying = [[AVAudioSession sharedInstance] isOtherAudioPlaying];
    

    适用于 iOS 6+。

    【讨论】:

    • Swift 2.3 版本:var otherAudioPlaying = AVAudioSession.sharedInstance().otherAudioPlaying
    • 斯威夫特 3:var otherAudioPlaying = AVAudioSession.sharedInstance().isOtherAudioPlaying
    【解决方案2】:

    查看this question

    您似乎可以通过像这样检查属性 kAudioSessionProperty_OtherAudioIsPlaying 来查看是否正在播放另一个音频:

    UInt32 propertySize, audioIsAlreadyPlaying=0;
    propertySize = sizeof(UInt32);
    AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &audioIsAlreadyPlaying);
    

    对此的补充可以是询问用户他/她是否想要游戏音乐或已经播放的声音/音乐。

    【讨论】:

    • 谢谢!我进行了大量搜索,但不知何故从未找到这个问题。我不打算问用户,我认为只要推断出用户希望继续听自己的音乐(如果他们已经在播放)就足够了。
    • 目前我收到了来自kAudioSessionProperty_OtherAudioIsPlaying 的误报。其他音频肯定没有播放(我在 iPhone 5 iOS 6.x 上停止了 iPod)。这有时会发生。就像有什么东西卡在那里,使kAudioSessionProperty_OtherAudioIsPlaying 总是返回YES/1/true/positive。它通常仅在重新启动设备后自行修复。
    • 仅供参考,这可能不是误报。一些应用程序播放“无声音频”(即没有声音的短音频文件)以绕过 15 分钟空闲计时器。它可能是一个应用程序,它会触发支票的真实回报。只是一个想法。
    【解决方案3】:

    从 iOS 8 开始,应该使用 secondaryAudioShouldBeSilencedHint 属性:

    /* Will be true when another application with a non-mixable audio session is playing audio.  Applications may use
    this property as a hint to silence audio that is secondary to the functionality of the application. For example, a game app
    using AVAudioSessionCategoryAmbient may use this property to decide to mute its soundtrack while leaving its sound effects unmuted.
    Note: This property is closely related to AVAudioSessionSilenceSecondaryAudioHintNotification.
    */
    @property(readonly) BOOL secondaryAudioShouldBeSilencedHint  NS_AVAILABLE_IOS(8_0);
    

    【讨论】:

      【解决方案4】:

      你可能想做这样的事情.....

      1. 创建一个类来处理您的音频设置,例如...“AudioManager”

      2. 轮询布尔“isOtherAudioPlaying”...也许将其分配给您自己的布尔值。

      import Foundation
      import AVFoundation
      
      class AudioManager {
          static let successBingSoundID: SystemSoundID = <Your System Sound ID in Int>
          static func playSystemSoundIfBackgroundSoundIsOff() {
              guard !AVAudioSession.sharedInstance().isOtherAudioPlaying else {return}
              AudioServicesPlaySystemSoundWithCompletion(successBingSoundID, nil)
          }
      }
      

      用法:

      AudioManager.playSystemSoundIfBackgroundSoundIsOff()
      

      【讨论】:

      • 这个答案没有添加任何无法从其他答案中学到的东西,除了它添加了添加音频管理器类的额外步骤。如果没有构建另一个音频管理器,您提供的示例甚至不能多次轮询该值。
      • @TimR。我同意你的看法。但是为了分离逻辑而必须创建一个新类有时会很有用。此外,@hoboBob 实际上提供了.isOtherAudioPlaying, 的快速版本,它提供了一些很好的信息。我更改了原始代码,以便人们可以轻松使用它们。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-03
      • 2015-06-29
      • 1970-01-01
      • 2011-11-23
      • 1970-01-01
      • 2011-01-17
      • 2022-01-23
      相关资源
      最近更新 更多