将应用程序升级到xcode 8后,当屏幕自动锁定时,iOS应用程序音频停止
我有一个已经在iTunes应用商店上发布的应用程序,它已经为音频启用了后台模式 更新到XCode 8后,我发布了我的应用程序的更新,之后我发现每当屏幕锁定时,应用程序就会停止播放。除此之外,我并没有对背景剧做任何改动。不确定iOS 9的行为或编码要求是否更改+ 以下是我的代码的作用:将应用程序升级到xcode 8后,当屏幕自动锁定时,iOS应用程序音频停止,ios,audio,Ios,Audio,我有一个已经在iTunes应用商店上发布的应用程序,它已经为音频启用了后台模式 更新到XCode 8后,我发布了我的应用程序的更新,之后我发现每当屏幕锁定时,应用程序就会停止播放。除此之外,我并没有对背景剧做任何改动。不确定iOS 9的行为或编码要求是否更改+ 以下是我的代码的作用: App plist file: <key>UIBackgroundModes</key> <array> <string>audio
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.");
}
}
App plist文件:
UIBackgroundModes
音频
远程通知
音频控制器
-(无效)挫折背景剧:(bool)背景剧
{
NSLog(@“挫折播放%d”,背景播放);
AVAudioSession*mySession=[AVAudioSession sharedInstance];
N错误*音频会话错误=无;
如果(背景剧){
//将播放类别指定给音频会话。
[mySession setCategory:AVAudioSessionCategoryPlayback
错误:&audioSessionError];
OSStatus PropertySetror=0;
UInt32 allowMixing=真;
PropertySetror=AudioSessionSetProperty(
kAudioSessionProperty_重写了与其他人的类别混合,//1
sizeof(allowMixing),//2
&allowMixing//3
);
如果(PropertySetror!=0){
NSLog(@“设置音频属性MixWithOthers时出错”);
}
}否则{
//将播放类别指定给音频会话。
[mySession setCategory:AVAudioSessionCategoryPlayback
错误:&audioSessionError];
}
如果(audioSessionError!=无){
NSLog(@“设置音频会话类别时出错”);
}
}
当我最小化应用程序时,音频会继续播放,直到屏幕自动锁定。每当屏幕打开时(如收到通知时),音频将恢复,然后在屏幕变黑时关闭
如前所述,这些东西过去是可以工作的,而且在升级到Xcode 8/iOS 9之后,似乎已经改变了行为
我试着在论坛和其他地方搜索遇到类似问题的人,但找不到任何东西
任何建议,或一双新的眼睛看这将不胜感激
谢谢,
Sridhar好的,我发现了问题!关于我如何设置背景音频,一切都很好 当屏幕锁打开时,赠送的钥匙正看着设备的控制台: Jan 17 11:03:59我的iPad Talanome[1179]:kAudioUnitErr_TooManyFramesToProcess:inFramesToProcess=4096,mMaxFramesPerSlice=1156 经过一番探索,我找到了这个技术说明- 关键是这个-- 我没有设置maxFramesPerSlice,因此它默认为1156,这对于打开自动锁定(4096)时来说太小了。在我的音频初始化中将maxFramesPerSlice设置为4096可确保在屏幕锁定时有足够的内存 希望这能帮助其他可能面临类似问题的人
-Sridhar您是否在项目设置的“功能”部分启用了背景音频?是的,我启用了!我设法找到了问题和解决方案,我将在下面添加。感谢您保存我的应用程序!
// set the mixer unit to handle 4096 samples per slice since we want to keep rendering during screen lock
UInt32 maxFPS = 4096;
AudioUnitSetProperty(mMixer, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0,
&maxFPS, sizeof(maxFPS));