开发者

how can i start a song when app is in background mode

I want to stop all playbacks by playing a empty audio file when my app is in background I tried following code but I was not played.

 -(void)stop{
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
if(_viewController.checkIpod==0){
    AVAudioPlayer *playerA = [[AVAudioPlayer alloc]
              开发者_StackOverflow社区 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");
}

}

I placed this code in beginBackgroundTaskWithExpirationHandler Please help


You need to add an item in your .plist to enable background audio. I think the property is 'Required background modes'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜