【问题标题】:iOS app audio stops when screen auto-locks after upgrading app to xcode 8将应用程序升级到 xcode 8 后屏幕自动锁定时 iOS 应用程序音频停止
【发布时间】: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 后似乎已经改变了行为。

我尝试在论坛和其他地方搜索遇到类似问题的人,但找不到任何东西。

任何建议,或一双新的眼睛看着这个将不胜感激!

谢谢, 斯里达尔

【问题讨论】:

  • 您是否在项目设置的“功能”部分启用了背景音频?
  • 是的,我愿意!我设法找到了问题和解决方案,我将在下面添加。

标签: ios audio


【解决方案1】:

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

当屏幕锁定打开时,钥匙赠品正在查看设备的控制台:

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 可确保我有足够的时间来应对屏幕锁定。

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

-斯里达尔

【讨论】:

  • 感谢您保存我的应用程序!
猜你喜欢
  • 1970-01-01
  • 2012-06-20
  • 2021-03-20
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
相关资源
最近更新 更多