網頁

2022年3月21日 星期一

Android Studio - 相機錄制視頻程式(Camera Video Recording Program)(七十一)

Android Studio - 相機錄制視頻程式(Camera Video Recording Program)(七十一):

完成了相機拍照程式(Camera Take Photo Program)後,開始編寫相機錄制視頻程式(Camera Video Recording Program),相機錄制視頻程式需要使用 MediaRecorder API,使用 MediaRecorder 可以進行錄影和錄音,支援捕獲和編碼各種常見的音訊和視頻格式。

Android 相機錄制視頻程式(Camera Video Recording Program)
  • 操作系統:Windows 7 64-bit 版本
  • 開發環境:Android Studio 4.0.1 版本
  • 原程式:C:\Development\Development_Android\Android_Project\DIY-Android-091-11c Camera Video Recording
  • 程式:C:\Development\Development_Android\Android_Project\DIY-Android-091-11c Camera Video Recording
  • 測試環境:Vivo(維沃)X5Max Android 4.4.4(KitKat)版本

Android 相機錄制視頻程式(Camera Video Recording Program)編程步驟:
  • 設定 Camera 許可權限後 - AndroidManifest.xml
  • 設定 SurfaceView 參數 - init_View()
  • 啟動 SurfaceView Callback - mSurfaceHolder.addCallback(this)
  • 打開 Camera - Camera.open()
  • 設置 Camera 參數 - setParameters()
  • StartPreview Camera 顯示 - setPreviewDisplay(surfaceHolder) → startPreview()
  • 設置 Button Video Recording 參數
  • 設置 Mediarecorder
  • 儲存錄制視頻

AndroidManifest.xml 獲取 Camera 許可權:

    <uses-feature android:name="android.hardware.camera" />

    <uses-permission android:name="android.permission.CAMERA" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


activity_camera_recordvideo.xml:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <SurfaceView

        android:id="@+id/activity_camera_surface"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

 

    <Button

        android:id="@+id/btn_camera_video"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="200dp"

        android:text="Recording Video"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

 

</androidx.constraintlayout.widget.ConstraintLayout>


CameraRecordVideo.java:

package bugworkshop.blogspot.com.camera;

 

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

 

// Camera

import android.hardware.Camera;

 

// SurfaceView

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.View;

import android.widget.Button;

 

// Recording Video

import android.media.CamcorderProfile;

import android.media.MediaRecorder;

import android.os.Environment;

import java.io.IOException;

 

 

public class CameraRecordVideo extends AppCompatActivity implements SurfaceHolder.Callback{

 

    private Button mbtn_camera_video;

 

    // Camera

    private Camera mCamera;

 

    // SurfaceView

    private SurfaceView mSurfaceView;

    private SurfaceHolder mSurfaceHolder;

 

    // Recording Video

    private boolean isRecording = false;

    private MediaRecorder mMediaRecorder;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_camera_recordvideo);

 

        mbtn_camera_video = (Button)findViewById(R.id.btn_camera_video);

 

        mbtn_camera_video.setOnClickListener(new View.OnClickListener()

        {

            @Override

            public void onClick(View v) {

                if (!isRecording) {

                    startRecording();

                } else {

                    stopRecording();

                }

            }

        });

        init_View();

    }

 

    //@Override

    public void init_View() {

        mSurfaceView = (SurfaceView)findViewById(R.id.activity_camera_surface);

        mSurfaceHolder = mSurfaceView.getHolder();

        mSurfaceHolder.addCallback(this);   // CallBack SurfaceCreate

        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }

 

    @Override

    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {

        mCamera = Camera.open();

        if (mCamera != null) {

            Camera.Parameters p = mCamera.getParameters();

            p.setPreviewSize(640, 480);

            p.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);

            mCamera.setParameters(p);

        }

 

        try {

            mCamera.setPreviewDisplay(surfaceHolder);

            mCamera.startPreview();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    @Override

    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

 

    @Override

    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

    }

 

    // Start Recording Video

    public void startRecording() {

        if (mCamera != null) {

            isRecording = true;

            mCamera.unlock();

            mbtn_camera_video.setBackgroundColor(0xffff0000);

            mMediaRecorder = new MediaRecorder();

            mMediaRecorder.setCamera(mCamera);

            mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

            mMediaRecorder.setOutputFile("/sdcard/Video.mp4");

            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

            mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

            mMediaRecorder.setOrientationHint(90);

            mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

            try {

                mMediaRecorder.prepare();

                mMediaRecorder.start();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

 

    // Stop Recording Video

    public void stopRecording() {

        isRecording = false;

        //mRecordbutton.setBackgroundResource(R.drawable.circle_background);

        mbtn_camera_video.setBackgroundColor(0xffffffff);

        try {

            mMediaRecorder.stop();

            mMediaRecorder.release();

            mCamera.lock();

            mCamera.startPreview();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}


2022年 3月 21日(Mon)天氣報告
氣溫:41.0°F / 5.0°C @ 07:00
風速:每小時 8公里
降雨機會:93%
相對濕度:百分之 96%
天氣:雨

沒有留言:

張貼留言