AvaudioSession静音使用AvaudioSessionCategoryPlay和Record以及选项AvaudioSessionCategoryOptionMixewithOthers进行二次音频提示
我正在尝试使用IOS 8.0中的新通知调用AVAudioSessionSilenceSecondaryAudioHintNotification,以便在其他背景音频(如系统音乐播放器)播放停止/开始时(声音效果将继续并与应用程序或背景音乐混合),我可以相应地播放/暂停我的iPad应用程序音乐。当我不播放任何音乐时,我会正确接收通知。然而,当我使用AvaudioSession静音使用AvaudioSessionCategoryPlay和Record以及选项AvaudioSessionCategoryOptionMixewithOthers进行二次音频提示,ios,audio,Ios,Audio,我正在尝试使用IOS 8.0中的新通知调用AVAudioSessionSilenceSecondaryAudioHintNotification,以便在其他背景音频(如系统音乐播放器)播放停止/开始时(声音效果将继续并与应用程序或背景音乐混合),我可以相应地播放/暂停我的iPad应用程序音乐。当我不播放任何音乐时,我会正确接收通知。然而,当我使用AVAudioPlayer实际播放我的应用程序音乐时,通知从未被调用(我正在通过使用耳塞上的遥控器启动和停止系统音乐进行测试) 如果我切换到使用音频会话
AVAudioPlayer
实际播放我的应用程序音乐时,通知从未被调用(我正在通过使用耳塞上的遥控器启动和停止系统音乐进行测试)
如果我切换到使用音频会话类别AVAudioSessionCategoryAmbient
,它会正常运行并发送通知,但我需要在应用过程中录制时使用类别avaudiosessioncategoryplayindrecord
我意识到默认情况下,AVAudioSessionCategoryPlayAndRecord
是不可混合的,人们不会期望它的行为像AvaudioSessionCategoryBient
,但我正在设置categoryAVAudioSessionCategoryPlayAndRecord
,选项为:AvaudioSessionCategoryOptionWithOthers,因此,我的播放/录制音频会话应该是可混合的,就像AVAudioSessionCategoryAmbient
。我希望在这种情况下,avaudiosessionsilensesecondary audiohintnotification
能起作用
我想知道是否有人成功地在应用程序AVAudioPlayer
播放过程中获得了avaudiosessionsilensesecondary audiohintnotification
AvaudioSessionCategoryPlay和Record
+带选项:AVAudioSessionCategoryOptionMixWithOthers
谢谢大家!!我希望这个问题足够具体,我对StackOverflow是个新手
编辑:添加一些代码示例(尽管我知道此代码确实有效,并且结果仅随音频会话类别/选项而异)
这在应用程序代理的didFinishLaunchingWithOptions的音频初始化中调用 设置后台通知(如系统播放器)音频状态更改:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleOtherAudioStatusChanged:)
name:AVAudioSessionSilenceSecondaryAudioHintNotification
object:[AVAudioSession sharedInstance]];
这是通知回调
-(void)handleOtherAudioStatusChanged:(id)notification {
#if DEBUG_MODE
NSLog(@"handleOtherAudioStatusChanged notification");
#endif
NSNotification *n = (NSNotification *)notification;
NSDictionary *interuptionDict = n.userInfo;
if(interuptionDict && (n.name == AVAudioSessionSilenceSecondaryAudioHintNotification)) {
// Get the AVAudioSessionInterruptionTypeKey enum from the dictionary
NSInteger hintType = [[interuptionDict valueForKey:AVAudioSessionSilenceSecondaryAudioHintTypeKey] integerValue];
// Decide what to do based on interruption type...
switch(hintType) {
case(AVAudioSessionSilenceSecondaryAudioHintTypeBegin): {
// OTHER AUDIO STARTED
// The system is indicating that another application's primary audio has started
#if DEBUG_MODE
NSLog(@"In handleOtherAudioStatusChanged, other background audio started, stop game music if enabled/playing");
#endif
// TEST AND STOP GAME MUSIC IF NEEDED
break;
} // end case
case(AVAudioSessionSilenceSecondaryAudioHintTypeEnd): {
// OTHER AUDIO STOPPED
// The system is indicating that another application's primary audio has stopped
#if DEBUG_MODE
NSLog(@"In handleOtherAudioStatusChanged, other background audio stopped, play game music if enabled/during game");
#endif
// TEST AND START GAME MUSIC IF NEEDED
break;
}// end case
} // end switch
}
}
无论我的应用程序是否播放音乐,此音频会话类别始终有效:
[audioSession setCategory:AVAudioSessionCategoryAmbient
error:&setCategoryError];
这仅在我的应用程序未播放音乐时有效。和
有了选项覆盖,这应该像AVAudioSessionCategoryAmbient一样可以混合,所以我很惊讶:
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:&setCategoryError];
我也尝试了这一次,只是为了测试,而且它只在我的应用程序不播放音乐时才起作用。但它也可以和这个覆盖选项混合
[audioSession setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:&setCategoryError];
我正在以下位置执行音频会话setCategory/setMode/setActive:
didfishlaunchingwithoptions
(音频初始化)d变为活动状态
AVAudioPlayer
appMusicPlayer播放
self.appMusicPlayer.numberOfLoops = AFCAudioLoopForever;
[self.appMusicPlayer prepareToPlay];
[self.appMusicPlayer play];
我有其他的AVAudioPlayer
s用于其他非常简短的声音效果,一些测试表明,在我前面提到的类别/选项中播放它们也会干扰avaudiosessionsilenseSecondaryAudioHintNotification
希望能有所帮助。@Bujutsu我不明白为什么还需要更多。我完全理解这个问题。Matt对我来说现在也没问题。有关IOS 8.0中此新通知的更多信息:他们说“例如,使用AVAudioSessionCategoryAmbient的游戏应用程序可能会使用此属性决定将其音轨静音,同时保持其音效不静音。”但是,它并没有说一个人必须使用音频会话类别。我认为这更多的是关于混合类别。