0

我是 iPhone 的初学者。我已经拍摄了两个音频文件,但是如何先播放完成然后接下来只播放一种方法...

这是我的代码是....

-(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);
        }
}

所以,第二个avaudioplayer如何在完成avaudioplayer后播放任何建议和源代码..

4

1 回答 1

2

您可以调用AVAudioPlayer的委托方法audioPlayerDidFinishPlaying:

例如:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)sender successfully:(BOOL)success
{
    if (success) 
        {
            //code to play next sound
        }
}
于 2012-07-19T10:42:03.180 回答