Xamarin Ios 视频播放完毕后应用程序崩溃 (AvPlayer)

Xamarin Ios App Crash Once Video Finish Playing (AvPlayer)

您好,我正在使用 AVPlayer 和 AvController 来播放 iOS 中的视频。视频播放成功,但一旦播放完毕,我的应用程序就会崩溃。

using AVFoundation;
using AVKit;
using TestMobile.iOS.PlatformSpecifics;
using UIKit;
using Xamarin.Forms;

[assembly: Dependency(typeof(LocalFileProvider))]
namespace TestMobile.iOS.PlatformSpecifics
{
    public class LocalFileProvider : UIViewController,ILocalFileProvider
    {
            private NSObject observer;
            AVPlayer player;
            AVPlayerViewController avpvc;
            UIViewController viewController ;
    
    
            public void PlayVideoFromLocalStorage(string filePath)
            {
                try
                {
                    observer = NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification, OnFinishedPlayingFromUrl);
                    viewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
                    while (viewController.PresentedViewController != null)
                    {
                        viewController = viewController.PresentedViewController;
                    }
                    var url = new NSUrl(filePath, false);
                    if (player == null)
                        player = new AVPlayer(url);
                    avpvc = new AVPlayerViewController();
                    avpvc.Player = player;
                    player.ActionAtItemEnd = AVPlayerActionAtItemEnd.Pause;
                    player.Play();
                    viewController.PresentViewController(avpvc, true, null);
                }
                catch (Exception ex)
                {
                }
            }
        
            private void OnFinishedPlayingFromUrl(NSNotification obj)
            {
                if (player != null)
                {
                    player.Pause();
                    player.ReplaceCurrentItemWithPlayerItem(null);
                    viewController.RemoveFromParentViewController();
                    viewController.DismissViewController(false, null);
                }
                NSNotificationCenter.DefaultCenter.RemoveObserver(observer);
            }
            
    }
}

我正在使用依赖服务调用 PlayVideoFromLocalStorage 方法。

 Xamarin.Forms.DependencyService.Get<ILocalFileProvider>().PlayVideoFromLocalStorage(filePath);

视频播放完毕后,我收到类似

的错误
{System.NullReferenceException: Object reference not set to an instance of an object
  at MediaManager.Platforms.Apple.Player.AppleMediaPlayer.SeekTo (System.TimeSpan position) [0x00080] in C:\Users\mhvdi\Documents\OpenSource\XamarinMediaManager\MediaManager\Platforms\Apple\Player\AppleMediaPlayer.cs:225 
  at MediaManager.Platforms.Apple.Player.AppleMediaPlayer.Stop () [0x0002d] in C:\Users\mhvdi\Documents\OpenSource\XamarinMediaManager\MediaManager\Platforms\Apple\Player\AppleMediaPlayer.cs:233 
  at MediaManager.Platforms.Apple.Player.AppleMediaPlayer.DidFinishPlaying (Foundation.NSNotification obj) [0x000af] in C:\Users\mhvdi\Documents\OpenSource\XamarinMediaManager\MediaManager\Platforms\Apple\Player\AppleMediaPlayer.cs:162 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021 
  at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.3.2/src/Xamarin.iOS/Foundation/NSAction.cs:178 
--- End of stack trace from previous location where exception was thrown ---

  at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.3.2/src/Xamarin.iOS/UIKit/UIApplication.cs:86 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.18.3.2/src/Xamarin.iOS/UIKit/UIApplication.cs:65 

我已经使用 Plugin.MediaManager(0.9.9) 插件解决了这个问题。

using AVFoundation;
using AVKit;
using TestMobile.iOS.PlatformSpecifics;
using UIKit;
using Xamarin.Forms;

[assembly: Dependency(typeof(LocalFileProvider))]
namespace TestMobile.iOS.PlatformSpecifics
{
    public class LocalFileProvider : ILocalFileProvider
    {
            private NSObject observer;
            AVPlayerViewController avpvc;
            UIViewController viewController ;
    
    
            public void PlayVideoFromLocalStorage(string filePath)
            {
                try
                {
                    viewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
                    while (viewController.PresentedViewController != null)
                    {
                        viewController = viewController.PresentedViewController;
                    }
                   IMediaItem item = new MediaItem();
                   item.FileName = Path.GetFileName(uri);
                   item.MediaUri = "file://" + uri;
                   item.MediaLocation = MediaLocation.FileSystem;
                   item.MediaType = MediaType.Video;
                   
                   VideoView videoView = new VideoView();
                   videoView.PlayerViewController = new AVPlayerViewController();
                   
                   CrossMediaManager.Current.MediaPlayer.VideoView = videoView;
                   CrossMediaManager.Current.MediaPlayer.ShowPlaybackControls = true;
                   Device.BeginInvokeOnMainThread(() =>
                   {
                       CrossMediaManager.Current.Play(item);
                   });
                   viewController.PresentViewController(videoView.PlayerViewController, true, null);
                }
                catch (Exception ex)
                {
                }
            }
            
    }
}