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



你好,我正在使用带有AvController的AVPlayer在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:UsersmhvdiDocumentsOpenSourceXamarinMediaManagerMediaManagerPlatformsApplePlayerAppleMediaPlayer.cs:225 
at MediaManager.Platforms.Apple.Player.AppleMediaPlayer.Stop () [0x0002d] in C:UsersmhvdiDocumentsOpenSourceXamarinMediaManagerMediaManagerPlatformsApplePlayerAppleMediaPlayer.cs:233 
at MediaManager.Platforms.Apple.Player.AppleMediaPlayer.DidFinishPlaying (Foundation.NSNotification obj) [0x000af] in C:UsersmhvdiDocumentsOpenSourceXamarinMediaManagerMediaManagerPlatformsApplePlayerAppleMediaPlayer.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 

我已经通过使用插件修复了这个问题。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)
{
}
}

}
}

最新更新