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

Android 錄製視頻(三):拍攝、播放並修改影片元數據

編輯:關於 Android 程式設計

本系列第三篇在系統相機錄影的基礎上,進一步示範如何修改影片的元數據(例如標題)。這裡有一個關鍵觀念:在 MediaStore 中更新元數據時,並不會改動影片檔案本身,而只是更新 MediaStore 中關於該影片的記錄。

MainActivity 程式碼

package c.c;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * Demo 描述:
 * 1. 利用系統相機拍攝視頻
 * 2. 利用系統控件播放視頻
 * 3. 修改視頻的元數據(如視頻 title)
 *
 * 注意:在 MediaStore 中更新元數據時,不會更新視頻文件本身,
 * 而只是更新 MediaStore 中關於該視頻的記錄。
 */
public class MainActivity extends Activity {
    private final static int VIDEO_CAPTURE = 9527;
    private Uri mVideoUri;
    private Button mCaptureButton;
    private Button mPlayButton;
    private Button mSaveButton;
    private EditText mEditText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }

    private void init() {
        mCaptureButton = (Button) findViewById(R.id.capture_Button);
        mCaptureButton.setOnClickListener(new ButtonClickListenerImpl());

        mPlayButton = (Button) findViewById(R.id.play_Button);
        mPlayButton.setEnabled(false);
        mPlayButton.setOnClickListener(new ButtonClickListenerImpl());

        mSaveButton = (Button) findViewById(R.id.save_Button);
        mSaveButton.setEnabled(false);
        mSaveButton.setOnClickListener(new ButtonClickListenerImpl());

        mEditText = (EditText) findViewById(R.id.editText);
        mEditText.setEnabled(false);
    }

    private class ButtonClickListenerImpl implements OnClickListener {
        public void onClick(View v) {
            if (v.getId() == R.id.capture_Button) {
                Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                startActivityForResult(intent, VIDEO_CAPTURE);
            }
            if (v.getId() == R.id.play_Button) {
                Intent intent = new Intent(Intent.ACTION_VIEW, mVideoUri);
                startActivity(intent);
            }
            if (v.getId() == R.id.save_Button) {
                ContentValues contentValues = new ContentValues(1);
                String title = mEditText.getText().toString();
                contentValues.put(MediaStore.MediaColumns.TITLE, title);
                int result = getContentResolver().update(mVideoUri, contentValues, null, null);
                if (result == 1) {
                    System.out.println("設置視頻名稱成功");
                } else {
                    System.out.println("設置視頻名稱失敗");
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == VIDEO_CAPTURE) {
            mVideoUri = data.getData();
            mPlayButton.setEnabled(true);
            mSaveButton.setEnabled(true);
            mEditText.setEnabled(true);
        }
    }
}

版面配置 main.xml

<LinearLayout 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"
    android:orientation="vertical">

    <Button
        android:id="@+id/capture_Button"
        android:layout_width="150dip"
        android:layout_height="wrap_content"
        android:text="Video Capture" />

    <Button
        android:id="@+id/play_Button"
        android:layout_width="150dip"
        android:layout_height="wrap_content"
        android:text="Video Play" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="150dip"
        android:layout_height="wrap_content"
        android:hint="video title" />

    <Button
        android:id="@+id/save_Button"
        android:layout_width="150dip"
        android:layout_height="wrap_content"
        android:text="save Button" />

</LinearLayout>

修改標題的核心在於透過 ContentResolver.update() 搭配 MediaStore.MediaColumns.TITLE,就能更新 MediaStore 中的記錄。掌握這個觀念後,還有許多影片屬性(如描述、方向等)都能用同樣方式調整。

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