我正在开发应用程序,我有一个问题,当我点击按钮然后选择索引歌曲开始,但我想要停止这首歌并想要播放下一首我给的索引,然后两首歌曲开始播放。表示先前连续,下一个也表示。 请指导我如何停止上一首歌,然后才开始播放。 代码在这里
- (IBAction)play:(id)sender
{
[self.audioPlayer stop];
self.audioPlayer = nil;
self.audioPlayer.currentTime = 0;
if(self.audioPlayer.playing ==YES)
{
self.audioPlayer.delegate=nil;
[self.audioPlayer stop];
self. audioPlayer=nil;
NSLog(@"plat");
}
NSString *path=[[NSBundle mainBundle]pathForResource:sound.text ofType:@"mp3"];
self.audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:
[NSURL fileURLWithPath:path] error:NULL];
//self.audioPlayer = nil;
[self.audioPlayer play];
}
答案 0 :(得分:0)
确定使用AVQueuePlayer来按顺序播放多个项目。
NSString *secondPath = [[NSBundle mainBundle] pathForResource:@"s1" ofType:@"mp3"];
NSString *firstPath = [[NSBundle mainBundle] pathForResource:@"s2" ofType:@"mov"];
AVPlayerItem *firstVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:firstPath]];
AVPlayerItem *secondVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:secondPath]];
self.queuePlayer = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstVideoItem, secondVideoItem,nil]];
[self.queuePlayer play];
答案 1 :(得分:0)
试试这个:
-(void)playSound
{
NSString *path=[[NSBundle mainBundle]pathForResource:sound.text ofType:@"mp3"];
self.audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:
[NSURL fileURLWithPath:path] error:NULL];
[self.audioPlayer play];
}
- (IBAction)play:(id)sender
{
if (self.audioPlayer.playing == NO)
{
[self playSound];
}
else
if (self.audioPlayer.playing == YES) {
[self.audioPlayer stop];
[self playSound];
}
}