Stop AVPlayer and restart MP3 file again

Best solution to do this in Swift < 4:

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

Swift >= 4:

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

seek#to has a completion handler.

p?.seek(to: .zero)
p?.rate = 1

is not really correct. You can get audio jitter.

p?.seek(to: .zero) { [weak self] _ in
    self?.p?.rate = 1
}

is probably what you want.

If it is already playing, and you just do the seek and nothing else, again you could get the jitters.

You are probably best to

p?.rate = 0
p?.seek(to: .zero) { [weak self] _ in
    self?.p?.rate = 1
}

If it's a really important app, you'll also have to consider the error condition in the seek completion.


  • In Xamarin.ios

player.Seek(CoreMedia.CMTime.Zero);

player.Play();


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.