2011-06-29 41 views

回答

-1

據我所知,設備鎖定時無法播放聲音。難道你不能只是禁止設備鎖定和活着的應用程序一直活躍,直到用戶手動關閉它爲止?

+0

很多警報長時間鎖定後纔開始播放音頻。 –

3

您需要在Apple的文檔中閱讀Executing Code in the Background。如果您將UIBackgroundModes鍵設置爲您應用的Info.plistaudio,音頻將在後臺播放時繼續播放。

請參閱部分聲明您支持的背景任務在上述文檔中播放背景音頻

0

我們需要兩個步驟,以在與AVAudioPlayer背景音樂播放:

第1步:選擇項目 - >性能 - >啓用後臺模式 - >選擇音頻模式

第2步:使用驗證碼:

NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"demo" withExtension:@"mp3"]; 
avSound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil]; 
[avSound setDelegate:self]; 
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; 
[[AVAudioSession sharedInstance] setActive: YES error: nil]; 
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
[avSound prepareToPlay]; 
[avSound setNumberOfLoops:-1]; 
[avSound setVolume:1.0]; 

注意,這條線的音樂可以開始,即使應用程序在後臺:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
相關問題