I've written a Xamarin.Forms iOS app Page
that users an AVPlayer
to play a video via a custom page renderer.
When the video finishes, or when the user scrubs to the end of the video (using controls created by AVPlayerViewController
), they should be sent to the next ContentPage
in the app.
I can track when the video 'naturally' finishes by observing the AVPlayerItem.DidPlayToEndTimeNotification
on the AVPlayerItem _playerItem
, like so:
videoEndNotificationToken = NSNotificationCenter.DefaultCenter.AddObserver(
AVPlayerItem.DidPlayToEndTimeNotification,
VideoDidFinishPlaying,
_playerItem);
I then push a new page on the navigation stack in VideoDidFinishPlaying
, and the user continues.
However, this doesn't work if the user scrubs to the end of the video using the default control bar.
How can I detect if the video finished via being scrubbed to the end by the user?
Using the AVPlayerViewController
and allowing the user to manually seek to the end will not fire DidPlayToEndTimeNotification
as the media asset did not actually play to the end 'normally'.
Here is what I did in a similar case:
TimeJumpedNotification
: didPlayToEndTimeNotification = NSNotificationCenter.DefaultCenter.AddObserver(
AVPlayerItem.DidPlayToEndTimeNotification,
videoFinished,
_playerItem);
timeJumpedNotification = NSNotificationCenter.DefaultCenter.AddObserver(
AVPlayerItem.TimeJumpedNotification,
videoFinished,
_playerItem);
public void videoFinished(NSNotification notify){
if (notify.Name == AVPlayerItem.TimeJumpedNotification) {
Console.WriteLine ("{0} : {1}", _playerItem.Duration, _player.CurrentTime);
if (Math.Abs(_playerItem.Duration.Seconds - _player.CurrentTime.Seconds) < 0.001) {
Console.WriteLine ("Seek to end by user");
}
} else if (notify.Name == AVPlayerItem.DidPlayToEndTimeNotification) {
Console.WriteLine ("Normal finish");
} else {
// PlaybackStalledNotification, ItemFailedToPlayToEndTimeErrorKey, etc...
Console.WriteLine (notify.Name);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.