在Android中的VideoView上显示和隐藏播放按钮

Joe*_*dez 10 android android-videoview

我希望能够显示一个按钮来启动视频,在视频播放的同一视图中心(使用VideoView).我也希望按钮在单击后消失,因为我在视频启动后使用MediaController类执行暂停,倒带,快进操作.

我该怎么做呢?

这是我到目前为止的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/LinearLayout1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">

<FrameLayout
  android:id="@+id/video_frame"
  android:layout_width="fill_parent"
  android:layout_height="480px"
  android:background="#000"
  >

  <VideoView
    android:id="@+id/video_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

我已经尝试以编程方式将ImageButton添加到FrameLayout,但这似乎不起作用.

Joe*_*dez 12

好的,这就是我解决这个问题的方法.布局文件非常简单.只需在VideoView元素添加一个ImageButton :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/LinearLayout1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">

<FrameLayout
  android:id="@+id/video_frame"
  android:layout_width="fill_parent"
  android:layout_height="480px"
  android:background="#000"
  >

  <VideoView
    android:id="@+id/video_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

  <ImageButton
    android:id="@+id/play_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    android:src="@drawable/ic_launcher"
    />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

FrameLayout视图元素按照您在布局中定义它们的顺序将其子元素层叠在一起.因此,布局中添加的最后一个元素将被绘制在顶部.请注意,ImageButton具有以下属性:

android:layout_gravity="center_vertical|center_horizontal"
Run Code Online (Sandbox Code Playgroud)

此属性将ImageButton集中在FrameLayout中.

下一个技巧是让ImageButton在点击后消失.使用ImageButton上的setVisibility()方法执行此操作:

    // Setup a play button to start the video
    mPlayButton = (ImageButton) findViewById(R.id.play_button);
    mPlayButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mPlayer.isPlaying()) {
                resetPlayer();
            } else {
                playVideo(videoUrl, mVideoView.getHolder());
                // show the media controls
                mController.show();
                // hide button once playback starts
                mPlayButton.setVisibility(View.GONE);
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)