Android音视频开发之音频录制和播放
1.封装音频录制工具类:
public class RecorderAudioManagerUtils {
private static volatile RecorderAudioManagerUtils mInstance;
public static RecorderAudioManagerUtils getInstance() {
if (mInstance == null) {
synchronized (RecorderAudioManagerUtils.class) {
if (mInstance == null) {
mInstance = new RecorderAudioManagerUtils();
}
}
}
return mInstance;
}
}
2.音频录制方法:
public void startRecord(WeakReference<Context> weakReference) {
this.weakReference = weakReference;
Log.e(TAG, "开始录音");
//生成PCM文件
String fileName = DateFormat.format("yyyy-MMdd-HHmmss", Calendar.getInstance(Locale.getDefault())) + ".pcm";
File file = new File(Environment.getExternalStorageDirectory(), "/ACC音频/");
if (!file.exists()) {
file.mkdir();
}
String audioSaveDir = file.getAbsolutePath();
Log.e(TAG, audioSaveDir);
recordFile = new File(audioSaveDir, fileName);
Log.e(TAG, "生成文件" + recordFile);
//如果存在,就先删除再创建
if (recordFile.exists()) {
recordFile.delete();
Log.e(TAG, "删除文件");
}
try {
recordFile.createNewFile();
Log.e(TAG, "创建文件");
} catch (IOException e) {
Log.e(TAG, "未能创建");
throw new IllegalStateException("未能创建" + recordFile.toString());
}
if (filePathList.size() == 2) {
filePathList.clear();
}
filePathList.add(recordFile);
try {
//输出流
OutputStream os = new FileOutputStream(recordFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
int bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, AudioFormat.CHANNEL_IN_STEREO, audioEncoding);
if (ActivityCompat.checkSelfPermission(weakReference.get(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
return;
}
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, AudioFormat.CHANNEL_IN_STEREO, audioEncoding, bufferSize);
short[] buffer = new short[bufferSize];
audioRecord.startRecording();
Log.e(TAG, "开始录音");
isRecording = true;
while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
for (int i = 0; i < bufferReadResult; i++) {
dos.writeShort(buffer[i]);
}
}
audioRecord.stop();
dos.close();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "录音失败");
showToast("录音失败");
}
}
3.播放音频方法:
public void playPcm(boolean isChecked) {
try {
if (isChecked) {
//两首一起播放
for (File recordFiles : filePathList) {
threadPoolExecutor.execute(() -> playPcmData(recordFiles));
}
} else {
//只播放最后一次录音
playPcmData(recordFile);
}
}catch (Exception e){
e.printStackTrace();
}
}
4.播放pcm流边录边播:
/**
* 播放Pcm流,边读取边播
*/
public void playPcmData(File recordFiles) {
Log.e(TAG, "打印线程" + Thread.currentThread().getName());
try {
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(recordFiles)));
//最小缓存区
int bufferSizeInBytes = AudioTrack.getMinBufferSize(sampleRateInHz, AudioFormat.CHANNEL_OUT_STEREO, audioEncoding);
AudioTrack player = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRateInHz, AudioFormat.CHANNEL_OUT_STEREO, audioEncoding, bufferSizeInBytes, AudioTrack.MODE_STREAM);
short[] data = new short[bufferSizeInBytes];
//开始播放
player.play();
while (true) {
int i = 0;
while (dis.available() > 0 && i < data.length) {
data[i] = dis.readShort();
i++;
}
player.write(data, 0, data.length);
//表示读取完了
if (i != bufferSizeInBytes) {
player.stop();//停止播放
player.release();//释放资源
dis.close();
showToast("播放完成了!!!");
break;
}
}
} catch (Exception e) {
Log.e(TAG, "播放异常: " + e.getMessage());
showToast("播放异常!!!!");
e.printStackTrace();
}
}
5.播放所有音频方法:
public void playAllRecord() {
if (recordFile == null) {
return;
}
//读取文件
int musicLength = (int) (recordFile.length() / 2);
short[] music = new short[musicLength];
try {
InputStream is = new FileInputStream(recordFile);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
int i = 0;
while (dis.available() > 0) {
music[i] = dis.readShort();
i++;
}
dis.close();
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRateInHz, channelConfiguration, audioEncoding, musicLength * 2, AudioTrack.MODE_STREAM);
audioTrack.play();
audioTrack.write(music, 0, musicLength);
audioTrack.stop();
} catch (Throwable t) {
Log.e(TAG, "播放失败");
showToast("播放失败");
}
}
6.设置是否录音方法:
public void setRecord(boolean isRecording) {
this.isRecording = isRecording;
}
7.暂停播放音频方法:
public void pauseAudio(){
if(audioRecord != null ){
audioRecord.stop();
}
}
8.回收资源和播放器方法:
public void releaseAudio(){
if(audioRecord != null){
audioRecord.release();
}
if(handler != null){
handler.removeCallbacksAndMessages(null);
}
if(threadPoolExecutor != null){
threadPoolExecutor.shutdown();
}
}
9.音频播放、文件读写权限申请:
private void afterPermissions() {
// Marshmallow开始才用申请运行时权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
for (int i = 0; i < permissions.length; i++) {
if (ContextCompat.checkSelfPermission(this, permissions[i]) !=
PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(permissions[i]);
}
}
if (!mPermissionList.isEmpty()) {
String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);
ActivityCompat.requestPermissions(this, permissions, MY_PERMISSIONS_REQUEST);
}
}
}
10.调用开始录音:
btnRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonEnabled(false, true, true);
Toast.makeText(MainActivity.this, "开始录音", Toast.LENGTH_SHORT).show();
threadPoolExecutor.execute(() -> {
RecorderAudioManagerUtils.getInstance().startRecord(new WeakReference<>(getApplicationContext()));
});
}
});
11.调用播放音频:
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RecorderAudioManagerUtils.getInstance().playPcm(true);
buttonEnabled(false, false, true);
}
});
12.调用停止录音:
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Handler().post(new Runnable() {
@Override
public void run() {
buttonEnabled(true, true, false);
Toast.makeText(MainActivity.this, "停止录音", Toast.LENGTH_SHORT).show();
RecorderAudioManagerUtils.getInstance().pauseAudio();
}
});
}
});
13.设置录音、播放、停止按钮状态:
private void buttonEnabled(boolean record, boolean play, boolean stop) {
btnRecord.setEnabled(record);
btnPlay.setEnabled(play);
btnStop.setEnabled(stop);
}
14.布局文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="录制音频"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/btn_play"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放音频"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/btn_stop"
app:layout_constraintStart_toEndOf="@id/btn_record"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止录制"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/btn_play"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
15.布局预览效果如下:
16.完整MainActivity代码:
public class MainActivity extends AppCompatActivity {
private Button btnRecord,btnPlay,btnStop;
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
3, 5,
1, TimeUnit.MINUTES,
new LinkedBlockingDeque<>(10),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
private String[] permissions = new String[]{
android.Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
/**
* 被用户拒绝的权限列表
*/
private List<String> mPermissionList = new ArrayList<>();
private static final int MY_PERMISSIONS_REQUEST = 1001;
private final String TAG = MainActivity.this.getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
afterPermissions();
initView();
}
private void afterPermissions() {
// Marshmallow开始才用申请运行时权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
for (int i = 0; i < permissions.length; i++) {
if (ContextCompat.checkSelfPermission(this, permissions[i]) !=
PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(permissions[i]);
}
}
if (!mPermissionList.isEmpty()) {
String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);
ActivityCompat.requestPermissions(this, permissions, MY_PERMISSIONS_REQUEST);
}
}
}
private void initView() {
btnRecord = findViewById(R.id.btn_record);
btnPlay = findViewById(R.id.btn_play);
btnStop = findViewById(R.id.btn_stop);
btnRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonEnabled(false, true, true);
Toast.makeText(MainActivity.this, "开始录音", Toast.LENGTH_SHORT).show();
threadPoolExecutor.execute(() -> {
RecorderAudioManagerUtils.getInstance().startRecord(new WeakReference<>(getApplicationContext()));
});
}
});
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RecorderAudioManagerUtils.getInstance().playPcm(true);
buttonEnabled(false, false, true);
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Handler().post(new Runnable() {
@Override
public void run() {
buttonEnabled(true, true, false);
Toast.makeText(MainActivity.this, "停止录音", Toast.LENGTH_SHORT).show();
RecorderAudioManagerUtils.getInstance().pauseAudio();
}
});
}
});
}
private void buttonEnabled(boolean record, boolean play, boolean stop) {
btnRecord.setEnabled(record);
btnPlay.setEnabled(play);
btnStop.setEnabled(stop);
}
@Override
protected void onStop() {
super.onStop();
RecorderAudioManagerUtils.getInstance().pauseAudio();
}
@Override
protected void onDestroy() {
super.onDestroy();
RecorderAudioManagerUtils.getInstance().releaseAudio();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean allGranted = true;
for (int results : grantResults) {
allGranted &= results == PackageManager.PERMISSION_GRANTED;
}
if (allGranted) {
afterPermissions();
} else {
Toast.makeText(this, "请打开特殊权限", Toast.LENGTH_LONG).show();
}
}
}
17.工具类完整代码如下:
package com.example.audiorecorddemo.utils;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @author: njb
* @date: 2023/2/25 18:29
* @desc:
*/
public class RecorderAudioManagerUtils {
private static final String TAG = "PlayManagerUtils";
private WeakReference<Context> weakReference;
private File recordFile;
private boolean isRecording;
/**
* 最多只能存2条记录
*/
private final List<File> filePathList = new ArrayList<>(2);
/**
* 16K采集率
*/
int sampleRateInHz = 16000;
/**
* 格式
*/
int channelConfiguration = AudioFormat.CHANNEL_OUT_STEREO;
/**
* 16Bit
*/
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
private static volatile RecorderAudioManagerUtils mInstance;
private final Handler handler = new Handler(Looper.getMainLooper());
AudioRecord audioRecord;
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 5, 1, TimeUnit.MINUTES, new LinkedBlockingDeque<>(10));
public static RecorderAudioManagerUtils getInstance() {
if (mInstance == null) {
synchronized (RecorderAudioManagerUtils.class) {
if (mInstance == null) {
mInstance = new RecorderAudioManagerUtils();
}
}
}
return mInstance;
}
public void startRecord(WeakReference<Context> weakReference) {
this.weakReference = weakReference;
Log.e(TAG, "开始录音");
//生成PCM文件
String fileName = DateFormat.format("yyyy-MMdd-HHmmss", Calendar.getInstance(Locale.getDefault())) + ".pcm";
File file = new File(Environment.getExternalStorageDirectory(), "/ACC音频/");
if (!file.exists()) {
file.mkdir();
}
String audioSaveDir = file.getAbsolutePath();
Log.e(TAG, audioSaveDir);
recordFile = new File(audioSaveDir, fileName);
Log.e(TAG, "生成文件" + recordFile);
//如果存在,就先删除再创建
if (recordFile.exists()) {
recordFile.delete();
Log.e(TAG, "删除文件");
}
try {
recordFile.createNewFile();
Log.e(TAG, "创建文件");
} catch (IOException e) {
Log.e(TAG, "未能创建");
throw new IllegalStateException("未能创建" + recordFile.toString());
}
if (filePathList.size() == 2) {
filePathList.clear();
}
filePathList.add(recordFile);
try {
//输出流
OutputStream os = new FileOutputStream(recordFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
int bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, AudioFormat.CHANNEL_IN_STEREO, audioEncoding);
if (ActivityCompat.checkSelfPermission(weakReference.get(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
return;
}
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, AudioFormat.CHANNEL_IN_STEREO, audioEncoding, bufferSize);
short[] buffer = new short[bufferSize];
audioRecord.startRecording();
Log.e(TAG, "开始录音");
isRecording = true;
while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
for (int i = 0; i < bufferReadResult; i++) {
dos.writeShort(buffer[i]);
}
}
audioRecord.stop();
dos.close();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "录音失败");
showToast("录音失败");
}
}
/**
* 播放pcm流的方法,一次性读取所有Pcm流,读完后在开始播放
*/
public void playAllRecord() {
if (recordFile == null) {
return;
}
//读取文件
int musicLength = (int) (recordFile.length() / 2);
short[] music = new short[musicLength];
try {
InputStream is = new FileInputStream(recordFile);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
int i = 0;
while (dis.available() > 0) {
music[i] = dis.readShort();
i++;
}
dis.close();
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRateInHz, channelConfiguration, audioEncoding, musicLength * 2, AudioTrack.MODE_STREAM);
audioTrack.play();
audioTrack.write(music, 0, musicLength);
audioTrack.stop();
} catch (Throwable t) {
Log.e(TAG, "播放失败");
showToast("播放失败");
}
}
public void playPcm(boolean isChecked) {
try {
if (isChecked) {
//两首一起播放
for (File recordFiles : filePathList) {
threadPoolExecutor.execute(() -> playPcmData(recordFiles));
}
} else {
//只播放最后一次录音
playPcmData(recordFile);
}
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 播放Pcm流,边读取边播
*/
public void playPcmData(File recordFiles) {
Log.e(TAG, "打印线程" + Thread.currentThread().getName());
try {
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(recordFiles)));
//最小缓存区
int bufferSizeInBytes = AudioTrack.getMinBufferSize(sampleRateInHz, AudioFormat.CHANNEL_OUT_STEREO, audioEncoding);
AudioTrack player = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRateInHz, AudioFormat.CHANNEL_OUT_STEREO, audioEncoding, bufferSizeInBytes, AudioTrack.MODE_STREAM);
short[] data = new short[bufferSizeInBytes];
//开始播放
player.play();
while (true) {
int i = 0;
while (dis.available() > 0 && i < data.length) {
data[i] = dis.readShort();
i++;
}
player.write(data, 0, data.length);
//表示读取完了
if (i != bufferSizeInBytes) {
player.stop();//停止播放
player.release();//释放资源
dis.close();
showToast("播放完成了!!!");
break;
}
}
} catch (Exception e) {
Log.e(TAG, "播放异常: " + e.getMessage());
showToast("播放异常!!!!");
e.printStackTrace();
}
}
public File getRecordFile() {
return recordFile;
}
public void setRecord(boolean isRecording) {
this.isRecording = isRecording;
}
public void pauseAudio(){
if(audioRecord != null ){
audioRecord.stop();
}
}
public void releaseAudio(){
if(audioRecord != null){
audioRecord.release();
}
if(handler != null){
handler.removeCallbacksAndMessages(null);
}
if(threadPoolExecutor != null){
threadPoolExecutor.shutdown();
}
}
private void showToast(String msg) {
if(weakReference.get() != null){
handler.post(() -> Toast.makeText(weakReference.get(), msg, Toast.LENGTH_LONG).show());
}
}
}
19.总结:
后面会放上类图和录音播放的效果,如果小伙伴们感兴趣可以自行尝试一下,下一篇会实现把录制的pcm流转成aac格式,总结遇到的问题和解决方法.写文章不易,且行且珍惜,喜欢的小伙伴们点赞转发,若有问题及时留言!