2012-04-24 53 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