一、代理方法
AVAudioRecorder錄音完成: stop或者超過最大時(shí)間限制 會(huì)調(diào)用下面的方法
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag;
但是 AVAudioPlayer 播放完成 會(huì)調(diào)用下面的方法: stop不會(huì)調(diào)用下面的方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
播放器操作 AVAudioPlayer
stop 之后 再次調(diào)用 play,會(huì)繼續(xù)接著上傳的播放時(shí)間播放,為了重新開始播放需要設(shè)置currentTime為0
耳機(jī)插入拔出的通知
// 耳機(jī)拔出,插入通知
[[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(@"耳機(jī)插入");
}
break;
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
{
NSLog(@"耳機(jī)拔出,暫停播放操作");
[self pauseAudioPlay];
}
break;
case AVAudioSessionRouteChangeReasonCategoryChange:
{
// called at start - also when other audio wants to play
NSLog(@"AVAudioSessionRouteChangeReasonCategoryChange");
}
break;
}
}
音頻播放:
切換到后臺(tái)會(huì)自動(dòng)暫停音樂播放,再次進(jìn)入前臺(tái),音頻會(huì)自動(dòng)播放
切換到后臺(tái)的通知有:
- `UIApplicationWillResignActiveNotification`:將要進(jìn)入后臺(tái)
- `UIApplicationDidEnterBackgroundNotification`: 已經(jīng)進(jìn)入后臺(tái)
-
pause方法在UIApplicationWillResignActiveNotification中調(diào)用才會(huì)起作用,UIApplicationDidEnterBackgroundNotification調(diào)用不起作用 -
stop方法在UIApplicationWillResignActiveNotification和UIApplicationDidEnterBackgroundNotification都會(huì)起作用