views:

77

answers:

1

I am working on an interactive children's book for the iPad that has a "read to me" option.

For each page (that has an index), theres an audio clip that produces the "read to me" feature. The feature works well, except for the fact that when I turn the page, the previous pages audio still plays, even when the new audio starts, here's my example:

- (void) didTurnToPageAtIndex:(NSUInteger)index {
    //NSLog(@"Did Turn to Page Index: %i",index);
    if ([delegate respondsToSelector:@selector(leavesView:didTurnToPageAtIndex:)])
    [delegate leavesView:self didTurnToPageAtIndex:index];

    if([[NSUserDefaults standardUserDefaults] boolForKey:@"kReadToMe"] == YES){
        NSString* filename = [voices objectAtIndex:index];
        NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"m4v"];  
        NSLog(@"File: %@, Index: %i",path,index);

        //Create new audio for next page
        AVAudioPlayer * newAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil]; 
        rtmAudio = newAudio; // automatically retain audio and dealloc old file if new file is loaded
        //[newAudio release];
        [rtmAudio play];
    }
}

Say for instance, I turn to page 3 before the audio for page 2 stops playing, both clips play over eachother, which will annoy the sh*t out of kids, I know it does me.

I've tried placing [rtmAudio stop] before I allocate the new file, but that doesnt seem to work. I need a way to kill the prevous audio clip before starting the new clip.

A: 

I would suggest that you have one instance of the audio player in your whole application. Then you can check if it playing, if so stop it and then move on.

you are creating a new player in this method before stopping the old.... I believe

Aaron Saunders
but even if I place a [rtmAudio stop]; before I create the new instance, it doesnt work.
AWright4911
would need to see more code to provide any more assistence
Aaron Saunders
thats pretty much all there is to it. I declare it in the header and use <AVAudioPlayerDelegate>.
AWright4911
for some reason, now it its working. I have a "- (void) didTurnToPageAtIndex:(NSUInteger)index" so I placed a [self.rtmAudio stop]; there, after setting delegate:self.
AWright4911