【问题标题】:Audio not playing in background using AVAudioPlayer iOS 9使用 AVAudioPlayer iOS 9 无法在后台播放音频
【发布时间】:2017-08-04 07:34:00
【问题描述】:

在前台播放时,使用 AVAudioPlayer iOS 9 不会在后台播放音频。

我已经在 Info.plist 中设置了必需的背景模式应用使用 AirPlay 播放音频或流式传输音频/视频

我还在 Capabilities

中检查了 Audio, Background fetch

使用代码:

@property (nonatomic, strong) AVAudioPlayer *ringtonePlayer;


    NSString *ringtonePath = [[NSBundle mainBundle] pathForResource:@"incoming" ofType:@"wav"];
    if ([ringtonePath length] <= 0) {
        NSLog(@"Can't find incoming sound file");
        return;
    }

    self.ringtonePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:ringtonePath] error:nil];
    self.ringtonePlayer.delegate = self;
    self.ringtonePlayer.numberOfLoops = -1;


    NSError *error = nil;
    if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                                error:&error]) {
        NSLog(@"Unable to reroute audio: %@", [error localizedDescription]);
    } else {
        [[AVAudioSession sharedInstance] setActive:YES error:&error];
    }

    self.ringtonePlayer.volume = 1.0f;
    [self.ringtonePlayer play];

请帮忙,提前谢谢。

【问题讨论】:

    标签: avfoundation avaudioplayer ios9.3


    【解决方案1】:

    我使用以下代码在后台播放,不需要修改info.plist文件。

    func setupPlayBackground(){
    
            do {
                try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
                try AVAudioSession.sharedInstance().setActive(true)
    
            } catch let error {
    
                print(error.localizedDescription)
    
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      相关资源
      最近更新 更多