辛苦堆砌,转载请注明出处,谢谢!
下面开始使用MediaRecorder完成录音功能,在使用MediaRecorder前,首先应当了解MediaRecorder的状态图,按图索骥,没有问题!下面给出MediaRecorder的状态图(实际上贴不贴无所谓了,网上一找一大把)。
从状态图看出几点:
1.除了Release,其他状态下,都可以调用reset回到Initial状态;
2.Release是一个状态孤岛,进到Release状态之后,整个状态图终结;
3.从Initial才可以到达Release,所以需要reset后,才能release;
4.注意从Initial到Prepared的过程,中间需要很多配置接口,记得设置,由于MediaRecorder既可以录音也可以录像,录音和录像需要设置的不同。
有了这几点认知,就可以基于MediaRecorder封装我们自己的AudioRecorder了,之所以封装,是因为基于MediaRecorder还可以进行录像,到时候我们可以做VideoRecorder,实际上我也是这么打算的XD
package com.yjp.recorder;
import android.media.MediaRecorder;
import java.io.IOException;
public class AudioRecorder {
private MediaRecorder mRecorder;
public AudioRecorder() {
mRecorder = new MediaRecorder();
}
public void ReleaseAudioRecorder() {
resetAudioRecorder();
mRecorder.release();
mRecorder = null;
}
public void prepareAudioRecorder(String filePathname){
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mRecorder.setOutputFile(filePathname);
try {
mRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
resetAudioRecorder();
}
}
public void startAudioRecorder() {
try {
mRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
resetAudioRecorder();
}
}
public void stopAudioRecorder() {
try {
mRecorder.stop();
} catch (IllegalStateException e) {
e.printStackTrace();
resetAudioRecorder();
}
}
private void resetAudioRecorder() {
mRecorder.reset();
}
}
构造函数创建MediaRecorder,提供ReleaseAudioRecorder()释放MediaRecorder,prepareAudioRecorder则是完成了从Initial到Prepared的,准备好了可用的MediaRecorder,其他函数都只是MediaRecorder的简单封装,唯一加入的处理是,当状态出错时,直接重置为Initial状态。
下面完成Activity部分,很简单,利用一个按钮启动和关闭录音
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="center"
tools:context="com.yjp.audiorecorder.MainActivity">
<Button
android:id="@+id/startAudioRecordingButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/startAudioRecording"/>
</RelativeLayout>
package com.yjp.audiorecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.yjp.recorder.AudioRecorder;
import java.io.File;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private Button mStartAudioRecordingButton;
private AudioRecorder mAudioRecorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStartAudioRecordingButton = (Button) findViewById(R.id.startAudioRecordingButton);
mStartAudioRecordingButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mStartAudioRecordingButton.getText().equals(
getResources().getString(R.string.startAudioRecording))) {
mStartAudioRecordingButton.setText(getResources().getString(R.string.stopAudioRecording));
String audioFileDir = Environment.getExternalStorageDirectory() + "/audio/";
File dir = new File(audioFileDir);
if (!dir.exists()) {
dir.mkdirs();
}
String audioFileName = new Date().getTime() + ".mp3";
mAudioRecorder.prepareAudioRecorder(audioFileDir + audioFileName);
mAudioRecorder.startAudioRecorder();
} else {
mStartAudioRecordingButton.setText(getResources().getString(R.string.startAudioRecording));
mAudioRecorder.stopAudioRecorder();
}
}
});
}
@Override
protected void onStart() {
super.onStart();
if (null == mAudioRecorder) {
mAudioRecorder = new AudioRecorder();
}
}
@Override
protected void onStop() {
if (null != mAudioRecorder) {
mAudioRecorder.ReleaseAudioRecorder();
mAudioRecorder = null;
}
mStartAudioRecordingButton.setText(getResources().getString(R.string.startAudioRecording));
super.onStop();
}
}
清单文件添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
完成,跑起来看看吧!
总结:学会使用MediaRecorder录音和录像,玩懂状态图就妥了,没什么难的。
源码链接在这里