Hello
I decided to try to implement a metronome on my own using NSTimer to learn about NSTimer and playing sounds in the SDK.
So after looking Apple's thread implementation I did this but it doesnt seem to work. I then tested the play sound function by having a button work on it and noticed that I couldnt get the sound to be interrupted to start the sound from the beginning again which effectively creates a maximum bpm based on the length of my tick and tock. Im not sure how to fix either of these problems but I believe the timer might not be calling the playSound function but Im not sure why.
Any help is appreciated and thanked for in advance.
I decided to try to implement a metronome on my own using NSTimer to learn about NSTimer and playing sounds in the SDK.
So after looking Apple's thread implementation I did this but it doesnt seem to work. I then tested the play sound function by having a button work on it and noticed that I couldnt get the sound to be interrupted to start the sound from the beginning again which effectively creates a maximum bpm based on the length of my tick and tock. Im not sure how to fix either of these problems but I believe the timer might not be calling the playSound function but Im not sure why.
Code:
- (void)playSound {
AVAudioPlayer *currentPlayer = tickPlayer;
NSUInteger timeSignature = kDefaultTimeSignature;
if (beatNumber == 1) {
currentPlayer = tockPlayer;
}
else if (beatNumber == timeSignature) {
beatNumber = 0;
}
[currentPlayer play];
beatNumber++;
}
- (IBAction)startstopSession:(id)sender{
if (playStatus){playStatus = FALSE;}
if (!playStatus){playStatus = TRUE;}
// Number of Milliseconds between beats
bpm = kDefaultBPM;
beatLength = 60000/bpm;
if(playStatus){
MasterClock =
[NSTimer scheduledTimerWithTimeInterval:(beatLength) target:self
selector:@selector(playSound) userInfo:nil repeats:YES];
}
if (!playStatus){
[MasterClock invalidate];}
}
/*
I previously initiated the AVAudioPlayer doing*/
NSBundle *mainBundle = [NSBundle mainBundle];
NSError *error;
NSURL *tickURL = [NSURL fileURLWithPath:[mainBundle pathForResource:@"tick" ofType:@"caf"]];
tickPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:tickURL error:&error];
if (!tickPlayer) {
NSLog(@"no tickPlayer: %@", [error localizedDescription]);
}
[tickPlayer prepareToPlay];
Any help is appreciated and thanked for in advance.