【问题标题】:android-black screen on displaying video by using VideoView使用 VideoView 显示视频时出现 android-black 屏幕
【发布时间】:2015-04-19 02:31:38
【问题描述】:

这是我的布局:

<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上,第一次打开它可以找到但是当关闭应用程序并再次打开它时,出现黑屏,如下所示:

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

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

【问题讨论】:

  • 它在 onPrepared 中进行吗?
  • @varunbhardwaj 感谢您的回复,我登录了,但它没有转到 onPrepared
  • 完美!!现在您可以检查原因,因为视频没有准备好,因此视频格式可能存在问题或其他问题
  • 对于任何听到声音但看到黑屏的人:只需确保添加 mediaplayer.setDisplay(holder);在surfaceCreated 方法中。

标签: android android-videoview


【解决方案1】:

此代码适用于我,在 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>

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      您可以通过使用自定义 VideoPlayer extends 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);
          }
      }
      

      【讨论】:

        【解决方案4】:

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

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

          我有同样的问题。我发现主要原因是使用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>
          

          【讨论】:

            【解决方案6】:

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

            在 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.setZOrderOnTop(true);给白屏。
            • 如果您想在视频顶部显示视图,则它不起作用。
            【解决方案7】:

            你需要在onPrepared 中改变Window 的内容!像 show toask 或其他,例如

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

            【讨论】:

              【解决方案8】:

              也许为时已晚,但我找到了适合我的解决方案。我结合了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

              【讨论】:

                【解决方案9】:

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

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

                我没有 android 低于 4.2 的设备可供测试,但这就是我使用我的应用程序开始视频的方式

                【讨论】:

                • 在 android 4+ 上运行良好,但在 android 小于 3 上也有同样的问题
                猜你喜欢
                • 1970-01-01
                • 2023-03-20
                • 2022-10-17
                • 1970-01-01
                • 2012-12-25
                • 1970-01-01
                • 1970-01-01
                • 2014-03-08
                • 1970-01-01
                相关资源
                最近更新 更多