2017-01-17 59 views
0

我有一個已在iTunes App Store上發佈的應用程序,並且已爲音頻啓用後臺模式。將應用程序升級到xcode後屏幕自動鎖定時,iOS應用程序音頻停止播放

更新到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/9的iOS

我試着搜索論壇和其他地方的人遇到類似的問題,但避風港無法找到任何東西。

任何建議,或一雙新鮮的眼睛看着這個將不勝感激!

感謝, 斯里達爾

+1

您是否在項目設置的功能部分啓用了背景音頻? – raidfive

+0

是的,我喜歡!我設法找到了問題和解決方案,我將在下面添加。 –

回答

1

好吧,我發現這個問題!關於我如何設置背景音頻,一切都很好。

關鍵贈品一直在尋找在設備的控制檯,當屏幕鎖定已開啓:

1月17日11時03分59秒我的,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可確保在屏幕鎖定時有足夠的空間。

希望這可以幫助其他可能面臨類似問題的人!

-Sridhar

相關問題