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

2017-12-15  本文已影响76人  凯文Kevin21
  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];
    }
}

上一篇 下一篇

猜你喜欢

热点阅读