iOS AVAudioPlayer让其他应用程序的背景音乐停止
我有一个需要播放声音的练习应用程序。我使用AVAudioPlayer播放声音。 但是当音频开始播放时,来自另一个应用程序(广播流应用程序)的背景音乐被关闭。iOS AVAudioPlayer让其他应用程序的背景音乐停止
我该如何让它不中断背景音乐?因为我希望用户在锻炼时听音乐。
谢谢。
您可以使用在您使用AVAudioPlayer下面的代码:基于SujithPt的答案在这里
[[AVAudioSession sharedInstance] setDelegate:self];
你是否真的需要设置代表。设置类别应该足够了。 – 2013-01-08 12:12:09
我测试了代码,只需要调用setCategory就可以了。相应地更改了示例代码。 – 2013-01-08 13:12:55
当来自其他应用程序的音频在后台播放时,我的应用程序会崩溃,原因是:'所需条件为false:_engine-> IsRunning()'。这解决了这个问题。 – vikzilla 2015-09-07 07:26:21
:
// Set AudioSession
NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error:&sessionError];
如果您想设置一个委托,您可以添加这行代码是在快速同样的事情:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
在我的情况下,我想背景音频播放音量较低,因为我的应用播放的声音更像是一个警报。我用这个:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// prevents our app from stopping other audio,
// e.g. music, podcats, etc. that may be playing when launched
// our audio will be played at a higher volume
// and the background audio will "duck" until done
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryMultiRoute
withOptions:AVAudioSessionCategoryOptionDuckOthers
error:nil];
}
对于雨燕2.2,添加这个你玩之前的任何地方或preparetoplay:
_ = try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
的[iPhone AVAudioPlayer停止背景音乐]可能重复(http://stackoverflow.com/questions/1672602/iphone-avaudioplayer-stopping-background-music) – benzado 2012-07-12 02:38:26
你需要使用不同类型的音频会话 – bkbeachlabs 2012-07-12 03:00:10