Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop AVPlayer and restart MP3 file again

Tags:

I want to start, pause and stop (same as restart) my mp3 File and I'm using AVPlayer. I get the files from a server.

To start the song I do:

[self.player start];

To pause I do:

[self.player pause];

but when I want to stop the song and reload it, so that the song starts from the beginning when the User clicks on the "start button" next time, I have no idea what to do.

I tried something like this:

[self.player pause];
self.player = nil;

But then the player is nil of course and I can't restart the file again without a new initialization. Any ideas how to stop it?

like image 267
Davis Avatar asked Sep 16 '14 13:09

Davis


2 Answers

Stop the video using [player pause]

Then use this code when you start the video.

[player seekToTime:kCMTimeZero];
[player play];

I like this better than the comment that solved the OP it b/c it eliminates a warning and uses a constant.

like image 189
Cbas Avatar answered Sep 28 '22 20:09

Cbas


Best solution to do this in Swift < 4:

player.seek(to: kCMTimeZero)
player.play()

Swift >= 4:

player.seek(to: .zero)
player.play()
like image 33
fabioalmeida Avatar answered Sep 28 '22 22:09

fabioalmeida