28

I used the AVAudioPlayer to play a 10 sec wav file and it works fine. Now I what to stop the wav at the 4th sec and then play it again from the very 1st sec.

Here is the code I tried:

NSString *ahhhPath = [[NSBundle mainBundle] pathForResource:@"Ahhh" ofType:@"wav"];
AVAudioPlayer *ahhhhhSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:ahhhPath] error:NULL];

[ahhhhhSound stop];
[ahhhhhSound play];

What I get is, the wav stops at the 4th sec but when I run the [XXX play] again, the wav continues to play the 5th sec instead of playing from the beginning.

How could I get this done? Any help will be appreciated.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
supersurabbit
  • 441
  • 1
  • 8
  • 14
  • And for short sounds the apparent symptom is that the sound does not play the second time - calling stop and play just caused the sound to finish playing and it sounded like the second play was ignored. That is what was happening in my code, and the answer below fixed it. – Echelon Oct 22 '14 at 09:12

3 Answers3

63

Apple's AVAudioPlayer class reference says:

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.

So you should be able to restart it with:

[ahhhhhSound stop];
ahhhhhSound.currentTime = 0;
[ahhhhhSound play];
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • I've found a problem with this on devices iOS 4.3.3 and 4.3.5 at least. It works OK on iOS 5.0.1 devices. (iPhone 4 and 4S) after doing stop, then setting currentTime to 0, then play, play actually fails (returning NO). I'm pulling my hair here... – Jonny Feb 23 '12 at 07:28
  • @Jonny: Then ask a new question. This questions is 1.5 years old ;-) – DarkDust Feb 23 '12 at 07:48
  • No I found out a workaround and am on a run for your fame ;-) – Jonny Feb 23 '12 at 07:57
  • 1
    Don't call stop() call pause() instead, because stop() will unload and remove the hardware setup for the audio player, while pause() will just pause the sound but keep the hardware setup ready for playback = much faster = less lag. – StackUnderflow Feb 14 '17 at 00:32
6

So I had some troubles as you can see from my comment on DarkDust's answer. The problem only appeared on iOS 4.3.3/4.3.5, iPhone 4. Possibly earlier iOS versions as well, but not iOS 5.0.1. Somehow the AVAudioPlayer itself seemed to "break" when stopping it and the latter play would just fail unexplicably. Actually a "stop" would unload some stuff loaded on prepareToPlay, that is not unloaded if you just "pause" the player - according to the reference. So my solution would be using pause instead of stop - regarded the player is currently playing - otherwise there would be no reason to stop anything. Should theoretically be faster too. Solved it for me.

if (ahhhhhSound.playing) {
    [ahhhhhSound pause];
}
ahhhhhSound.currentTime = 0;
[ahhhhhSound play];

Now I can get on with life again.

Jonny
  • 15,955
  • 18
  • 111
  • 232
  • +1 to this solution. I was having problems with stop (sounds would break, only end parts would play of other sounds, etc... I have multiple AVAudioPlayer instances). Using pause and setting currentTime to 0 resolves all of my issues! – James Brooks Jul 09 '12 at 21:24
0

Swift:

mySound.audioPlayer?.stop()
mySound.audioPlayer?.currentTime = 0
mySound.audioPlayer?.play()
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168