首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >安卓-使用VideoView显示视频时出现黑屏

安卓-使用VideoView显示视频时出现黑屏
EN

Stack Overflow用户
提问于 2015-02-18 14:54:17
回答 9查看 16.2K关注 0票数 4

这是我的布局:

代码语言:javascript
运行
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

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

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

</LinearLayout>

这是我的活动代码:

代码语言:javascript
运行
复制
public class MainActivity extends ActionBarActivity implements OnPreparedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        VideoView mVideoView = (VideoView) findViewById(R.id.videoview);
        Uri uri = Uri.parse("android.resource://" + getPackageName()+"/raw/lst2");
        mVideoView.setVideoURI(uri);
        mVideoView.requestFocus();
        mVideoView.setZOrderOnTop(true); 
        mVideoView.start();

    }

     @Override
      public void onPrepared(MediaPlayer mp) {
        mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
          @Override
          public boolean onInfo(MediaPlayer mp, int what, int extra) {
              View placeholder = (View) findViewById(R.id.placeholder);
            if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)  {
              // video started; hide the placeholder.
              placeholder.setVisibility(View.GONE);
              return true;
            }
            return false;
          }
        });
     }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
    }

    public void surfaceCreated(SurfaceHolder holder) {
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
    }

}

它在安卓4.2上运行良好,但在安卓2.3上却无法正常工作。在android 2.3上,第一次打开它时,它可以找到,但当关闭应用程序并再次打开它时,会出现一个黑屏,如下所示:

大约一分钟后,它从黑屏变成白屏,但仍然没有播放任何东西。

你能帮我解决这个问题吗?

EN

回答 9

Stack Overflow用户

发布于 2016-07-11 17:13:06

回答得很晚。但它肯定对任何人都有帮助。

在start()之前设置videoView.setZOrderOnTop(true)

https://developer.android.com/reference/android/view/SurfaceView.html#setZOrderOnTop(boolean)

代码语言:javascript
运行
复制
    videoView.setVideoURI(Uri.parse(uriString));
    videoView.setZOrderOnTop(true);//this line solve the problem
    videoView.start();
票数 16
EN

Stack Overflow用户

发布于 2017-09-06 03:45:57

我通过切换VideoViewalpha解决了这个问题

代码语言:javascript
运行
复制
public class VideoPlayer extends VideoView {

    ....
public VideoPlayer(Context context) {
    super(context);
    init();
}

public void init() {
    setAlpha(0); // hides view 
    setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                @Override
                public boolean onInfo(MediaPlayer mp, int what, int extra) {
                    if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                        setAlpha(1); // shows view
                        return true;
                    }
                    return false;
                }
            });
        }
    });

我猜如果你有外部VideoView并且可以访问它,你也可以做同样的事情。

票数 5
EN

Stack Overflow用户

发布于 2020-03-28 06:40:02

这段代码适用于我,在android nougat上测试过的代码。在java文件中不需要额外的代码。

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_gravity="center"
    tools:context=".SecondaryDisplay"
    android:background="@color/black">

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28577704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档