可以在没有MixWithOthers的情况下在背景状态下激活AVAudioSession吗? [英] Can AVAudioSession be activated in Background state without MixWithOthers?

查看:95
本文介绍了可以在没有MixWithOthers的情况下在背景状态下激活AVAudioSession吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于CoreLocation已请求AlwaysAuthorization,因此我的应用程序在后台运行.这样可以防止应用程序进入暂停状态.它保持在后台状态,并且正在接收位置事件.在发生一些位置事件之后,我想激活AVAudioSession并播放一些声音.例如,当我现在在前台使用其他应用程序并且此应用程序正在播放音频时,如何正确激活会话(在此后台应用程序中).假设我正在Youtube应用中观看一些视频,而我的后台应用现在需要播放音频.在这种情况下,我在激活音频会话时遇到问题.

So my app is running in the background because of CoreLocation that has requested AlwaysAuthorization. This prevents app to reach suspended state. It stays in background state and is receiving location events. After some location events I would like to activate AVAudioSession and play some sounds. How to activate session properly (in this background app) when I'm using other app in foreground right now and this app is playing audio track for example. Suppose I'm watching some video in Youtube app and my background app needs to play audio right now. I've problems in activating audio session in this case.

在这种情况下,我不能使用 AudioToolbox 播放引擎,因为即使打开铃声/静音"开关,也需要播放这些声音.

I can't use AudioToolbox playing engine in this case because those sounds needs to be played even when Ring/Silent switch is on.

我知道我需要使用 AVAudioSessionCategoryPlayback 类别.我也不想同时播放其他应用程序,因此我不使用 MixWithOthers DuckOthers 类别选项.

I know I need to use AVAudioSessionCategoryPlayback category. I also don't want other apps to be played in the same time so I'm not using MixWithOthers or DuckOthers category options.

我已启用 UIBackgroundModes 以便在后台播放音频.

I have enabled UIBackgroundModes for audio playback in background.

在应用处于后台且其他应用当前正在播放音频而未启用任何混合选项的情况下,如何激活此类会话.如何在当前的前台应用程序中中断音频会话,以便我的后台应用程序可以激活它以实现播放时间.播放结束后,我的计划是在后台停用音频会话,并通知其他应用程序他们可以恢复其播放.

How to activate such session when app is in background and other app currently is playing audio without any mixing options enabled. How to interrupt audio session in current foreground app so my background app can activate it for playback time. After playing is over my plan is to deactivate audio session in background and notify other apps that they can resume theirs playback.

推荐答案

将以下三行添加到 AppDelegate.m 类的 didFinishLaunchingWithOptions 方法中,然后再次测试该应用.让我知道与此有关的任何问题.

Add these below 3 lines in the didFinishLaunchingWithOptions method of AppDelegate.m class and test the app again. Let me know if there is any problem regarding the same.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[UIApplication sharedApplication].idleTimerDisabled = YES;

这篇关于可以在没有MixWithOthers的情况下在背景状态下激活AVAudioSession吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆