Browse Source

tuner部分设备 电话触发crash问题处理

Steven 9 months ago
parent
commit
18a355229b

+ 9 - 3
KulexiuForTeacher/KulexiuForTeacher/Common/Base/AccompanyWebView/KSAccompanyWebViewController.m

@@ -1600,9 +1600,15 @@
 
 #pragma mark ----- 跟音模块
 - (void)startTuner {
-    if (self.isTunerRuning == NO) {
-        self.isTunerRuning = YES;
-        [self.tuner start];
+    @try {
+        if (self.isTunerRuning == NO) {
+            self.isTunerRuning = YES;
+            [self.tuner start];
+        }
+    } @catch (NSException *exception) {
+        NSLog(@"----- exception --- %@", exception);
+    } @finally {
+        
     }
 }
 

+ 19 - 3
KulexiuForTeacher/KulexiuForTeacher/Module/Widget/Controller/ToneTuningViewController.m

@@ -66,6 +66,11 @@
     [self configDefault];
     [self configUI];
     [self setupAudioSession];
+    [self addNotification];
+}
+
+- (void)addNotification {
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopPlayBeat) name:ERROR_NOTIFICATION object:nil];
 }
 
 - (void)configDefault {
@@ -152,9 +157,15 @@
 }
 
 - (void)startTuner {
-    if (self.isRuning == NO) {
-        self.isRuning = YES;
-        [self.tuner start];
+    @try {
+        if (self.isRuning == NO) {
+            self.isRuning = YES;
+            [self.tuner start];
+        }
+    } @catch (NSException *exception) {
+        NSLog(@"--exception -- %@",exception);
+    } @finally {
+        
     }
 }
 
@@ -547,6 +558,11 @@
     _isTransfer = isTransfer;
     self.bodyView.isTransfer = isTransfer;
 }
+
+- (void)dealloc {
+    [[NSNotificationCenter defaultCenter] removeObserver:self];
+    NSLog(@"----- toneturning dealloc");
+}
 /*
 #pragma mark - Navigation
 

+ 7 - 0
KulexiuForTeacher/KulexiuForTeacher/Module/Widget/Controller/WidgetViewController.m

@@ -42,6 +42,7 @@
     // Do any additional setup after loading the view.
     self.ks_prefersNavigationBarHidden = YES;
     [self configUI];
+    [self addNotification];
 }
 
 - (void)backAction {
@@ -60,6 +61,10 @@
     [self changeRhythmDisplay];
 }
 
+- (void)addNotification {
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopBeat) name:ERROR_NOTIFICATION object:nil];
+}
+
 - (void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];
     [self setDefaultConfig];
@@ -250,9 +255,11 @@
 }
 
 - (void)dealloc {
+    [[NSNotificationCenter defaultCenter] removeObserver:self];
     NSLog(@"----dealloc ");
 }
 
+
 /*
 #pragma mark - Navigation
 

+ 57 - 30
KulexiuForTeacher/KulexiuForTeacher/Module/Widget/Model/KSMetronomePlayer.m

@@ -63,34 +63,25 @@
         if (self.delegate && [self.delegate respondsToSelector:@selector(metronomePlayInterruption)]) {
             [self.delegate metronomePlayInterruption];
         }
-    }else{
+    }
+    else {
         AVAudioSessionInterruptionOptions options = [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
         if (options == AVAudioSessionInterruptionOptionShouldResume) {
             //Handle Resume
             if (self.delegate && [self.delegate respondsToSelector:@selector(resumeAudioSession)]) {
                 [self.delegate resumeAudioSession];
             }
+            [[AVAudioSession sharedInstance] setActive:YES error:nil];
+            [self startEngineWithErrorReport:NO];
+
         }
     }
 }
 
-- (void)configEngine {
-    [self setupAudioSession];
-    NSString *tickPath = [[NSBundle mainBundle]pathForResource:@"tick" ofType:@"wav"];
-    NSURL *tickUrl = [NSURL fileURLWithPath:tickPath];
-    self.audioDIFile = [[AVAudioFile alloc] initForReading:tickUrl error:nil];
-    
-    NSString *tockPath = [[NSBundle mainBundle]pathForResource:@"tock" ofType:@"wav"];
-    NSURL *tockUrl = [NSURL fileURLWithPath:tockPath];
-    self.audioDongFile = [[AVAudioFile alloc] initForReading:tockUrl error:nil];
-    self.playerNode = [[AVAudioPlayerNode alloc] init];
-    self.audioEngine = [[AVAudioEngine alloc] init];
-
-    [self.audioEngine attachNode:self.playerNode];
-    [self.audioEngine connect:self.playerNode to:self.audioEngine.mainMixerNode format:self.audioDIFile.processingFormat];
+- (void)startEngineWithErrorReport:(BOOL)needReport {
     NSError *error = nil;
     [self.audioEngine startAndReturnError:&error];
-    if (error) {
+    if (error && needReport) {
 #pragma mark ----- 错误上报
         NSLog(@"-- error desc - %@", error.description);
         NSMutableDictionary *parm = [NSMutableDictionary dictionary];
@@ -113,6 +104,30 @@
     self.audioEngine.autoShutdownEnabled = YES;
 }
 
+- (void)configEngine {
+    [self setupAudioSession];
+    NSString *tickPath = [[NSBundle mainBundle]pathForResource:@"tick" ofType:@"wav"];
+    NSURL *tickUrl = [NSURL fileURLWithPath:tickPath];
+    self.audioDIFile = [[AVAudioFile alloc] initForReading:tickUrl error:nil];
+    
+    NSString *tockPath = [[NSBundle mainBundle]pathForResource:@"tock" ofType:@"wav"];
+    NSURL *tockUrl = [NSURL fileURLWithPath:tockPath];
+    self.audioDongFile = [[AVAudioFile alloc] initForReading:tockUrl error:nil];
+    self.playerNode = [[AVAudioPlayerNode alloc] init];
+    self.audioEngine = [[AVAudioEngine alloc] init];
+
+    [self.audioEngine attachNode:self.playerNode];
+    @try {
+        [self.audioEngine connect:self.playerNode to:self.audioEngine.mainMixerNode format:self.audioDIFile.processingFormat];
+
+    } @catch (NSException *exception) {
+        NSLog(@"--exception -- %@", exception);
+    } @finally {
+        
+    }
+    [self startEngineWithErrorReport:YES];
+}
+
 
 - (CGFloat)getRate:(KSWidgeMetronomeType)metronomeType {
     switch (metronomeType) {
@@ -243,30 +258,42 @@
 
 - (void)playAction:(int)bpm metronomeTyle:(KSWidgeMetronomeType)type {
     if (self.audioEngine.isRunning == NO) {
-        [self.audioEngine startAndReturnError:nil];
+        [self startEngineWithErrorReport:NO];
     }
-    AVAudioPCMBuffer *buffer = [self generateBuffer:bpm metronomeType:type];
-    if (self.playerNode.isPlaying) {
-        [self.playerNode stop];
+    if (self.audioEngine.isRunning) {
+        AVAudioPCMBuffer *buffer = [self generateBuffer:bpm metronomeType:type];
+        if (self.playerNode.isPlaying) {
+            [self.playerNode stop];
+        }
+        [self.playerNode scheduleBuffer:buffer atTime:nil options:AVAudioPlayerNodeBufferLoops completionHandler:nil];
+        [self.playerNode play];
+    }
+    else {
+        if (self.delegate && [self.delegate respondsToSelector:@selector(metronomePlayInterruption)]) {
+            [self.delegate metronomePlayInterruption];
+        }
     }
-    [self.playerNode play];
-    
-    [self.playerNode scheduleBuffer:buffer atTime:nil options:AVAudioPlayerNodeBufferLoops completionHandler:nil];
 }
 
 
 - (void)playRhythmAction:(int)speed rhythmType:(RHYTHM_TYPE)type beatNumber:(NSInteger)beatNumber {
     self.beatNumber = beatNumber;
     if (self.audioEngine.isRunning == NO) {
-        [self.audioEngine startAndReturnError:nil];
+        [self startEngineWithErrorReport:NO];
+    }
+    if (self.audioEngine.isRunning) {
+        AVAudioPCMBuffer *buffer = [self generateThythmBuffer:speed rhythmType:type];
+        if (self.playerNode.isPlaying) {
+            [self.playerNode stop];
+        }
+        [self.playerNode scheduleBuffer:buffer atTime:nil options:AVAudioPlayerNodeBufferLoops completionHandler:nil];
+        [self.playerNode play];
     }
-    AVAudioPCMBuffer *buffer = [self generateThythmBuffer:speed rhythmType:type];
-    if (self.playerNode.isPlaying) {
-        [self.playerNode stop];
+    else {
+        if (self.delegate && [self.delegate respondsToSelector:@selector(metronomePlayInterruption)]) {
+            [self.delegate metronomePlayInterruption];
+        }
     }
-    [self.playerNode play];
-    
-    [self.playerNode scheduleBuffer:buffer atTime:nil options:AVAudioPlayerNodeBufferLoops completionHandler:nil];
 }
 
 - (AVAudioPCMBuffer *)generateThythmBuffer:(int)speed rhythmType:(RHYTHM_TYPE)type {

+ 2 - 3
KulexiuForTeacher/KulexiuForTeacher/Module/Widget/Model/MetronomeManager.h

@@ -11,8 +11,6 @@
 
 @protocol MetronomeManagerDelegate <NSObject>
 
-- (void)playInterruption;
-
 @optional;
 
 - (void)updateSpotHightState:(int)currentNo;
@@ -23,12 +21,13 @@ typedef void(^BeatChangeCallback)(NSInteger beatNumber);
 
 typedef void(^RhythmChangeCallback)(RHYTHM_TYPE thythmType);
 
+#define ERROR_NOTIFICATION (@"KSMetronomePlayerStop")
+
 #define METRONOME_MANAGER ([MetronomeManager shareInstance])
 
 NS_ASSUME_NONNULL_BEGIN
 
 
-
 @interface MetronomeManager : NSObject
 
 @property (nonatomic, weak) id <MetronomeManagerDelegate>delegate;

+ 6 - 0
KulexiuForTeacher/KulexiuForTeacher/Module/Widget/Model/MetronomeManager.m

@@ -64,6 +64,12 @@
     if (self.isPlaying) {
         [self stopPlay];
     }
+    // 播放打断处理
+    [self notiferInterruption];
+}
+
+- (void)notiferInterruption {
+    [[NSNotificationCenter defaultCenter] postNotificationName:ERROR_NOTIFICATION object:nil];
 }
 
 -  (void)startPlay {