|
@@ -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 {
|