ios中audio session纪录

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;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值