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

用 Intent 呼叫系統相機錄影:Android 錄製視頻的最短路徑

編輯:關於 Android 程式設計

自己寫錄影功能要處理鏡頭預覽、編碼參數、檔案寫入等一堆細節,多數情況下並沒必要。Android 提供了現成的捷徑:發一個 ACTION_VIDEO_CAPTURE 意圖,把錄影工作交給系統內建的相機應用,錄完再把結果 URI 取回來播放。

版面配置

一個錄影按鈕、一個播放按鈕,加上一個 VideoView 作為顯示區域。

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

    <Button
        android:id="@+id/button_capture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="錄製視頻" />

    <Button
        android:id="@+id/button_play"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button_capture"
        android:text="播放視頻" />

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button_play" />

</RelativeLayout>

主要程式碼

package com.multimediademo14intentcamera;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.VideoView;

/**
 * 使用意圖錄製視頻
 */
public class MainActivity extends Activity implements OnClickListener {

    public static int VIDEO_CAPTURED = 1;

    /** 兩個按鈕:錄製視頻、播放視頻 */
    private Button button_capture, button_play;
    private VideoView videoView;
    private Uri videoFileUri;

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

        button_capture = (Button) findViewById(R.id.button_capture);
        button_play = (Button) findViewById(R.id.button_play);
        videoView = (VideoView) findViewById(R.id.videoView);

        button_capture.setOnClickListener(this);
        button_play.setOnClickListener(this);
        button_play.setEnabled(false); // 還沒有影片前先禁用播放
    }

    @Override
    public void onClick(View v) {
        if (v == button_capture) {
            /**
             * 點擊錄製按鈕後,建立意圖並連同 VIDEO_CAPTURED 一起傳給
             * startActivityForResult(),這會啟動系統內建的相機應用。
             */
            Intent captureVideoIntent =
                    new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
            startActivityForResult(captureVideoIntent, VIDEO_CAPTURED);
        } else if (v == button_play) {
            videoView.setVideoURI(videoFileUri);
            videoView.start();
        }
    }

    /**
     * 相機應用返回時會回到這裡。先確認 resultCode 為 RESULT_OK,
     * 且 requestCode 等於先前傳入的 VIDEO_CAPTURED,
     * 再取出錄製完成的視頻檔案 Uri。
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == VIDEO_CAPTURED) {
            videoFileUri = data.getData();
            button_play.setEnabled(true);
        }
    }
}

流程重點

整段邏輯可以濃縮成三步:建立意圖 → startActivityForResult() → 在 onActivityResult() 取回 URI。系統相機應用負責所有繁瑣細節,我們只處理「甚麼時候開始」和「拿到結果後做什麼」。

實務上要留意兩點:一是錄影這種大檔案活動應該在 AndroidManifest.xml 中相應處理儲存空間權限,Android 10 以上還需配合 MediaStore 或儲存空間存取架構;二是若希望控制錄影長度、畫質或輸出路徑,可以額外帶上 EXTRA_VIDEO_QUALITY、EXTRA_DURATION_LIMIT 與 EXTRA_OUTPUT 等 extras,讓系統相機能照我們的規格錄製。

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