我目前让我的游戏正确处理在内置iPod应用程序中播放音乐时禁用其自身的BGM,但它无法检测到Pandora等应用何时播放音乐。
目前,在我的applicationDidBecomeActive
方法中,我会检查[[MPMusicPlayerController iPodMusicPlayer] playbackState]
以确定音乐是否正在播放。有什么相当于检查像Pandora这样的应用程序是否在后台播放音频?
答案 0 :(得分:19)
AudioSessionGetProperty
(如jake_hetfield的回答中所述)自iOS 7起已弃用。
相反,请尝试使用isOtherAudioPlaying:
的单行BOOL isOtherAudioPlaying = [[AVAudioSession sharedInstance] isOtherAudioPlaying];
适用于iOS 6 +。
答案 1 :(得分:14)
似乎你可以通过检查属性kAudioSessionProperty_OtherAudioIsPlaying来查看是否正在播放另一个音频:
UInt32 propertySize, audioIsAlreadyPlaying=0;
propertySize = sizeof(UInt32);
AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &audioIsAlreadyPlaying);
对此的补充可能是询问用户他/她是否想要拥有游戏音乐或已播放的声音/音乐。
答案 2 :(得分:4)
从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);
答案 3 :(得分:-1)
你可能想做这样的事情......
1)创建一个类来处理你的音频设置说..." AudioManager"
2)轮询布尔" isOtherAudioPlaying" ...可能将其分配给您自己的布尔值。
import Foundation
import AVFoundation
class AudioManager {
var isExternalAudioPlaying = AVAudioSession.sharedInstance().isOtherAudioPlaying
}