检测用户跳到 AVPlayer 视频的结尾

Detect user skipping to end of AVPlayer video

我写了一个 Xamarin.Forms iOS 应用程序 Page 用户 AVPlayer 通过自定义页面渲染器播放视频。

当视频结束时,或者当用户滑动到视频末尾时(使用 AVPlayerViewController 创建的控件),它们应该被发送到应用中的下一个 ContentPage

我可以通过观察 AVPlayerItem _playerItem 上的 AVPlayerItem.DidPlayToEndTimeNotification 来跟踪视频 'naturally' 何时结束,如下所示:

    videoEndNotificationToken = NSNotificationCenter.DefaultCenter.AddObserver(
        AVPlayerItem.DidPlayToEndTimeNotification,
        VideoDidFinishPlaying,
        _playerItem);

然后我在 VideoDidFinishPlaying 的导航堆栈上推送一个新页面,用户继续。

但是,如果用户使用默认控制栏滑动到视频末尾,这将不起作用。

如何检测视频是否已被用户拖到最后?

使用 AVPlayerViewController 并允许用户手动搜索到结尾将不会触发 DidPlayToEndTimeNotification 因为媒体资产实际上并未播放到结尾 'normally'.

这是我在类似案例中所做的:

添加一个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);
    }
}