开发者

iOS AVAudioPlayer multiple instances, multiple sounds at once

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 
{
    if ([delegate respondsToSel开发者_如何学Goector:@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.


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


As AlexChaffee mentioned the Apple docs, "Play multiple sounds simultaneously, one sound per audio player, with precise synchronization". It seems preferable to use multipe instances across the app with NSNotificationCenter's notifications.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜