AVAudioPlayer,使用AmbientSound会话,当应用程序在后台是不是在玩 [英] AVAudioPlayer, using AmbientSound session, is not playing when app is in background

查看:190
本文介绍了AVAudioPlayer,使用AmbientSound会话,当应用程序在后台是不是在玩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是在苹果的文档,我已经没有能够找到它 - 希望有人可以帮助:

我的应用起着偶尔短音频剪辑 - 我想要的音频与来自其他应用程序中,如iPod应用程序后台音频播放拌入 - 但我也希望它进行演奏时,应用程序在运行这些音频剪辑背景。

我已经设置应用程序播放音频中的info.plist中所需的背景模式设置(应用程序也在使用定位服务也这样也有中集)

我的应用程序上的applicationDidFinishLaunching设置了一个音频会话:

  AudioSessionInitialize(NULL,NULL,NULL,NULL);
UInt32的sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof的(sessionCategory),放大器; sessionCategory);
AudioSessionSetActive(真);

在我的viewWillAppear中:在激活视图的方法我有:

  [超级viewWillAppear中:动画]
[UIApplication的sharedApplication] beginReceivingRemoteControlEvents]
[个体经营becomeFirstResponder]

和相应的事件处理程序和endReceivingRemoteControlEvents code在viewWillDisappear:在的的iOS 4:背景音乐遥控器

最后,我有一个AVAudioPlayer,以正常的方式设置,即播放声音某些事件

  bool的成功= [avAudioPlayer玩]
如果(!成功)
    的NSLog(@没有发挥);

当应用程序是在前台应用程序工作正常,播放音频 - 但是当应用进入后台和应用程序试图播放声音从[avAudioPlayer播放]返回值是NO,声音不播放 - 当切换回前台音频重新开始工作。

如果当我建立会话我改用

  UInt32的sessionCategory = kAudioSessionCategory_MediaPlayback;

然后,音频前景和背景完美播放。但是的MediaPlayback是不是真的为这个应用程序的正确方式,因为我只是偶尔播放音频剪辑 - AmbientSound是真的,我应该使用的模式。

我在想什么?难道只是无法使用kAudioSessionCategory_AmbientSound在后台播放音频?如果是这样我还没有发现有关的文档中任何事情。


解决方案

不得不提交技术支持请求,这到底。

据苹果后台播放不受AmbientSound类别的支持 - 你必须使用的MediaPlayback

他们声称这是在文档中 - 我又看了一下,我找不到它。

好了,所以让背景声音混合成的MediaPlayback是很容易的使用kAudioSessionProperty_OverrideCategoryMixWithOthers - 但现在我将不得不通过其他跳火圈复制其他AmbientSound功能(服从静音开关和锁定时不打)。我真的不明白为什么不支持AmbientSound在后台播放 - 但是我们去

If this is in the apple doc then I've not been able to find it - hoping someone can help:

My app plays occasional short audio clips - I want the audio to mix in with audio playing from other apps in the background like the iPod app - but I also want it to carry on playing these audio clips when the app is running in background.

I have set "App plays audio" in the Required Background Modes settings in info.plist (the app is also using location services too so that is also set in there)

My app sets up an audio session on applicationDidFinishLaunching:

AudioSessionInitialize (NULL,NULL,NULL,NULL);
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,sizeof (sessionCategory),&sessionCategory); 
AudioSessionSetActive(true);

In my viewWillAppear: method in the view that is active I have:

[super viewWillAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder]; 

and the corresponding event handler and endReceivingRemoteControlEvents code in viewWillDisappear: as discussed in iOS 4: Remote controls for background audio

Finally I have an AVAudioPlayer, set up in the normal way, that plays a sound on certain events

bool successful = [avAudioPlayer play];
if(!successful)
    NSLog(@"did not play");

When the app is in foreground the app works fine and plays the audio - but when the app goes into background and the app attempts to play a sound the return value from the [avAudioPlayer play] is NO and the sound does not play - when switched back to foreground the audio starts working again.

If when I set up the session I instead use

    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;

Then the audio plays in foreground and background perfectly. But MediaPlayback is not the really the right mode for this app since I am only occasionally playing audio clips - AmbientSound is really the mode I should be using.

What am I missing? Is it just not possible to use kAudioSessionCategory_AmbientSound to play Audio in the background? If so I've not found anything in the documentation about that.

解决方案

Had to submit a tech support request for this in the end.

According to Apple background playback is not supported by the AmbientSound category - you have to use MediaPlayback.

They claim this is in the documentation - I've looked again and I could not find it.

Ok so getting background sounds to mix into MediaPlayback is easy enough using kAudioSessionProperty_OverrideCategoryMixWithOthers - but I am now going to have to jump through some other hoops to replicate the other AmbientSound functionality (obeying the mute switch and not playing when locked). I really don't understand why AmbientSound is not supported playing in background - but there we go.

这篇关于AVAudioPlayer,使用AmbientSound会话,当应用程序在后台是不是在玩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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