|
@@ -50,6 +50,8 @@
|
|
|
|
|
|
@property (nonatomic, strong) dispatch_semaphore_t volumeChangeSemaphore;
|
|
|
|
|
|
+@property (nonatomic, assign) BOOL isInterrupt; // 是否被打断
|
|
|
+
|
|
|
@end
|
|
|
|
|
|
|
|
@@ -109,9 +111,7 @@
|
|
|
if (error) {
|
|
|
self.audioEngine = nil;
|
|
|
// 错误回调
|
|
|
- if (self.delegate && [self.delegate respondsToSelector:@selector(enginePlayerDidError:error:)]) {
|
|
|
- [self.delegate enginePlayerDidError:self error:error];
|
|
|
- }
|
|
|
+ [self sendInterruptError:error];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -276,11 +276,6 @@
|
|
|
|
|
|
AVAudioFrameCount minFrameCount = MIN(bgBuffer.frameLength, recordBuffer.frameLength);
|
|
|
AVAudioFrameCount offsetFrame = labs(offsetTime)/1000.0 * recordBuffer.format.sampleRate;
|
|
|
- // 检查 floatChannelData 是否为空
|
|
|
- if (!bgBuffer.floatChannelData[0] || !bgBuffer.floatChannelData[1] || !recordBuffer.floatChannelData[0]) {
|
|
|
- NSLog(@"Error: Float channel data is null");
|
|
|
- return;
|
|
|
- }
|
|
|
|
|
|
float *bgLeftChannel = bgBuffer.floatChannelData[0];
|
|
|
float *bgRightChannel = bgBuffer.floatChannelData[1];
|
|
@@ -342,36 +337,55 @@
|
|
|
AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
|
|
|
if (type == AVAudioSessionInterruptionTypeBegan) {
|
|
|
//Handle InterruptionBegan
|
|
|
- if (self.delegate && [self.delegate respondsToSelector:@selector(enginePlayerDidError:error:)]) {
|
|
|
+ if (self.isInterrupt == NO) {
|
|
|
+ // 停止播放
|
|
|
+ if (self.nodePlayer.isPlaying) {
|
|
|
+ [self.nodePlayer stop];
|
|
|
+ }
|
|
|
+ self.isInterrupt = YES;
|
|
|
NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:99999 userInfo:@{@"errorDesc" : @"播放被打断"}];
|
|
|
- [self.delegate enginePlayerDidError:self error:error];
|
|
|
+ [self sendInterruptError:error];
|
|
|
}
|
|
|
}
|
|
|
else if (type == AVAudioSessionInterruptionTypeEnded) {
|
|
|
AVAudioSessionInterruptionOptions options = [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
|
|
|
- if (options == AVAudioSessionInterruptionOptionShouldResume) {
|
|
|
+ if (options == AVAudioSessionInterruptionOptionShouldResume && self.isInterrupt) {
|
|
|
//Handle Resume
|
|
|
- [[AVAudioSession sharedInstance] setActive:YES error:nil];
|
|
|
+ NSError *error = nil;
|
|
|
+ [[AVAudioSession sharedInstance] setActive:YES error:&error];
|
|
|
+ if (error) {
|
|
|
+ NSLog(@"------ error desc %@", error.description);
|
|
|
+ }
|
|
|
+ NSLog(@"---- 播放恢复");
|
|
|
+ self.isInterrupt = NO;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (void)handleRouteChange:(NSNotification *)notification {
|
|
|
-
|
|
|
NSDictionary *info = notification.userInfo;
|
|
|
- if ([notification.name isEqualToString:AVAudioSessionRouteChangeNotification]) {
|
|
|
- AVAudioSessionRouteChangeReason routeChangeReason = [[info valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
|
|
|
- if (routeChangeReason == AVAudioSessionRouteChangeReasonCategoryChange) {
|
|
|
- return;
|
|
|
+ AVAudioSessionRouteChangeReason reason = [info[AVAudioSessionRouteChangeReasonKey] unsignedIntegerValue];
|
|
|
+ if (reason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
|
|
|
+ // 耳机拔出时暂停音频
|
|
|
+ if (self.nodePlayer.isPlaying) {
|
|
|
+ NSError *error = nil;
|
|
|
+ [self sendInterruptError:error];
|
|
|
}
|
|
|
- if (self.delegate && [self.delegate respondsToSelector:@selector(enginePlayerDidError:error:)]) {
|
|
|
- NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:99999 userInfo:@{@"errorDesc" : @"播放被打断"}];
|
|
|
- [self.delegate enginePlayerDidError:self error:error];
|
|
|
+
|
|
|
+ } else if (reason == AVAudioSessionRouteChangeReasonNewDeviceAvailable) {
|
|
|
+ // 耳机插入时恢复音频
|
|
|
+ if (self.nodePlayer.isPlaying) {
|
|
|
+ NSError *error = nil;
|
|
|
+ [self sendInterruptError:error];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+- (void)sendInterruptError:(NSError *)error {
|
|
|
+ if (self.delegate && [self.delegate respondsToSelector:@selector(enginePlayerDidError:error:)]) {
|
|
|
+ [self.delegate enginePlayerDidError:self error:error];
|
|
|
+ }
|
|
|
+}
|
|
|
#pragma mark ------ play action
|
|
|
|
|
|
- (void)changeRecordDelay:(NSInteger)delayMs {
|
|
@@ -379,6 +393,10 @@
|
|
|
}
|
|
|
|
|
|
- (void)seekToTimePlay:(NSInteger)time {
|
|
|
+ if (self.isInterrupt) {
|
|
|
+ [self sendInterruptError:nil];
|
|
|
+ return;
|
|
|
+ }
|
|
|
if (self.audioEngine.isRunning == NO) {
|
|
|
[self startEngine];
|
|
|
}
|
|
@@ -397,6 +415,10 @@
|
|
|
}
|
|
|
|
|
|
- (void)seekToTime:(NSInteger)time {
|
|
|
+ if (self.isInterrupt) {
|
|
|
+ [self sendInterruptError:nil];
|
|
|
+ return;
|
|
|
+ }
|
|
|
if (self.audioEngine.isRunning == NO) {
|
|
|
[self startEngine];
|
|
|
}
|