AVAudioPlayer UIButton - 第二次触摸停止当前音频播放
我有一个 UIButton,触摸时会播放音频文件。目前,连续触摸按钮两次会播放音频文件两次,而不是停止播放。
我希望第二次单击停止音频文件并且不播放第二个实例。任何建议将不胜感激。谢谢
-(IBAction) buttonNoisePlay {
NSString *pathsoundFile = [[NSBundle mainBundle] pathForResource:@"noise" ofType:@"mp4"];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathsoundFile] error:NULL];
sound.delegate = self;
sound.numberOfLoops = -1;
sound.volume = 1;
if(sound == nil || sound.playing == NO) //if not playing
{
[sound play];
}
else
{
[sound stop];
}
}
I have a UIButton that when touched plays an audio file. At the moment touching the button twice in succession plays the audio file twice instead of stopping it.
I'm looking to have the second click stop the audio file and not play a second instance. any suggestions would be very much appreciated. thanks
-(IBAction) buttonNoisePlay {
NSString *pathsoundFile = [[NSBundle mainBundle] pathForResource:@"noise" ofType:@"mp4"];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathsoundFile] error:NULL];
sound.delegate = self;
sound.numberOfLoops = -1;
sound.volume = 1;
if(sound == nil || sound.playing == NO) //if not playing
{
[sound play];
}
else
{
[sound stop];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以这样做:
You could do something like this:
嗯,这就是我所做的&它只播放一次:
并且在标题中:
我可能还应该在初始化audioPlayer之前检查nil,所以它只完成一次......但现在 - 它有效。
hmm, this is what I do & it only plays once:
And in the header:
I should probably also be checking for nil before initializing the audioPlayer, so it's only done once... but for now - it works.