gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:52:39 29 4
gpt4 key购买 nike

我是 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的委托(delegate)方法audioPlayerDidFinishPlaying:

例如:

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

关于objective-c - 如何播放音频先完成音频文件然后再开始播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11559044/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com