在后台模式下播放音频不起作用

时间:2011-06-27 11:05:05

标签: iphone audio background

在我的应用程序中,当我将我的应用程序置于后台模式时,音频没有播放..我在这个网站本身已经看到了这样的问题。提到

1)在app-info.plist中添加背景模式“audio” 2)在代码中我添加了对音频会话的支持

  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;

但是当我将应用程序置于背景模式时它仍然无法工作。当我将应用程序恢复时,它会恢复。我错过了什么?

1 个答案:

答案 0 :(得分:1)

在 - (void)viewDidLoad

中使用此行
[UIApplication sharedApplication].idleTimerDisabled = YES;
相关问题