gpt4 book ai didi

将应用程序升级到 xcode 8 后屏幕自动锁定时 iOS 应用程序音频停止

转载 作者:行者123 更新时间:2023-12-01 15:58:24 25 4
gpt4 key购买 nike

我有一个已在 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 后似乎改变了行为。

我尝试在论坛和其他地方搜索遇到类似问题的人,但无法找到任何内容。

任何建议,或者用新的眼光看待这个问题,我们将不胜感激!

谢谢,斯里达

最佳答案

好的,我找到问题了!关于我如何设置背景音频,一切都很好。

关键的赠品是在屏幕锁定打开时查看设备的控制台:

1 月 17 日 11:03:59 My-iPad Talanome[1179]:kAudioUnitErr_TooManyFramesToProcess:inFramesToProcess=4096,mMaxFramesPerSlice=1156

经过一番搜索,我找到了这份技术说明 - https://developer.apple.com/library/content/qa/qa1606/_index.html

关键是这个——

// 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));

我没有设置 maxFramesPerSlice,因此默认为 1156,这对于自动锁定打开时来说太小(即 4096)。在我的音频初始化中将 maxFramesPerSlice 设置为 4096 确保了屏幕锁定时有足够的空间。

希望这可以帮助其他可能面临类似问题的人!

-斯里达尔

关于将应用程序升级到 xcode 8 后屏幕自动锁定时 iOS 应用程序音频停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41688199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com