2014-01-27 104 views
3

我正在嘗試AVFoundationFramework當應用程序轉到後臺時,AVAudioPlayer不播放

目前我能夠在前臺運行AVAudioPlayer。 當應用程序轉到後臺時,AVAudioPlayer不會繼續播放。

實施的具體步驟是:

  1. 添加後臺任務給的.plist

    enter image description here

  2. 設置AVAudioPlayer

    self.objAVAudioPlayer = [[AVAudioPlayer alloc]initWithData:mp3Data error:&error]; 
    self.objAVAudioPlayer.delegate = self; 
    
  3. 設置AVAudioSession

    if([self.objAVAudioPlayer prepareToPlay]) 
    { 
        AVAudioSession *objAVAudioSession = [AVAudioSession sharedInstance]; 
        [objAVAudioSession setCategory:AVAudioSessionCategoryPlayback error:&error]; 
        [objAVAudioSession setActive:YES 
                error:nil]; 
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
        [self.objAVAudioPlayer play]; 
    } 
    

當應用程序恢復,它從那裏辭職完全一樣的地方開始。

基本SDK是iOS 7.0

任何想法我缺少什麼?任何幫助將不勝感激。

+0

你在你的'有什麼添加以下代碼

UIBackgroundTaskIdentifier backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{ [application endBackgroundTask:backgroundTask]; //Tell the system that we are done with the tasks backgroundTask = UIBackgroundTaskInvalid; //Set the task to be invalid }]; 

到您AppDelegate.m類 - (空)applicationDidEnterBackground :(UIApplication *)應用程序? – Aluminum

+0

尚未在'applicationDidEnterBackground'中實施任何代碼 – footyapps27

+0

您是否使用您的應用ID啓用了應用內音頻權利? – ZeMoon

回答

1

用於播放音頻在後臺模式下你應該建立AVAudioSession和方法

- (void)applicationDidEnterBackground:(UIApplication *)application { 
    //Some code 
} 
+0

一個小小的更新 - 我共享的代碼似乎在'iOS'設備上正常工作。在模擬器中只是一個問題。 – footyapps27

相關問題