IOS: AVAudioPlayer stops and plays

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 the 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 does not work.

 [change stop]; [change prepareToPlay]; [change play]; 

What can I do?

+4
source share
3 answers

How about this:

 [change pause]; change.currentTime = 0.0; [change play]; 
+12
source

The documentation of the -stop method says:

The stop method does not reset the value of the current Time 0 property. In other words, if you call stop during playback and then call playback, playback resumes at the point where it stopped.

So you should use the currentTime property and set it to 0 :

 [change pause]; [change setCurrentTime:0]; [change play]; 
+1
source

Did you try to initialize again? I mean first [change release] and ran it.

0
source

Source: https://habr.com/ru/post/1403264/


All Articles