当前位置: 首页 > 知识库问答 >
问题:

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

燕禄
2023-03-14

你好,我正在使用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 

共有1个答案

昝阳嘉
2023-03-14

我已经通过使用插件解决了这个问题。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)
                {
                }
            }
            
    }
}
 类似资料:
  • 我正在用xamarin开发一个应用程序,用户可以用它来播放电影。我需要应用程序从内存流播放电影,而不是直接从url或文件。是否有一个播放器控件或库,我可以使用它来播放来自mmemory流的电影?

  • 我有一个<代码>材料数据包< /代码>,它选择了一系列数据,例如从<代码> 2021年1月1日到2021年1月31日< /代码>。 打开MaterialDatePicker时,会显示当前日期。当我尝试从2月滚动到1月时,一个月的最后一天是不可见的 要选择本月的最后一天,我应该向下滚动,但当我选择本月的最后一天时,应用程序崩溃,显示此错误: java.lang.NullPointerExcture:

  • 我上传了一个aab到play控制台,最后把它列在play商店里。这个应用程序是用Python和Kivy编写的,我能够通过首先运行Buildozer,然后通过Android Studio运行脚本,按照网络上的各种说明来生成aab。 我可以从aab生成的游戏控制台下载apk,它工作得很好。然而,当我从play store安装应用程序时,它一打开就关闭。然而,我没有得到真正的坠机报告。 这是我的第一个真

  • 我正在使用默认相机使用意图录制视频,但是,一旦视频录制完毕,预览屏幕就会显示。我没有创建预览屏幕。这是Android的默认屏幕。点击播放按钮,应用程序崩溃,没有任何错误。 以下是代码: logcat中没有显示错误。

  • 我的应用程序似乎总是在部署后崩溃。 2015-07-21T13:29:28.127452+00:00 Heroku[router]:at=error code=h10 desc=“应用程序崩溃”method=get path=“/api/v1/sessions”host=calm-hamlet-5165.herokuapp.com request_id=69711e3a-913e-45ed-b4b