繁体   English   中英

使用 VideoView 显示视频时的 android-黑屏

[英]android-black screen on displaying video by using VideoView

这是我的布局:

<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>

这是我的活动代码:

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) {
    }

}

它在 android 4.2 上工作正常,但在 android 2.3 上不能正常工作。 在 android 2.3 上,它第一次打开它可以正常工作,但是当关闭应用程序并再次打开它时,出现黑屏,如下所示:在此处输入图片说明

大约一分钟后,它从黑屏变为白屏,但仍然没有播放。

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

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

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

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

    videoView.setVideoURI(Uri.parse(uriString));
    videoView.setZOrderOnTop(true);//this line solve the problem
    videoView.start();

我通过为VideoView切换alpha解决了这个问题:

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并且可以访问它,您也可以这样做。

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

<?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>

您需要在 onPrepared 中更改 Window 内容! 像显示 toask 或其他,例如

@Override
            public void onPrepared(final MediaPlayer mp) {
                Toast.makeText(getApplicationContext(),"onPrepared",ToasLENGTH_SHORT).show();
                mVideoView.setZOrderOnTop(false);
                mVideoView.setBackgroundColor(Color.TRANSPARENT);
            }

删除 mVideoView.start() 并更改您的 onPrepared 以开始您的视频,

@Override
      public void onPrepared(MediaPlayer mp) {
          mp.start();

..............

我没有安卓低于 4.2 的设备来测试,但这就是我用我的应用程序开始视频的方式

也许为时已晚,但我找到了一个对我有用的解决方案。 我结合了Sagar Limbanifaljbour 的答案

videoView.setVideoURI(Uri.parse(path));
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(final MediaPlayer mp) {
        mp.start();
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                if(mp.getCurrentPosition() != 0){
                    View placeholder = findViewById(R.id.placeholder);
                    placeholder.setVisibility(View.GONE);
                } else {
                    new Handler().postDelayed(this, 50);
                }
            }
        });
    }
});

它适用于 android 5.1、5.0 和 4.0

我遇到过同样的问题。 我发现其主要原因是使用FrameLayout作为父布局。 使用RelativeLayout作为VideoView的父布局

<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" >

    <RelativeLayout
        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>
    </RelativeLayout>

</LinearLayout>

您可以通过使用自定义 VideoPlayer 扩展 VideoView 来解决此问题

    public class VideoPlayer extends VideoView {

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

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        TyrooLog.i(TAG, "onMeasure");
        int width = getDefaultSize(videoWidth, widthMeasureSpec);
        int height = getDefaultSize(videoHeight, heightMeasureSpec);
        if (videoWidth > 0 && videoHeight > 0) {
            if (videoWidth * height > width * videoHeight) {
                TyrooLog.i(TAG, "video too tall, correcting");
                height = width * videoHeight / videoWidth;
            } else if (videoWidth * height < width * videoHeight) {
                TyrooLog.i(TAG, "video too wide, correcting");
                width = height * videoWidth / videoHeight;
            } else {
                TyrooLog.i(TAG, "aspect ratio is correct: " + width+"/"+height+"="+mVideoWidth+"/"+mVideoHeight);
            }
        }
        TyrooLog.i(TAG, "setting size: " + width + 'x' + height);
        setMeasuredDimension(width, height);
    }
}

我遇到了类似的问题,发现添加mp.seekTo(1); onPrepared(MediaPlayer mp)方法中修复了它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM