1. 程式人生 > >android videoview 播放前黑屏的解決方法之一

android videoview 播放前黑屏的解決方法之一

http://stackoverflow.com/questions/9765629/android-videoview-black-screen

播放前黑屏的原因是videoview載入資源需要一定的耗時, 如何避免播放前的黑屏現象呢, 可以給videoview設定載入的監聽, 如果載入前給一個遮罩,等資源載入完成後隱藏遮罩.

如果有更好的解決方法,歡迎留言大笑

採用這樣解決方案: 需要videoview放到framelayout佈局中:

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

videoview設定監聽:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                //Called when the video is ready to play
                View placeholder = findViewById(R.id.placeholder);

                placeholder.setVisibility(View.GONE);
            }
        });