Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

VideoView 實作影音播放:播放、暫停與跳轉時機

編輯:關於 Android 開發

VideoView 是最省事的播放方案

要在 Android 裡播放一支影片,VideoView 是最快上手的选择:它已經把 MediaPlayer 與 SurfaceView 的組合包裝好了,你只要給它一個路徑,再呼叫 start()、pause()、seekTo() 三個方法,基本播放控制就完成了。

一、布局:播放器 + 三顆控制鈕

用 FrameLayout 當根容器,讓控制列以 layout_gravity="bottom" 疊在播放器下方。

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.videoview.MainActivity">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="416dp" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal">

        <Button android:id="@+id/start"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="開始" />
        <Button android:id="@+id/pause"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="暫停" />
        <Button android:id="@+id/seekto"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="3秒處" />
    </LinearLayout>
</FrameLayout>

二、主程式碼

package com.example.videotest;

import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.VideoView;

public class MainActivity extends Activity implements OnClickListener {

    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        videoView = (VideoView) findViewById(R.id.videoView);

        // 從外部儲存空間的 DCIM 目錄取影片
        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        File f = new File(path, "/Camera/test.mp4");
        videoView.setVideoPath(f.getAbsolutePath());

        Button start  = (Button) findViewById(R.id.start);
        start.setOnClickListener(this);
        Button pause  = (Button) findViewById(R.id.pause);
        pause.setOnClickListener(this);
        Button seekto = (Button) findViewById(R.id.seekto);
        seekto.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                videoView.start();
                break;
            case R.id.pause:
                videoView.pause();
                break;
            case R.id.seekto:
                videoView.seekTo(3 * 1000);   // 單位是毫秒
                break;
        }
    }
}

三、幾個一定要知道的細節

seekTo 的單位是毫秒。seekTo(3 * 1000) 代表跳到第 3 秒,寫成 seekTo(3) 幾乎感覺不到變化,這是新手最常踩的坑。

setVideoPath 之後不能直接 seekTo。VideoView 在載入媒體過程中還不知道總長度,過早呼叫 seekTo 可能無效。正確做法是在 OnPreparedListener 的 onPrepared 回呼中再處理跳轉與初始位置。

記得補上 MediaController。若不想自己做控制列,一行就能拿到系統內建的播放/拖曳進度條介面:

videoView.setMediaController(new MediaController(this));

播放完成後不會自動重播。需要的話設 setOnCompletionListener,在其中呼叫 videoView.start() 即可迴圈播放。

權限與生命週期。讀取外部儲存空間需要在 manifest 宣告 READ_EXTERNAL_STORAGE,在 Android 6.0 以上還必須執行期動態申請。另外建議在 onPause 中呼叫 videoView.pause(),避免離開頁面後影片還在背景出聲。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved