iOS音频开发相关(四)小结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhhelnice/article/details/82053769

一、代理方法

AVAudioRecorder录音完成: stop或者超过最大时间限制 会调用下面的方法

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag;

但是 AVAudioPlayer 播放完成 会调用下面的方法: stop不会调用下面的方法

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;

播放器操作 AVAudioPlayer

stop 之后 再次调用 play,会继续接着上传的播放时间播放,为了重新开始播放需要设置currentTime0

耳机插入拔出的通知

// 耳机拔出,插入通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionRouteChange:)   name:AVAudioSessionRouteChangeNotification object:nil];

通知处理

#pragma mark - AVAudioSessionRouteChangeNotification
- (void)audioSessionRouteChange:(NSNotification *)noti {
    NSDictionary *interuptionDict = noti.userInfo;
    NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
    NSString *description = [interuptionDict valueForKey:AVAudioSessionRouteChangePreviousRouteKey];
    NSLog(@"%ld -- %@", routeChangeReason, description);
    switch (routeChangeReason) {
        case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
        {
            NSLog(@"耳机插入");
        }
            break;
        case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
        {
            NSLog(@"耳机拔出,暂停播放操作");
            [self pauseAudioPlay];
        }
            break;
        case AVAudioSessionRouteChangeReasonCategoryChange:
        {
            // called at start - also when other audio wants to play
            NSLog(@"AVAudioSessionRouteChangeReasonCategoryChange");
        }
            break;
    }
}

音频播放:

切换到后台会自动暂停音乐播放,再次进入前台,音频会自动播放

切换到后台的通知有:

- `UIApplicationWillResignActiveNotification`:将要进入后台
- `UIApplicationDidEnterBackgroundNotification`: 已经进入后台
  • pause方法在UIApplicationWillResignActiveNotification中调用才会起作用,UIApplicationDidEnterBackgroundNotification调用不起作用
  • stop方法在UIApplicationWillResignActiveNotificationUIApplicationDidEnterBackgroundNotification都会起作用

上拉画出控制面板,app走的是UIApplicationWillResignActiveNotification方法

猜你喜欢

转载自blog.csdn.net/zhhelnice/article/details/82053769