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