正文
今天我们来学习下如何使用Android的录音类MediaRecorder
我们在开发一些软件,比如即时通信的软件,需要发送语音的功能,在发送之前我们需要把用户通过麦克风输入的声音录制下来,那么就需要借助Android自带的MediaRecorder进行开发,当然,Android还提供了一个更专业的类AudioRecord,它可以更加专业的帮助我们处理音频,但是AudioRecord使用起来比较麻烦,所有,为了方便,我们可以使用MediaRecorder进行开发,好了,废话不多说,直接开始
创建一个录音工具类
MyMediaRecorder.java
public class MyMediaRecorder {
private static MyMediaRecorder instance;
private MediaRecorder mMediaRecorder;
private String filePath;
private MyMediaRecorder() {
}
public static synchronized MyMediaRecorder getInstance() {
if (null == instance) {
synchronized (MyMediaRecorder.class) {
if (null == instance) {
instance = new MyMediaRecorder();
}
}
}
return instance;
}
}
我们把这个工具类做成单例模式,方便我们调用相关的方法
开始录音
/**
* 开始录音
*
* @param path
*/
public void startRecord(String path) {
if (null == mMediaRecorder) {
mMediaRecorder = new MediaRecorder();
}
try {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
filePath = path;
mMediaRecorder.setOutputFile(filePath);
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
可以看到,开始录音比较容易理解,我们先获取mMediaRecorder的实例,然后对其进行一些配置,比如
- 音频源(setAudioSource)
- 输出格式(setOutputFormat)
- 编码(setAudioEncoder)
- 输出路径(setOutputFile)
接着调用prepare()进行准备,最后调用start()方法开始录音,很简单不是吗
停止录音
/**
* 结束录音
*/
public void stopRecord() {
try {
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;
filePath = "";
} catch (RuntimeException e) {
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
filePath = "";
}
}
结束录音也比较简单,我们只需要将相应的资源释放掉就可以了
下面是完整的代码
MyMediaRecorder.java
package com.yk.jchat.utils;
import android.media.MediaRecorder;
import java.io.File;
import java.io.IOException;
public class MyMediaRecorder {
private static MyMediaRecorder instance;
private MediaRecorder mMediaRecorder;
private String filePath;
private MyMediaRecorder() {
}
public static synchronized MyMediaRecorder getInstance() {
if (null == instance) {
synchronized (MyMediaRecorder.class) {
if (null == instance) {
instance = new MyMediaRecorder();
}
}
}
return instance;
}
/**
* 开始录音
*
* @param path
*/
public void startRecord(String path) {
if (null == mMediaRecorder) {
mMediaRecorder = new MediaRecorder();
}
try {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
filePath = path;
mMediaRecorder.setOutputFile(filePath);
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 结束录音
*/
public void stopRecord() {
try {
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;
filePath = "";
} catch (RuntimeException e) {
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
filePath = "";
}
}
}
好了,今天的录音工具类就到讲到这里,希望大家喜欢。
网友评论