当自己视频APP,遇到别人音频APP后台播放音频时候

        当别家音乐APP播放音乐,打开自家视频APP(虽然没有播放视频),但音乐app的音频却暂停了(如果是钉钉语音会议这种情况,会自动退出语音会议)。

        查了资料:这是音频打断处理问题,举个例子:
        第一种情况:比如闹铃打断的情况。先是AVAudioSessionInterruptionTypeBegan,闹铃结束调AVAudioSessionInterruptionTypeEnd,通知中的option是 AVAudioSessionInterruptionOptionShouldResume 可恢复。电话的中断也类似。

        第二种情况:其他音乐APP正在播放,视频APP到前台也会收到系统的音频中断通知,但音乐app它是一直播放的。系统不可能等到它播放完再调xxxxend通知,所以会立即调用end,但通知中的option字段是0.

        目前来看还是视频APP做一些操作,导致立即发送了AVAudioSessionInterruptionTypeEnd通知。果然,查阅代码后发现,视频APP有调用

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

 

        这个意思就是不让其他音乐APP播放。所以视频APP会立即收到AVAudioSessionInterruptionTypeEnd通知。

        这时候就要根据视频APP的状态和AVAudioSessionInterruptionOptionKey的值来处理逻辑了:

        代码示例:

/**
 *  处理音频打断
 */
-(void)AVAudioSessionInterruptionNotification:(NSNotification *)notif{
    NSDictionary *interruptionDictionary = [notif userInfo];
    AVAudioSessionInterruptionType type =
    [interruptionDictionary [AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
    AVAudioSessionInterruptionOptions option = [interruptionDictionary [AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
    switch (type) {
        case AVAudioSessionInterruptionTypeBegan:
		if(如果当前不需要播放视频){//需要释放音频控制权
	         [[AVAudioSession sharedInstance] setActive:NO
                                           withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
                                                 error:nil];
	}
            break;
        case AVAudioSessionInterruptionTypeEnded:
            if (option == AVAudioSessionInterruptionOptionShouldResume){
                   if (正在播放视频){
                        //继续播放视频
			//设置setactive
			[[AVAudioSession sharedInstance] setActive:YES error:nil];
                    }
            }
            break;
        default:
            break;
    }
}

注:

  • AVAudioSessionCategoryAmbient : 只用于播放音乐时,并且可以和QQ音乐同时播放,比如玩游戏的时候还想听QQ音乐的歌,那么把游戏播放背景音就设置成这种类别。同时,当用户锁屏或者静音时也会随着静音,这种类别基本使用所有App的背景场景。
  • AVAudioSessionCategorySoloAmbient: 也是只用于播放,但是和"AVAudioSessionCategoryAmbient"不同的是,用了它就别想听QQ音乐了,比如不希望QQ音乐干扰的App,类似节奏大师。同样当用户锁屏或者静音时也会随着静音,锁屏了就玩不了节奏大师了。
  • AVAudioSessionCategoryPlayback: 如果锁屏了还想听声音怎么办?用这个类别,比如App本身就是播放器,同时当App播放时,其他类似QQ音乐就不能播放了。所以这种类别一般用于播放器类App
  • AVAudioSessionCategoryRecord: 有了播放器,肯定要录音机,比如微信语音的录制,就要用到这个类别,既然要安静的录音,肯定不希望有QQ音乐了,所以其他播放声音会中断。想想微信语音的场景,就知道什么时候用他了。
  • AVAudioSessionCategoryPlayAndRecord: 如果既想播放又想录制该用什么模式呢?比如VoIP,打电话这种场景,PlayAndRecord就是专门为这样的场景设计的 。
  • AVAudioSessionCategoryMultiRoute: 想象一个DJ用的App,手机连着HDMI到扬声器播放当前的音乐,然后耳机里面播放下一曲,这种常人不理解的场景,这个类别可以支持多个设备输入输出。
  • AVAudioSessionCategoryAudioProcessing: 主要用于音频格式处理,一般可以配合AudioUnit进行使用

了解了这七大类别,我们就可以根据自己的需要进行对应类别的设置了

 

 

参考:http://www.voycn.com/article/yinshipinkaifa-avaudiosessionzaikaifazhongdeyingyong
https://www.jianshu.com/p/1ce64fd142fa
http://www.voycn.com/article/yinshipinkaifa-avaudiosessionzaikaifazhongdeyingyong

https://www.jianshu.com/p/3e0a399380df

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用AVAudioSession的category选项来监听当前app是否正在播放音频。当你的app被切换到后台或被其他app打断时,AVAudioSession的interruptionNotification会被发送。你可以在AppDelegate中添加以下代码: ```objective-c - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. AVAudioSession *audioSession = [AVAudioSession sharedInstance]; NSError *error; BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&error]; if (!success) { NSLog(@"Error setting category: %@", error.localizedDescription); } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterruption:) name:AVAudioSessionInterruptionNotification object:nil]; return YES; } - (void)handleInterruption:(NSNotification *)notification { AVAudioSessionInterruptionType type = [notification.userInfo[AVAudioSessionInterruptionTypeKey] integerValue]; if (type == AVAudioSessionInterruptionTypeBegan) { // Audio has been interrupted, pause playback NSLog(@"Audio has been interrupted"); } else if (type == AVAudioSessionInterruptionTypeEnded) { AVAudioSessionInterruptionOptions options = [notification.userInfo[AVAudioSessionInterruptionOptionKey] integerValue]; if (options == AVAudioSessionInterruptionOptionShouldResume) { // Audio has resumed, resume playback NSLog(@"Audio has resumed"); } } } ``` 这段代码会将AVAudioSession的category设置为AVAudioSessionCategoryPlayback,这样可以保证即使你的app被切换到后台或被其他app打断,音频依然可以继续播放。同时,它也会添加一个AVAudioSessionInterruptionNotification的观察者,以便在音频被打断或恢复时接收通知。在handleInterruption方法中,你可以根据通知的类型来暂停或恢复音频播放

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值