一、代理方法
AVAudioRecorder
录音完成: stop
或者超过最大时间限制
会调用下面的方法
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag;
但是 AVAudioPlayer
播放完成 会调用下面的方法: stop
不会调用下面的方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
播放器操作 AVAudioPlayer
stop
之后 再次调用 play
,会继续接着上传的播放时间播放,为了重新开始播放需要设置currentTime
为0
耳机插入拔出的通知
// 耳机拔出,插入通知
[[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
方法在UIApplicationWillResignActiveNotification
和UIApplicationDidEnterBackgroundNotification
都会起作用