user1516779
user1516779

Reputation: 35

how to play audio first finish audio file then next start to play?

I am beginner of iPhone. I have taken two audio file but how to play first finished then next play only one method...

this is my code is....

-(IBAction)onclicksound
{
   path=[[NSBundle mainBundle]pathForResource:@"Animalfile" ofType:@"plist"];
        dict=[[NSDictionary alloc]initWithContentsOfFile:path];
        NSError *error;
        animalaudio=[dict valueForKey:@"Animalsound"];

        NSLog(@"animalaudio:%@",animalaudio);
        NSLog(@"currentsound=%d",currentsound);
        selectanimalaudio=[animalaudio objectAtIndex:currentsound];

        NSString *soundpath=[[NSBundle mainBundle]pathForResource:selectanimalaudio ofType:@""];
        NSLog(@"selectaudio:%@",selectanimalaudio);
        AVAudioPlayer *audioplayer=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:soundpath]  error:&error];
            if (error)
            {
                NSLog(@"Error in audioPlayer: %@", 
                  [error localizedDescription]);
            } 
            else 
            {

                self.audiosound=audioplayer;
                [audioplayer release];
                //audiosound.numberOfLoops=currentsound;
                //[self.audiosound setDelegate:self];
                [audiosound play];
                NSLog(@"audioplayer:%@",audiosound);
            }


        NSError *err;
        animalsecaudio=[dict valueForKey:@"Birdsound"];
        NSLog(@"animalaudio:%@",animalsecaudio);
       // NSLog(@"currentsound=%d",currentsound);
        selectanimalsecaudio=[animalsecaudio objectAtIndex:currentsound];
          NSLog(@"selectanimalsecaudio:%@",selectanimalsecaudio);
         NSString *soundsecpath=[[NSBundle mainBundle]pathForResource:selectanimalsecaudio ofType:@""];
        AVAudioPlayer *audioplayerr=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:soundsecpath]  error:&err];
        if (err)
        {
            NSLog(@"Error in audioPlayer: %@", 
                  [err localizedDescription]);
        } 
        else 
        {

            self.animalsecsound=audioplayerr;
            [audioplayerr release];
            //audiosound.numberOfLoops=currentsound;
            [animalsecsound play];
            [animalsecsound prepareToPlay];
           // [self.animalsecsound setDelegate:self];
            NSLog(@"audioplayer:%@",animalsecsound);
        }
}

so, second avaudioplayer how to play after one finished avaudioplayer give any suggestion and source code..

Upvotes: 0

Views: 724

Answers (1)

Mick MacCallum
Mick MacCallum

Reputation: 130222

You can call AVAudioPlayer's delegate method audioPlayerDidFinishPlaying:

For example:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)sender successfully:(BOOL)success
{
    if (success) 
        {
            //code to play next sound
        }
}

Upvotes: 2

Related Questions