Objective c 如何播放音频先完成音频文件,然后再开始播放?
我是iPhone的初学者。我已经采取了两个音频文件,但如何播放第一次完成,然后下一次播放只有一种方法 这是我的代码是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;
-(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
}
}
阅读常见问题。你应该接受有帮助的答案。如果你不这样做,人们就会停止帮助。