【发布时间】:2017-01-17 02:32:56
【问题描述】:
我有一个已在 iTunes App Store 上发布的应用程序,它为音频启用了后台模式。
更新到 XCode 8 后,我为我的应用发布了更新,之后我发现只要屏幕锁定,应用就会停止播放。否则我没有对后台播放进行任何更改。不确定 iOS 9+ 的行为或编码要求是否发生了变化
这是我的代码的作用:
App plist file:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>remote-notification</string>
</array>
AudioController.m
-(void)setBackgroundPlay:(bool)backgroundPlay
{
NSLog(@"setBackgroundPlay %d", backgroundPlay);
AVAudioSession *mySession = [AVAudioSession sharedInstance];
NSError *audioSessionError = nil;
if (backgroundPlay) {
// Assign the Playback category to the audio session.
[mySession setCategory: AVAudioSessionCategoryPlayback
error: &audioSessionError];
OSStatus propertySetError = 0;
UInt32 allowMixing = true;
propertySetError = AudioSessionSetProperty (
kAudioSessionProperty_OverrideCategoryMixWithOthers, // 1
sizeof (allowMixing), // 2
&allowMixing // 3
);
if (propertySetError != 0) {
NSLog (@"Error setting audio property MixWithOthers");
}
} else {
// Assign the Playback category to the audio session.
[mySession setCategory: AVAudioSessionCategoryPlayback
error: &audioSessionError];
}
if (audioSessionError != nil) {
NSLog (@"Error setting audio session category.");
}
}
当我最小化应用程序时,音频会继续播放,并且会继续播放直到屏幕自动锁定。每当屏幕打开时(例如收到通知时),音频就会恢复,然后在屏幕变黑时关闭。
如前所述,这些东西曾经可以工作,并且在更新到 Xcode 8/iOS 9 后似乎已经改变了行为。
我尝试在论坛和其他地方搜索遇到类似问题的人,但找不到任何东西。
任何建议,或一双新的眼睛看着这个将不胜感激!
谢谢, 斯里达尔
【问题讨论】:
-
您是否在项目设置的“功能”部分启用了背景音频?
-
是的,我愿意!我设法找到了问题和解决方案,我将在下面添加。