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?
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.
Best solution to do this in Swift < 4:
player.seek(to: kCMTimeZero)
player.play()
Swift >= 4:
player.seek(to: .zero)
player.play()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With