Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何播放音频先完成音频文件,然后再开始播放?_Objective C_Ios_Cocoa Touch_Avaudioplayer - Fatal编程技术网

Objective c 如何播放音频先完成音频文件,然后再开始播放?

Objective c 如何播放音频先完成音频文件,然后再开始播放?,objective-c,ios,cocoa-touch,avaudioplayer,Objective C,Ios,Cocoa Touch,Avaudioplayer,我是iPhone的初学者。我已经采取了两个音频文件,但如何播放第一次完成,然后下一次播放只有一种方法 这是我的代码是 -(IBAction)onclicksound { path=[[NSBundle mainBundle]pathForResource:@"Animalfile" ofType:@"plist"]; dict=[[NSDictionary alloc]initWithContentsOfFile:path]; NSError *error;

我是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之后如何播放给出任何建议和源代码。

您可以调用
avaudioplayer
的委托方法
AudioPlayerDifinishPlaying:

例如:

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

阅读常见问题。你应该接受有帮助的答案。如果你不这样做,人们就会停止帮助。