2016-10-17 137 views
0

我的項目相關音樂和我關閉屏幕時停止播放音樂的問題。儘管我註冊了背景模式(音頻),但有時當我關閉屏幕時,AVPlayer停止播放音樂,並在開啓屏幕時再次播放。AVPlayer在關閉屏幕時停止播放音樂

我花了很多時間,但我可以知道它爲什麼發生。

+0

你可以在這裏找到正確的答案:http://stackoverflow.com/questions/10429204/how-to-handle-background-audio-playing-while-ios-device-is-locked- or-on-another – iGenio

+0

按照這個答案我用我的應用程序的方式,但問題仍然出現 –

+0

音頻是從互聯網流傳輸還是在本地文件? – iGenio

回答

0

僅僅添加audio背景模式是不夠的。您還必須設置與回放音頻會話類,如described in Apple QA1668

答:你必須聲明你的應用程序播放的音頻內容,而在後臺,並分配一個合適的類別音頻會議。

#import <AVFoundation/AVFoundation.h> 
#import <AudioToolbox/AudioToolbox.h> 

AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 

NSError *setCategoryError = nil; 
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError]; 
if (!success) { /* handle the error condition */ } 

NSError *activationError = nil; 
success = [audioSession setActive:YES error:&activationError]; 
if (!success) { /* handle the error condition */ } 
相關問題