Android VideoView 黑屏 [英] Android VideoView black screen

查看:89
本文介绍了Android VideoView 黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方法来在 start() 方法运行之前摆脱 VideoView 上令人讨厌的黑色初始屏幕.

I have been looking for a way to get rid of the nasty black initial screen on a VideoView before the start() method is run.

我已尝试在小部件上使用背景图像,但它根本无法按预期工作.我还尝试将视频中第一帧的图像放在 VideoView 之上,并在 start() 方法之后隐藏它.添加一个 onPrepared 侦听器以启动视频,然后隐藏图像.这有效,但在过渡中有一个可怕的闪烁,我不知道如何摆脱它.

I have tried with background image on the widget but it doesn't work as expected at all. I have also tried putting an image of the first frame in the video on top of the VideoView and hiding it after the start() method. Adding an onPrepared listener to start the video and then hide the image. This works but there is a horrible flicker in the transition and I don't know how to get rid of it.

添加 MediaController 根本没有效果.问题仍然存在(我仍然看到黑色闪烁),我根本不想让视频控件可见.我的代码如下所示:

Adding the MediaController had no effect at all. The problem persists (I still see the black flicker) and I don't want to have the video controls visible at all. My code looks like this:

    VideoView vSurface= (VideoView) findViewById(R.id.surfaceView1);
    vSurface.setVideoURI(Uri.parse("android.resource://com.mypackage/" + R.raw.video1));
    vSurface.setVisibility(View.VISIBLE);
    vSurface.setOnPreparedListener(this);
    vSurface.setDrawingCacheEnabled(true);
    vSurface.setOnErrorListener(this);

推荐答案

我遇到了同样的问题,我找到了解决方案.它有点hacky,但它可以解决问题.所以基本上你需要把你的 VideoView 放到一个 FrameLayout 中.在视频视图上,您​​需要添加另一个带有视频背景的 FrameLayout,当您的视频加载并准备好播放时,您需要隐藏占位符.

I got the same problem and i found a solution. Its a little bit hacky but it do the trick. So basically you need to put your VideoView into a FrameLayout. Over the videoview you need to add another FrameLayout with the background of your video and when your video is loaded and ready to play you hide the placeholder.

<FrameLayout
  android:id="@+id/frameLayout1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_gravity="center"
  android:layout_marginTop="50dip" >

  <VideoView
    android:id="@+id/geoloc_anim"
    android:layout_width="fill_parent"
    android:layout_height="172dip" android:layout_gravity="top|center" android:visibility="visible"/>

  <FrameLayout
      android:id="@+id/placeholder"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" android:background="@drawable/fondvert_anim">
  </FrameLayout>

在您的活动中,您需要实现 OnPreparedListener 并添加此

In your activity you need to implements OnPreparedListener and add this

//Called when the video is ready to play
public void onPrepared(MediaPlayer mp) {

    View placeholder = (View) findViewById(R.id.placeholder);

    placeholder.setVisibility(View.GONE);
}

所以当视频准备好时,我们隐藏占位符,并避免黑色闪烁屏幕.

So when the video is ready we hide our placeholder and that trick avoid the black flicker screen.

希望这对某人有所帮助.

Hope this help someone.

这篇关于Android VideoView 黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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