问题
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?
回答1:
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:
Add a TimeJumpedNotification
:
didPlayToEndTimeNotification = NSNotificationCenter.DefaultCenter.AddObserver(
AVPlayerItem.DidPlayToEndTimeNotification,
videoFinished,
_playerItem);
timeJumpedNotification = NSNotificationCenter.DefaultCenter.AddObserver(
AVPlayerItem.TimeJumpedNotification,
videoFinished,
_playerItem);
Test for the manual seek to end:
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);
}
}
来源:https://stackoverflow.com/questions/35017037/detect-user-skipping-to-end-of-avplayer-video