iOS:如何检测音乐是否在任何背景音乐应用程序中播放?
目前我的游戏正确处理了在内置的iPod应用程序中播放音乐时禁用自己的BGM,但是当潘多拉(Pandora)等应用程序正在播放音乐时,它无法检测到。
目前,在我的applicationDidBecomeActive
方法中,我检查[[MPMusicPlayerController iPodMusicPlayer] playbackState]
来判断音乐是否在播放。 这是什么等同于检查像潘多拉这样的应用程序是否在后台播放audio?
AudioSessionGetProperty
(如jake_hetfield的答案中所提到的)从iOS 7开始已弃用。
相反,试试这个使用isOtherAudioPlaying的单线程 :
BOOL isOtherAudioPlaying = [[AVAudioSession sharedInstance] isOtherAudioPlaying];
适用于iOS 6+。
看看这个问题
似乎你可以通过检查属性kAudioSessionProperty_OtherAudioIsPlaying像这样看看是否有另一个audio播放:
UInt32 propertySize, audioIsAlreadyPlaying=0; propertySize = sizeof(UInt32); AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &audioIsAlreadyPlaying);
对此的补充可以是询问用户他/她是否想要游戏音乐或已经播放的声音/音乐。
从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);