xcode IOS:AVAudioPlayer 停止播放
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9855334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOS: AVAudioPlayer stop and play
提问by cyclingIsBetter
I have this code:
我有这个代码:
NSString *path = [NSString stringWithFormat:@"%@/%@",
[[NSBundle mainBundle] resourcePath],
@"change.mp3"];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
change = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
[change prepareToPlay];
I write in this code in viewdidload; now with a button I start this sound, but if I press the same button, I want to stop this sound and restart the sound; I try this code inside IBAction but it don't work
我在 viewdidload 中写了这段代码;现在用一个按钮我开始这个声音,但是如果我按下同一个按钮,我想停止这个声音并重新开始这个声音;我在 IBAction 中尝试此代码但它不起作用
[change stop];
[change prepareToPlay];
[change play];
what can I do?
我能做什么?
回答by Till
How about this:
这个怎么样:
[change pause];
change.currentTime = 0.0;
[change play];
回答by sch
The documentationof the method -stop
says:
该方法的文档-stop
说:
The stop method does not reset the value of the currentTime property to 0. In other words, if you call stop during playback and then call play, playback resumes at the point where it left off.
stop 方法不会将 currentTime 属性的值重置为 0。换句话说,如果您在播放过程中调用 stop 然后调用 play,则播放会从停止的点恢复。
So you have to use the property currentTime
and set it to 0
:
因此,您必须使用该属性currentTime
并将其设置为0
:
[change pause];
[change setCurrentTime:0];
[change play];
回答by GeRyCh
Did you try to make initialization again? I mean first [change release]
and init it.
您是否尝试再次进行初始化?我的意思是首先[change release]
并初始化它。