开发者

playing audio while in background mode doesnt work

In my application audio is not being played when i put my application in background mode ..i have seen such issues in this site itself.it is mentioned that

1) in the app-info.plist add to background modes "audio" 2) in the code i added support for audio sessions

  if([player isPlaying])
    {
   开发者_运维问答     [player stop];
    }

        NSError *error=nil;
        int trackid=arc4random()%10+1;

        NSString *trackname=[NSString stringWithFormat:@"trk-%d",trackid];
    NSLog(@"%@",trackname);
        NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:trackname ofType:@"mp3"];
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL  fileURLWithPath:newAudioFile] error:NULL];


        AVAudioSession *audioSession = [AVAudioSession sharedInstance];

      NSError *setCategoryError = nil;
      [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
         if (setCategoryError) { /* handle the error condition */ }

        NSError *activationError = nil;
         [audioSession setActive:YES error:&activationError];
         if (activationError) { /* handle the error condition */ }
        if(error) {
            NSLog(@"%@",&error);
        }
        [player prepareToPlay];
        [player play];
        player.numberOfLoops=1;

but still it is not working when i put the application in backgroud mode.it resumes when i put the application back.have i missed something?


use this line in the - (void)viewDidLoad

[UIApplication sharedApplication].idleTimerDisabled = YES;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜