c17r
c17r

Reputation: 153

AVAudioPlayer works once

Trying to create a playlist of music files that are NOT part of the user's iphone library so I'm using AVAudioPlayer and creating the playlist functionality myself. It works on the first pass (meaning the first song is played). When the first song finishes and it goes to play the 2nd AVAudioPlayer crashes in prepareToPlay.

- (void) create {
    Song *song;

    NSArray *parts;
    NSString *path;
    NSString *name;  
    NSError *err;

    if (currentIndex > [queue count])
        currentIndex = 0;
    if (currentIndex < 0)
        currentIndex = ([queue count] - 1);

    song = (Song *) [queue objectAtIndex:currentIndex];
    name = song.fileName;
    parts = [name componentsSeparatedByString:@"."];

    path = [[NSBundle mainBundle] pathForResource:[parts objectAtIndex:0] ofType:[parts objectAtIndex:1]];
    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];

    [[AVAudioSession sharedInstance] setDelegate: self];
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &err];
    [[AVAudioSession sharedInstance] setActive: YES error: &err];

    appPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: &err];

    [url release];
    [parts release];

    [appPlayer prepareToPlay];
    [appPlayer setVolume: 1.0];
    [appPlayer setDelegate: self];
}

- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL) flag { 
    [appPlayer stop];
    [appPlayer release];
    appPlayer = nil;

    [self next];
}

The call to [self next] just increases currentIndex and calls create again.

The only other thing (I know, the infamous "only other thing" statement) is that this is all going on inside a Singleton. There's lots of things that could cause the music to start and stop playing, change the queue, etc so I thought it would be best to wrap it all up in one spot.

Any thoughts?

Upvotes: 1

Views: 1857

Answers (2)

Rostyslav Druzhchenko
Rostyslav Druzhchenko

Reputation: 3773

One of the reason of having exception in prepareToPlay method is enabled "All Exceptions" breakpoint. Go to Breakpoint Navigator and disable it.

Upvotes: 1

c17r
c17r

Reputation: 153

I was able to track this down myself. I re-wrote the whole section to have multiple AVAudioPlayer objects but still had issues. Turns out the [parts release] line was the issue, it was causing an over-release situation in the autorelease routine.

Upvotes: 1

Related Questions