嗨,在我的应用程序中,当用户按下按钮时音频正在播放但现在我有两个音频按钮,如audio1和audio2,现在我想在完成audio1完成后通过单击相同的按钮播放第二个音频。
- (IBAction)btn1:(id)sender {
NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"audio" ofType:@"mp3" ];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[self.audioPlayer play];
}
上面的代码我用来播放一个音频现在我想在第一个音频完成后播放第二个音频请告诉我如何制作它。
感谢。
答案 0 :(得分:0)
试试这个:
你必须添加一个名为' firstAudioPlayed' 的bool变量,启动变量应为 NO或FALSE 。
- (IBAction)btn1:(id)sender {
if (![self.audioPlayer isPlaying]) {
NSString *audioPath;
if (self.firstAudioPlayed) {
audioPath = [[NSBundle mainBundle] pathForResource:@"audio2" ofType:@"mp3" ];
} else {
audioPath = [[NSBundle mainBundle] pathForResource:@"audio1" ofType:@"mp3" ];
}
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[self.audioPlayer play];
self.firstAudioPlayed = !self.firstAudioPlayed;
}
}
我不确定它是否像这样工作,否则只是发表评论...
代码的解释:
首先检查播放器是否已播放歌曲,如果他不是,你检查第一个音频是否已播放(self.firstAudioPlayed),你可以给出加载音频文件的正确路径。现在你播放这个音频文件。
(对不起我的语法和英语,欢迎更正)
答案 1 :(得分:0)
你可能应该使用玩家的代表。设置audioPlayer.delegate=self;
并实施audioPlayerDidFinishPlaying:successfully:
方法,以查看播放器何时播放完毕。
然后你可以在那里做任何你想做的事情,例如开始第二个音频。