Solo Ambient:独立播放,其他音频暂停,不可以在后台播放(即使利用其它技术使后台仍在运行,音频依然暂停),开启静音键时,能使其静音
Ambient:和其他程序的音频可以同时混播,不可以在后台播放(即使利用其它技术使后台仍在运行,音频依然暂停),开启静音键时,能使其静音。
Playback:可能与其他音频混合播放(默认是不能,但可以设置),能在后台播放,开启静音键时,不能使其静音。
Record:录音,开启静音键时,不能使其静音,能在后台录音
Play and Record:表示音频输入和输出,按顺序或同时,可能与其他音频混合播放(默认是不能,但可以设置),能在后台工作,开启静音键时,不能使其静音
Audio Processing:应用程序执行硬件辅助音频编码(它不播放或记录),开启静音键时,能否静音未知,不能混合其他音频,能在后台工作
例如,设置音频能后台播放的session代码:
AVAudioSession *session = [AVAudioSession sharedInstance];
//这里是设置当程序退出或者锁屏时音频怎么处理,此设置是独占后台播放模式,如果程序进入后台,除非有电话进来,否则音频一直播放至完成,即使有其他音频播放
//AVAudioSessionCategoryOptionDuckOthers--和其他音频能混合播放,但是当前app声音最大 AVAudioSessionCategoryOptionMixWithOthers--
//和其他音频混合播放,声音大小都一样
[session setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers
error:nil];
NSError *error=nil;
[session setActive:YES error:&error];
//当音频处理完毕后,暂停服务,通知其他软件恢复音频
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
当音频被中断后,在中断源处理完毕后,可以接收到一个通知,然后可以相应恢复音频
代码示例:
//监听音频中断
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruption:)
name:AVAudioSessionInterruptionNotification object:nil];
-(void) audioInterruption:(NSNotification *)notification{
NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];
switch (interruptionType.unsignedIntegerValue) {
case AVAudioSessionInterruptionTypeBegan:{
// • Audio has stopped, already inactive
// • Change state of UI, etc., to reflect non-playing state
} break;
case AVAudioSessionInterruptionTypeEnded:{
NSError *error=nil;
AVAudioSession *session = [AVAudioSession sharedInstance];
//BOOL flag= session.secondaryAudioShouldBeSilencedHint;
[session setActive:YES error:&error];
if(error)
{
ADTLog(@"%@",error);
}
// • Make session active
// • Update user interface
// • AVAudioSessionInterruptionOptionShouldResume option
if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
if (_TTSsynthesizer != nil) {
[_TTSsynthesizer continueSpeaking];
_playStatus = 1;
}
else if ((_audioPlayer != nil)) {
[_audioPlayer prepareToPlay];
[_audioPlayer play];
_playStatus = 3;
}
else if (_audioStreamer != nil) {
_audioStreamer.state=AS_PAUSED;
[_audioStreamer start];
_playStatus = 5;
}
}
} break;
default:
break;
}
}