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

标签 iphone audio background

在我的应用程序中,当我将我的应用程序置于后台模式时无法播放音频..我已经在该站点本身中看到了此类问题。

1)在app-info.plist中添加到后台模式“音频”
2)在代码中,我添加了对音频 session 的支持

  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;

但是当我将应用程序置于backgroud模式时仍然无法正常工作。当我将应用程序放回原处时它又恢复了。我错过了什么吗?

最佳答案

在-(void)viewDidLoad中使用此行

[UIApplication sharedApplication].idleTimerDisabled = YES;

关于iphone - 在后台模式下播放音频不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6491872/

相关文章:

iphone - 如何在按下按钮后以动画方式显示 UIPickerView?

ios - 如何让Uibutton被选中

javascript - Javascript:音频播放器出现问题,同时播放所有音频

Android - 当设备插入音频插孔时播放声音

iOS 13.2 消息 : nehelper sent invalid result code [1] for Wi-Fi information request

html - CSS:让文本出现在我的 div 中的渐变之上

c++ - 使用系统托盘图标创建后台进程

ios - NSLayoutconstraints 未按预期工作

ios - 使 iOS 应用程序在启动时运行

java - 使用 jfreechart 绘制音频信号(幅度与时间)