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

Android 使用 VideoView 播放影片完整範例

編輯:關於 Android 程式設計

VideoView 是最省事的播放方案:它內建 MediaPlayer 與 SurfaceView,只要指定影片來源再呼叫 start() 就能播。搭配 MediaController 還能免費得到播放/暫停與拖拉進度的控制列。

Activity 程式碼

public class Activity01 extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

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

        Button PauseButton = (Button) this.findViewById(R.id.PauseButton);
        Button LoadButton = (Button) this.findViewById(R.id.LoadButton);
        Button PlayButton = (Button) this.findViewById(R.id.PlayButton);

        // 載入
        LoadButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0)
            {
                // videoView.setVideoPath("/sdcard/test.mp4");
                videoView.setVideoPath("android.resource://com.homer/" + R.raw.china);
                videoView.setMediaController(new MediaController(Activity01.this));
                videoView.requestFocus();
            }
        });

        // 播放
        PlayButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0)
            {
                videoView.start();
            }
        });

        // 暫停
        PauseButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0)
            {
                videoView.pause();
            }
        });
    }
}

版面配置 main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <VideoView
        android:id="@+id/VideoView01"
        android:layout_width="320px"
        android:layout_height="240px" />

    <Button
        android:id="@+id/LoadButton"
        android:layout_width="80px"
        android:layout_height="wrap_content"
        android:layout_x="30px"
        android:layout_y="300px"
        android:text="裝載" />

    <Button
        android:id="@+id/PlayButton"
        android:layout_width="80px"
        android:layout_height="wrap_content"
        android:layout_x="120px"
        android:layout_y="300px"
        android:text="播放" />

    <Button
        android:id="@+id/PauseButton"
        android:layout_width="80px"
        android:layout_height="wrap_content"
        android:layout_x="210px"
        android:layout_y="300px"
        android:text="暫停" />

</AbsoluteLayout>

影片來源的三種寫法

範例中同時出現了兩種來源,實際上有三種常見形式:

  • 本機檔案:videoView.setVideoPath("/sdcard/test.mp4")
  • raw 資源:setVideoPath("android.resource://包名/" + R.raw.china)
  • 網路串流:setVideoUri(Uri.parse("https://.../video.mp4")),此時務必加上 INTERNET 權限。

幾個實務上的提醒

  • 版面中的尺寸請改用 dip 而非 px,否則在不同解析度的裝置上會嚴重變形。
  • AbsoluteLayout 已被棄用,建議改用 RelativeLayout 或 ConstraintLayout。
  • VideoView 不支援所有編碼格式,MP4(H.264)相容性最好;播放前可先用 MediaMetadataRetriever 檢查。
  • 記得在 onPause() 中呼叫 videoView.pause(),離開頁面時停止播放,避免聲音繼續跑。
  • 若需要自訂進度條與全螢幕行為,MediaController 的預設樣式可能不夠用,可以繼承改寫。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved