Detect user skipping to end of AVPlayer video

ε祈祈猫儿з 提交于 2019-12-12 02:40:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!