Hello. I have a problem with a music application I am working on enhancing by giving it the ability to adjust the timing of sounds which are repeated when a loop BOOL is true. I have implemented an NSTimer, which it is recognizing, but I am having trouble with the selector. After the given time, it just crashes instead of replaying the sound. My code looks something like this:
Code:
-(IBAction) playSound: (id) sender
{
NSString *pathsoundFile;
float nVolume;
switch ([sender tag])
{
case 1:
pathsoundFile = [[NSBundle mainBundle] pathForResource:@"soundA" ofType:@"wav"];
break;
case 2:
pathsoundFile = [[NSBundle mainBundle] pathForResource:@"soundB" ofType:@"wav"];
break;
if (bDoRecord)
{
//only allow up to 64 background sounds
if([loopedSounds count]<=64)
{
[loopedSounds addObject:[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathsoundFile] error:NULL]];
((AVAudioPlayer *)loopedSounds.lastObject).delegate=self;
((AVAudioPlayer *)loopedSounds.lastObject).volume = nVolume;
((AVAudioPlayer *)loopedSounds.lastObject).numberOfLoops = 0;
[((AVAudioPlayer *)loopedSounds.lastObject) prepareToPlay];
pauseTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(playSound:) userInfo:nil repeats:YES];
[((AVAudioPlayer *)loopedSounds.lastObject) play];
}
}
else
{
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathsoundFile] error:NULL];
sound.delegate = self;
sound.volume =nVolume;
[sound play];
}
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback
error: &setCategoryError];
if (setCategoryError)
{
nil;
}
}
I originally had the sound loop indefinitely and the .wav file is 6 seconds long. Thus, a six second loop. .. always... [yawn].
The problem is that I am not really a programmer. What am I missing? The app in its current state is called freesong and if you look at it you will notice the need to control the loop length! Help!