Xamarin Forms Android 视频 (VideoView) 在页面加载或后退键 + 循环时出现黑色闪烁 [英] Xamarin Forms Android video (VideoView) black flash on page load or back key + looping

查看:15
本文介绍了Xamarin Forms Android 视频 (VideoView) 在页面加载或后退键 + 循环时出现黑色闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照本指南/从此处复制示例,以 Xamarin 形式为 iOS 和 Android 实现视频播放器:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/视频播放器/

I followed this guide/copied the sample from here for implementing a Video Player for both iOS and Android in Xamarin forms: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/video-player/

视频播放器可以工作,但在页面加载时它会很快闪烁黑色.同样,当返回时,它会在上一页前面短暂地闪烁视频播放器.

The videoplayer works but on page load it will shortly flash black. Also when going back it would shortly flash the videoplayer in front of the previous page.

你可以试试:videoView.setZOrderOnTop(true); 但这给我带来了其他页面的问题,即使它不在 xaml 中,视频仍然存在.

You could try: videoView.setZOrderOnTop(true); but this gave me problems with other pages where the video would still be present even though it was not in the xaml.

推荐答案

您可以执行以下所有操作或使用新的 Xamarin.Forms MediaElement 开箱即用.请记住,MediaElement 尚不能与 StackLayout 一起使用.

You can do everything below or use the new Xamarin.Forms MediaElement out of the box. Keep in mind that the MediaElement does not work yet with a StackLayout.

Mediaelement 在 stacklayout #2780 中不起作用

[Bug] MediaElement 不喜欢在 StackLayout 中(特别是在 Android 上)

为了解决此问题并在 Xamarin Forms 中集成循环(假设您遵循原始帖子中的指南):

In order to fix this and integrate looping in Xamarin Forms (assuming you followed the guide in the original post):

将下面的代码添加到 OnElementChanged() 函数的末尾

Add the code below to the end of the OnElementChanged() function

videoView.SetOnPreparedListener(new VideoLoop(videoView));

创建videoloop类(我也想循环)

Create the videoloop class (I also wanted to loop)



using Android.Graphics;
using Android.Media;
using Android.Views;
using Android.Widget;
using YOURPROJECTHERE.Droid.FormsVideoLibrary;
using Java.Lang;
using System.Threading.Tasks;

namespace FormsVideoLibrary.Droid
{
    public class VideoLoop : Java.Lang.Object, Android.Media.MediaPlayer.IOnPreparedListener
    {
        VideoView Video;
        public VideoLoop(VideoView video)
        {
            Video = video;
            Video.SetBackgroundColor(Android.Graphics.Color.White);
        }

        public void OnPrepared(MediaPlayer mp)
        {
            mp.SetOnInfoListener(new OnInfo(Video));

            //Remove or comment the line below if you don't want to loop
            mp.Looping = true;
            mp.Start();
        }
    }
}

现在我们在推入第一帧时使背景颜色透明(不是在 OnPrepared 处,因为此时它仍在缓冲):

Now we make the background color transparent when the first frame is pushed (not at OnPrepared because it is still buffering at that point):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Media;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace YOURPOJECTNAMESPACE.Droid.FormsVideoLibrary
{
    class OnInfo : Java.Lang.Object, Android.Media.MediaPlayer.IOnInfoListener
    {
        VideoView Video;
        public OnInfo(VideoView video)
        {
            Video = video;
        }
        
        bool MediaPlayer.IOnInfoListener.OnInfo(MediaPlayer mp, MediaInfo what, int extra)
        {

            if (what == MediaInfo.VideoRenderingStart)
            {
                // video started; hide the placeholder.
                Video.SetBackgroundColor(Android.Graphics.Color.Transparent);
                return true;
            }
            return false;
        }
    }
}

我还在 VideoRenderer 类的 OnStopRequested 函数中添加了以下内容.

I also added the below in the OnStopRequested function in the VideoRenderer class.

        void OnStopRequested(object sender, EventArgs args)
        {
            videoView.StopPlayback();
            videoView.SetBackgroundColor(Android.Graphics.Color.White);
        }

然后最后回到表单公共项目 xaml.cs 中,在每个带有视频的页面上添加以下内容:

And then finally back in the forms common project xaml.cs add the below on each page with a video:

 protected override void OnAppearing()
        {
            xNAMEOFYOURVIDEO.Play();           
            xNAMEOFYOURVIDEO.Source = new ResourceVideoSource
                {
                    Path = "yourfile.mp4"
                };
        }

  protected override void OnDisappearing()
        {
            xNAMEOFYOURVIDEO.Stop();
        }

这篇关于Xamarin Forms Android 视频 (VideoView) 在页面加载或后退键 + 循环时出现黑色闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆