我正在尝试创建一个Xamarin Forms应用程序,该应用程序显示启动页面的全屏视频 . 加载完成后,启动页面将带您进入主屏幕 . 主屏幕在旋转木马中显示一系列视频 .

在Android上,旋转木马视频可以自行工作,但是当我添加启动画面视频时,它会显示在旋转木马上 . 目前我的应用程序中有一个名为VideoView的视图,它具有视频源的BindableProperty .

渲染是在下面的自定义渲染器中专门完成的 . 它使用适用于Android的原生VideoView和MediaPlayer控件 . 我已经在启动屏幕的OnDisappearing功能中尝试了各种dispose和release函数,但它们似乎都没有发布视频 .

[assembly: ExportRenderer(typeof(App.Renderers.VideoView), 
typeof(VideoViewRenderer))]
namespace App.Droid.Renderers
{
    public class VideoViewRenderer : 
    ViewRenderer<App.Renderers.VideoView, VideoView>, ISurfaceHolderCallback
    {
        private VideoView videoview;
        private MediaPlayer player;

        public void SurfaceChanged(ISurfaceHolder holder, global::Android.Graphics.Format format, int width, int height)
        {
        }

        public void SurfaceDestroyed(ISurfaceHolder holder)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<App.Renderers.VideoView> e)
        {
            base.OnElementChanged(e);

            e.NewElement.StopAction = () =>
            {
                player.Pause();
            };

            e.NewElement.StartAction = () =>
            {
                player.Start();
            };

            e.NewElement.DisposeAction = () => 
            {
                Control.StopPlayback();
                Control.Dispose();
                player.Stop();
                player.Reset();
                player.Release();
            };

            if (Control == null)
            {
                videoview = new VideoView(Context);
                base.SetNativeControl(videoview);
                Control.Holder.AddCallback(this);
                player = new MediaPlayer();
                play(e.NewElement.FileSource);
            }
        }

        void play(string fullPath)
        {
            AssetFileDescriptor afd = Forms.Context.Assets.OpenFd(fullPath);
            if (afd != null)
            {
                try
                {
                    player.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
                    player.Prepare();
                    player.Looping = true;
                    Control.Layout(0, 200, player.VideoHeight, player.VideoWidth);
                    player.SetVideoScalingMode(VideoScalingMode.ScaleToFit);
                }
                catch
                {
                }
            }
        }

        public void SurfaceCreated(ISurfaceHolder holder)
        {
            player.SetDisplay(holder);
        }
    }
}

VideoView类扩展了View,如下所示:

public class VideoView : View
{
    public Action StopAction;
    public Action StartAction;
    public Action DisposeAction;

    public VideoView()
    {
        FileSource = string.Empty;
    }

    public static readonly BindableProperty FileSourceProperty =
        BindableProperty.Create<VideoView, string>(
            p => p.FileSource, string.Empty);

    public string FileSource
    {
        get { return (string)GetValue(FileSourceProperty); }
        set { SetValue(FileSourceProperty, value); }
    }

    public void Stop()
    {
        if (StopAction != null)
            StopAction();
    }

    public void Dispose()
    {
        if (DisposeAction != null)
            DisposeAction();
    }

    public void Start()
    {
        if (StartAction != null)
            StartAction();
    }
}

最后使用视图如下:

<controls:VideoView Grid.Row="0" Grid.Column="0" x:Name="Video1" FileSource="big_buck_bunny.mp4" Margin="0,10,0,40" />