一、代理方法
AVAudioRecorder
錄音完成: stop
或者超過最大時間限制
會調(diào)用下面的方法
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag;
但是 AVAudioPlayer
播放完成 會調(diào)用下面的方法: stop
不會調(diào)用下面的方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
播放器操作 AVAudioPlayer
stop
之后 再次調(diào)用 play
,會繼續(xù)接著上傳的播放時間播放缰盏,為了重新開始播放需要設置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`: 已經(jīng)進入后臺
-
pause
方法在UIApplicationWillResignActiveNotification
中調(diào)用才會起作用涮总,UIApplicationDidEnterBackgroundNotification
調(diào)用不起作用 -
stop
方法在UIApplicationWillResignActiveNotification
和UIApplicationDidEnterBackgroundNotification
都會起作用