美文网首页
iOS app内播放录音, 网易云音乐会中断

iOS app内播放录音, 网易云音乐会中断

作者: 凯文Kevin21 | 来源:发表于2017-12-15 14:39 被阅读76次
  1. 播放音频.
- (void)startPlayRecorder:(NSString *)recorderPath
{
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *err = nil;  // 加上这两句,否则声音会很小
    [audioSession setCategory :AVAudioSessionCategorySoloAmbient error:&err];
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:recorderPath] error:nil];
    self.player.numberOfLoops = 0;
    [self.player prepareToPlay];
    self.player.delegate = self;
    [self.player play];
}
  1. 播放音频结束
#pragma mark - AVAudioPlayerDelegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
                       successfully:(BOOL)flag
{
    [self.player stop];
    self.player = nil;
    //app录音播放结束后, 恢复网易云,优酷等第三方音乐播放器的后台音乐.
     [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];

    if (self.playDelegate && [self.playDelegate respondsToSelector:@selector(voiceDidPlayFinished)]) {
        [self.playDelegate voiceDidPlayFinished];
    }
}

相关文章

网友评论

      本文标题:iOS app内播放录音, 网易云音乐会中断

      本文链接:https://www.haomeiwen.com/subject/yzcdwxtx.html