问题描述
我做了一些初步测试,并且有个好主意,答案是否定的。但是只需确认一下:后台和前景应用程序可以共享音频播放设备吗? (后台应用程序将是我的。前台应用程序将来自第三方)
I did some preliminary test and have a good idea the answer is no. But just need to confirm: Can a background and foreground app share audio playback device? (The background app will be mine. The foreground app will be from third party)
推荐答案
这是可行的,方法如下:
That is possible and here is how:
-
通过执行以下操作,确保应用在后台播放时继续播放音频:
Make sure that app continues playing audio when left in background by doing this:
a)将以下内容添加到您的Info plist文件中:
a) add the following to your Info plist file:
所需的背景模式
项目0->应用播放音频
"Required background modes" "Item 0" -> "App plays audio"
b)为应用程序的AVAudioSession调用setCategory:error:
b) Call setCategory:error: for AVAudioSession of your app:
NSError * setCategoryError = nil;
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback错误:& setCategoryError];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
允许您通过调用AudioSessionSetProperty()将音频与其他应用程序的音频混合:
Allow your audio to be mixed with audio from other apps by calling AudioSessionSetProperty():
OSStatus propertySetError = 0;
OSStatus propertySetError = 0;
UInt32 allowMixing = true;
UInt32 allowMixing = true;
propertySetError = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers,
sizeof(allowMixing),
和amp; allowMixing
);
propertySetError = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (allowMixing), &allowMixing );
您需要链接针对此代码针对AVFoundation和AudioToolbox框架。
You will need to link against AVFoundation and AudioToolbox frameworks for this code.
这篇关于使用音频的背景和前景应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!