Android系统提供了MediaRecorder类用来在应用中录制视频
使用步骤如下:
1、创建MediaRecorder对象
2、调用MediRecorder对象的setAudioSource()方法设置声音的来源,一般传入MediaRecorder.MIC
3、调用MediaRecorder对象的setOutputFormat()设置所录制的音频文件的格式
4、调用MediaRecorder对象的setAudioRncoder()、setAudioEncodingBitRate(int bitRate)、setAudioSamlingRate(int SamplingRate)设置所录音的编码格式、编码位率、采样率等,
5、调用MediaRecorder对象的setOutputFile(String path)方法设置录制的音频文件的保存位置
6、调用MediaRecoder对象的Prepare()方法准备录制
7、调用MediaRecoder对象的start()方法开始录制
8、调用MediaRecoder对象的stop()方法停止录制,并调用release()方法释放资源
MediaRecorder状态图如下:
需要加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.CAMERA"/>
package com.example.mediarecord;
import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button mButton1,mButton2,mButton3,mButton4;
private SurfaceView mSurfaceView;
private MediaPlayer mediaPlayer;
private MediaRecorder mediaRecorder;
private String FilePath=null;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton1= (Button) findViewById(R.id.btn_start);
mButton2= (Button) findViewById(R.id.btn_stop);
mButton3= (Button) findViewById(R.id.btn_prepare);
mButton4= (Button) findViewById(R.id.btn_pause);
mSurfaceView= (SurfaceView) findViewById(R.id.surface_view);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
mButton4.setOnClickListener(this);
mediaPlayer=new MediaPlayer();
//设置属性识别
mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//设置屏幕高亮
mSurfaceView.setKeepScreenOn(true);
FilePath= Environment.getExternalStorageDirectory().getPath()+"/Download/record.3gp";
//surfaceView的监听事件 回调函数
mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
//打开camera 即初始化camera
camera=Camera.open();
mediaRecorder=new MediaRecorder();
}
//当控件发生变化时
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
try {
//预览
camera.setPreviewDisplay(mSurfaceView.getHolder());
camera.setDisplayOrientation(getDegree(MainActivity.this));
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
}
private int getDegree(Activity activity) {
//得到摄像头本身的角度
int rotation=activity.getWindowManager().getDefaultDisplay().getRotation();
//给角度一个初始值
int degree=0;
switch (rotation){
case Surface.ROTATION_0:
degree=90;
break;
case Surface.ROTATION_90:
degree=0;
break;
case Surface.ROTATION_180:
degree=270;
break;
case Surface.ROTATION_270:
degree=180;
break;
}
return degree;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_start:
startRecord();
break;
case R.id.btn_stop:
mediaRecorder.release();
//camera被占用,所以应释放,然后让他等于空
camera.release();
camera=null;
break;
case R.id.btn_prepare:
startPlay();
break;
case R.id.btn_pause:
break;
}
}
public void startRecord() {
//camera被占用时,start会报错,将camera释放掉,然后再与MediaRecoder进行绑定
camera.unlock();
mediaRecorder.setCamera(camera);
//录制关联
mediaRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface());
//设置音频视频来源
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(FilePath);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaRecorder.start();
}
public void startPlay(){
try {
mediaPlayer.setDisplay(mSurfaceView.getHolder());
mediaPlayer.setDataSource(FilePath);
mediaPlayer.prepare();
mediaPlayer.seekTo(0);
} catch (IOException e) {
e.printStackTrace();
}
}
}