2012-04-24 78 views
0

我已經嘗試了一切:AVAudioPlayer後臺任務不工作[的iOS]

-setup的info.plist 所需的背景模式:應用程序播放音頻;

-setup球員:

[[AVAudioSession sharedInstance] setDelegate: self]; 

// allow app continue to play when the screen is locked 
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback 
             error:nil]; 

UInt32 doSetProperty = 0; 
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, 
         sizeof (doSetProperty), 
         &doSetProperty 
         ); 

// Registers the audio route change listener callback function 
AudioSessionAddPropertyListener (
           kAudioSessionProperty_AudioRouteChange, 
           audioRouteChangeListenerCallback, 
           self 
           ); 

// Activates the audio session. 

NSError *activationError = nil; 
[[AVAudioSession sharedInstance] setActive: YES error: &activationError]; 

[appSoundPlayer prepareToPlay]; 
[appSoundPlayer setVolume: 1.0]; 
[appSoundPlayer setDelegate: self]; 
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

什麼我忘了?有什麼不對?

+0

你可以發郵件給我你的代碼。我一直在努力,它正常工作,第二件事是你在設備上測試它還是隻是模擬器。在模擬器的情況下,它不會工作。 – 2012-04-24 09:08:41

+0

請點擊此鏈接http://stackoverflow.com/questions/10276096/cant-beginreceivingremotecontrolevents-in-ios/10278392#10278392。如果即使你沒有得到它,那麼請給我發電子郵件你的代碼,我會解決它insha阿拉,[email protected] – 2012-04-24 09:20:28

回答

1

您需要設置音頻類別來回放:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error]; 

然後,如果您希望允許混合與其他應用程序的音頻,你可以添加這個選項,只要確保做到這一點之前設置音頻會話激活。

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; 

現在你可以將音頻會話設置爲激活:

[[AVAudioSession sharedInstance] setActive:YES error:&error]; 
+0

這個答案只是保存了我的培根! – 2014-12-18 23:49:35