当应用程序处于后台时,使用 AmbientSound 会话的 AVAudioPlayer 不会播放
如果这是在苹果文档中,那么我一直无法找到它 - 希望有人可以提供帮助:
我的应用程序偶尔播放短音频剪辑 - 我希望音频与后台其他应用程序(如 iPod)播放的音频混合在一起应用程序 - 但我也希望它在应用程序在后台运行时继续播放这些音频剪辑。
我已在 info.plist 中的“所需后台模式”设置中设置了“应用程序播放音频”(该应用程序也使用位置服务,因此也在那里设置)
我的应用程序在 applicationDidFinishLaunching: 上设置了音频会话:
AudioSessionInitialize (NULL,NULL,NULL,NULL);
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,sizeof (sessionCategory),&sessionCategory);
AudioSessionSetActive(true);
在我的 viewWillAppear: 方法中在处于活动状态的视图中,我有:
[super viewWillAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
以及 viewWillDisappear 中相应的事件处理程序和 endReceivingRemoteControlEvents 代码: 如 iOS 4:背景音频的远程控制
最后我有一个 AVAudioPlayer,设置以正常方式,在某些事件上播放声音
bool successful = [avAudioPlayer play];
if(!successful)
NSLog(@"did not play");
当应用程序位于前台时,应用程序工作正常并播放音频 - 但是当应用程序进入后台并且应用程序尝试播放声音时,返回值来自[avAudioPlayer play] 为“否”,并且不播放声音 - 当切换回前台时,音频再次开始工作。
如果当我设置会话时我改为使用
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
那么音频会在前景和背景中完美播放。但 MediaPlayback 并不是这个应用程序真正正确的模式,因为我只是偶尔播放音频剪辑 - AmbientSound 确实是我应该使用的模式。
我缺少什么?是否无法使用 kAudioSessionCategory_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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终不得不为此提交技术支持请求。
根据 Apple 的说法,AmbientSound 类别不支持后台播放 - 您必须使用 MediaPlayback。
他们声称这是在文档中 - 我又看了一遍,但找不到它。
好的,使用 kAudioSessionProperty_OverrideCategoryMixWithOthers 将背景声音混合到 MediaPlayback 中非常容易 - 但我现在必须跳过其他一些环节来复制其他 AmbientSound 功能(遵守静音开关并且在锁定时不播放)。我真的不明白为什么 AmbientSound 不支持在后台播放 - 但我们就这样了。
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.