2011-07-21 33 views
0

我想在應用程序處於後臺時播放空的音頻文件來停止所有播放我嘗試了以下代碼,但未播放。如何在應用程序處於後臺模式時啓動歌曲

-(void)stop{ 
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
if(_viewController.checkIpod==0){ 
    AVAudioPlayer *playerA = [[AVAudioPlayer alloc] 
       initWithContentsOfURL:[NSURL fileURLWithPath: 
             [[NSBundle mainBundle] pathForResource:@"2sec" ofType:@"mp3"]] 
       error:nil]; 


    [playerA play]; 
    //[player close]; 
    _viewController.onceClosed=1; 
    NSLog(@"Oneclosed=1"); 
} 
else{ 
    //[self.musicPlayer play]; 
    [_viewController.musicPlayer pause]; 
    [_viewController.playerA play]; 
    NSLog(@"closing and playing the sound"); 
} 

}

我把這個代碼在beginBackgroundTaskWithExpirationHandler 請幫

+2

作爲一個用戶,這會讓我瘋狂。我不想在後臺播放任何空的音頻文件。 – dasdom

+0

它會關閉所有的播放 –

回答

0

您需要在您的.plist添加項目,使背景音樂。我認爲該屬性是'必需的背景模式'。

+0

看到,當我播放音頻時意味着應用程序要背景然後它可以工作,但我設置了一個計時器,並希望在定時器完成時播放音頻 –

相關問題