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

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

问题描述

我遵循了本指南/从此处复制了示例,用于以Xamarin形式为iOS和Android实施视频播放器: https://docs.microsoft.com/zh-cn/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中(假设您遵循上述指南):

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

将以下代码添加到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在页面加载或返回键+循环时会出现Android视频(VideoView)黑色闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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