I have an if statement that is measuring the microphone detection and triggering an animation. The issue I am having is that the animation begins 1/2 seconds after the user has stopped blowing. This is the code I have:
@interface ViewController ()
@property (nonatomic, strong) SecondViewController *secondVC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryRecord error:nil];
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless],
AVFormatIDKey,
[NSNumber numberWithInt: 1],
AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax],
AVEncoderAudioQualityKey, nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(levelTimerCallBack:) userInfo:nil repeats:YES];
} else
NSLog(@"%@", [error description]);
}
- (IBAction)loadsecondVC:(id)sender {
_secondVC = [[SecondViewController alloc] init];
_secondVC.view.backgroundColor = [UIColor purpleColor];
[self presentViewController:_secondVC animated:YES completion:nil];
}
- (void)levelTimerCallBack:(NSTimer *)timer {
[recorder updateMeters];
const double ALPHA = 0.5;
double peakPowerForChannel = pow(10, (0.5 * [recorder peakPowerForChannel:0]));
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
if (lowPassResults > 0.5){
NSLog(@"Mic blow detected");
NSArray *animationArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"dandelion0001.png"],
[UIImage imageNamed:@"dandelion0002.png"],
[UIImage imageNamed:@"dandelion0003.png"],
[UIImage imageNamed:@"dandelion0004.png"],
[UIImage imageNamed:@"dandelion0005.png"],
[UIImage imageNamed:@"dandelion0006.png"],
[UIImage imageNamed:@"dandelion0007.png"],
[UIImage imageNamed:@"dandelion0008.png"],
[UIImage imageNamed:@"dandelion0009.png"],
[UIImage imageNamed:@"dandelion0010.png"],
[UIImage imageNamed:@"dandelion0011.png"],
[UIImage imageNamed:@"dandelion0012.png"],
[UIImage imageNamed:@"dandelion0013.png"],
[UIImage imageNamed:@"dandelion0014.png"],
[UIImage imageNamed:@"dandelion0015.png"],
[UIImage imageNamed:@"dandelion0016.png"],
[UIImage imageNamed:@"dandelion0017.png"],
[UIImage imageNamed:@"dandelion0018.png"],
[UIImage imageNamed:@"dandelion0019.png"],
[UIImage imageNamed:@"dandelion0020.png"],
nil];
UIImageView *animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
animationView.animationImages = animationArray;
animationView.animationRepeatCount = 1;
[animationView startAnimating];
[self.view addSubview:animationView];
}
else if (lowPassResults < 0.5) {
UIImageView *animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
[self.view addSubview:animationView];
[animationView stopAnimating];
}
}
@end
So the detection registers in the NSLog and when it stops, the animation plays. Do I have something in here that is delaying the animation? Or is an if statement the wrong thing to use here if I want it to happen at the same time. I have thought about using a 'while' statement but I got error messages when I did this.
I also need the animation to pause when the user stops blowing and proceed when they blow again so if anyone could help me out with the else statement I have at the bottom that would be really helpful as I know it is wrong.
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire