MediaRecorder進(jìn)行錄影和錄音沒什么差別 ,就多了一個(gè)設(shè)置圖像的格式
參考:http://www.jfrwli.cn/article/141197.html
實(shí)例:
<!-- 授予該程序錄制聲音的權(quán)限 -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- 授予該程序使用攝像頭的權(quán)限 -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 授予使用外部存儲(chǔ)的權(quán)限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<SurfaceView
android:id="@+id/dView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="@+id/record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/record" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/stop" />
</LinearLayout>
</RelativeLayout>
package com.android.xiong.videotest;
import java.io.File;
import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button record, stop;
// 系統(tǒng)視頻文件
File viodFile;
MediaRecorder mRecorder;
// 顯示視頻的SurfaceView
SurfaceView sView;
// 記錄是否正在進(jìn)行錄制
boolean isRecording = false;
Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
record = (Button) findViewById(R.id.record);
stop = (Button) findViewById(R.id.stop);
sView = (SurfaceView) findViewById(R.id.dView);
// stop按鈕不可用
stop.setEnabled(false);
// 設(shè)置Surface不需要維護(hù)自己的緩沖區(qū)
sView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// 設(shè)置分辨率
sView.getHolder().setFixedSize(320, 280);
// 設(shè)置該組件不會(huì)讓屏幕自動(dòng)關(guān)閉
sView.getHolder().setKeepScreenOn(true);
record.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.record:
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "SD卡不存在,請(qǐng)插卡!", Toast.LENGTH_SHORT).show();
return;
}
try {
// 創(chuàng)建MediaPlayer對(duì)象
mRecorder = new MediaRecorder();
mRecorder.reset();
/* camera = Camera.open();
camera.unlock();
camera.setDisplayOrientation(0);
mRecorder.setCamera(camera);*/
// 創(chuàng)建保存錄制視頻的視頻文件
viodFile = new File(Environment.getExternalStorageDirectory()
.getCanonicalFile() + "/myvideo.mp4");
if (!viodFile.exists())
viodFile.createNewFile();
// 設(shè)置從麥克風(fēng)采集聲音
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 設(shè)置從攝像頭采集圖像
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// 設(shè)置視頻、音頻的輸出格式
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
// 設(shè)置音頻的編碼格式、
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
// 設(shè)置圖像編碼格式
mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mRecorder.setOrientationHint(90);
//mRecorder.setVideoSize(320, 280);
// mRecorder.setVideoFrameRate(5);
mRecorder.setOutputFile(viodFile.getAbsolutePath());
// 指定SurfaceView來預(yù)覽視頻
mRecorder.setPreviewDisplay(sView.getHolder().getSurface());
mRecorder.prepare();
// 開始錄制
mRecorder.start();
// 讓record按鈕不可用
record.setEnabled(false);
// 讓stop按鈕可用
stop.setEnabled(true);
isRecording = true;
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.stop:
// 如果正在錄制
if (isRecording) {
// 停止錄制
mRecorder.stop();
// 釋放資源
mRecorder.release();
mRecorder = null;
// 讓record按鈕可用
record.setEnabled(true);
// 讓stop按鈕不可用
stop.setEnabled(false);
}
break;
default:
break;
}
}
}