【问题标题】:willEndFullScreenPresentationWithAnimationCoordinator not fire when video is closed视频关闭时不会触发 willEndFullScreenPresentationWithAnimationCoordinator
【发布时间】:2021-03-09 23:13:31
【问题描述】:

您好,我有一个基于 Xamarin MvvmCross 框架的应用程序,它使用来自 iOS 的 NSOject 库的 AVPlayerItem。每当视频播放关闭时,由于观察者通知动画协调器的全屏演示结束时,应弹出测验。最常见的是,函数 WillEndFullScreenPresentation 被触发。但有时当我在暂停期间滑动过快或关闭时,会在调用视频播放之前触发此函数。如何防止这种情况发生?

private void SetVideoPlayer(VideoAsset video)
        {

            var player = new AVPlayer(new NSUrl(video.VideoUrl));
            ViewModel.WatchedVideoAssetCommand.Execute(video);
            aVPlayerViewController = new AVPlayerViewController();
            aVPlayerViewController.Player = player;
            aVPlayerViewController.Delegate = this;
            this.PresentViewController(aVPlayerViewController, true, () =>
            {
                aVPlayerViewController.Player?.Play();
            });
            //NotificationCenter.default.addObserver(self, selector: Selector(("playerDidFinishPlaying:")),
            //name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: fourVideoPlayer.player.currentItem)
            
            NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification, (notify) => {
                notify.Dispose();
                aVPlayerViewController.DismissModalViewController(true);
            });

            NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.ItemFailedToPlayToEndTimeNotification, (notify) =>
            {
                notify.Dispose();
                aVPlayerViewController.DismissModalViewController(true);
            });
        }

        [Export("playerViewController:willEndFullScreenPresentationWithAnimationCoordinator:")]
        public void WillEndFullScreenPresentation(AVPlayerViewController playerViewController, IUIViewControllerTransitionCoordinator coordinator)
        {
            ViewModel.StartQuizForVideoAssetCommand.Execute(ViewModel.PlayingAsset);
        }

【问题讨论】:

    标签: ios xamarin video-streaming mvvmcross nsobject


    【解决方案1】:

    AVPlayer有一个AVPlayerTimeControlStatus属性:

    但有时当我在暂停期间滑动过快或关闭时,会在调用视频播放之前触发此函数。

    您可以在WillEndFullScreenPresentation方法中查看此状态以确定是否需要退出全屏。

    示例代码如下:

    [Export("playerViewController:willEndFullScreenPresentationWithAnimationCoordinator:")]
    public void WillEndFullScreenPresentation(AVPlayerViewController playerViewController, IUIViewControllerTransitionCoordinator coordinator)
    {
        if (aVPlayerViewController.Player.TimeControlStatus != AVFoundation.AVPlayerTimeControlStatus.Paused || aVPlayerViewController.Player.TimeControlStatus != AVFoundation.AVPlayerTimeControlStatus.WaitingToPlayAtSpecifiedRate)
        {
            ViewModel.StartQuizForVideoAssetCommand.Execute(ViewModel.PlayingAsset);
        }
    
    }
    

    【讨论】:

    • 应用了这个修复,没有发现太大的区别,但是这个bug很少产生。事实上,重现这个错误非常困难。
    • @TPHo Okey,如果回复有帮助,请不要忘记接受它作为答案(点击该答案左上角的✔)并投票,它将帮助其他人有类似的问题。
    猜你喜欢
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多