前言
Android使用AudioRecord实现录音
提示:以下是本篇文章正文内容,下面案例可供参考
一、AudioRecord使用
Android平台可以使用AudioRecord和MediaRecorder来实现录音,因为AudioRecord更接近底层,并且录制的数据为原始(pcm)数据,pcm数据可以再进行处理转换,直播中使用的都是处理后的pcm数据,所以在这里面学习下使用AudioRecord。
- 构造AudioRecord
AudioRecord类的构造方法中有5个参数
AudioRecord(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat,
int bufferSizeInBytes)
- 创建AudioRecord对象
private AudioRecord audioRecord = null;
private int recordBufsize = 0;
private void createAudioRecord() {
recordBufsize = AudioRecord
.getMinBufferSize(44100,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
44100,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
recordBufsize);
}
- 开始录音
// 调用开始录音
audioRecord.startRecording();
调用开始录音后,需要从audioRecord中读取录音的数据
audioRecord.read(data, 0, recordBufsize);
- 结束录音
audioRecord.stop();
- 释放资源
audioRecord.release();
AudioRecord的关键方法已经介绍了,下面贴上完整的代码
二、使用步骤
清单文件声明权限
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
关于Android 6.0动态申请权限这里就不重复了,可自行百度
完整代码
public class MainActivity extends AppCompatActivity {
private static final String FILE_NAME = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + File.separator + "test.pcm";
private AudioRecord audioRecord = null;
private int recordBufsize = 0;
private boolean isRecording = false;
private Button startRecordBtn;
private Button stopRecordBtn;
private Thread recordingThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1000);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, 2000);
}
startRecordBtn = findViewById(R.id.start_record_btn);
stopRecordBtn = findViewById(R.id.stop_record_btn);
createAudioRecord();
startRecordBtn.setOnClickListener(v -> {
startRecord();
});
stopRecordBtn.setOnClickListener(v -> {
stopRecord();
});
}
private void createAudioRecord() {
recordBufsize = AudioRecord
.getMinBufferSize(44100,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
Log.i("audioRecordTest", "size->" + recordBufsize);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
44100,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
recordBufsize);
}
private void startRecord() {
if (isRecording) {
return;
}
isRecording = true;
audioRecord.startRecording();
Log.i("audioRecordTest", "开始录音");
recordingThread = new Thread(() -> {
byte data[] = new byte[recordBufsize];
File file = new File(FILE_NAME);
FileOutputStream os = null;
try {
if (!file.exists()) {
file.createNewFile();
Log.i("audioRecordTest", "创建录音文件->" + FILE_NAME);
}
os = new FileOutputStream(file);
} catch (Exception e) {
e.printStackTrace();
}
int read;
if (os != null) {
while (isRecording) {
read = audioRecord.read(data, 0, recordBufsize);
if (AudioRecord.ERROR_INVALID_OPERATION != read) {
try {
os.write(data);
Log.i("audioRecordTest", "写录音数据->" + read);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
});
recordingThread.start();
}
private void stopRecord() {
isRecording = false;
if (audioRecord != null) {
audioRecord.stop();
Log.i("audioRecordTest", "停止录音");
audioRecord.release();
audioRecord = null;
recordingThread = null;
}
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/start_record_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始录音" />
<Button
android:id="@+id/stop_record_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="结束录音" />
</LinearLayout>
运行程序,点击开始录音,本地会创建test.pcm文件并写入录音数据。
总结
通过上面的学习,对AudioRecord有初步的了解,能使用AudioRecord来实现录音功能保存到本地。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。