gpt4 book ai didi

ios - 如何停止播放歌曲和播放新歌曲

转载 作者:行者123 更新时间:2023-12-03 02:10:02 31 4
gpt4 key购买 nike

我正在开发应用程序,但有一个问题,当我单击按钮时,选定的索引歌曲开始播放,但是我想停止播放这首歌曲,然后播放下一首我给出索引的歌曲,这两首歌曲都开始播放。表示上一个连续播放,下一个也播放。
请指导我如何停止上一首歌,然后下一首才开始播放。
代码在这里

  - (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];
}

最佳答案

试试这个:

   -(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];

}
}

关于ios - 如何停止播放歌曲和播放新歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24880726/

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