網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言
Android使用AudioRecord實(shí)現(xiàn)錄音
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、AudioRecord使用
Android平臺(tái)可以使用AudioRecord和MediaRecorder來(lái)實(shí)現(xiàn)錄音,因?yàn)锳udioRecord更接近底層,并且錄制的數(shù)據(jù)為原始(pcm)數(shù)據(jù),pcm數(shù)據(jù)可以再進(jìn)行處理轉(zhuǎn)換,直播中使用的都是處理后的pcm數(shù)據(jù),所以在這里面學(xué)習(xí)下使用AudioRecord。
- 構(gòu)造AudioRecord
AudioRecord類的構(gòu)造方法中有5個(gè)參數(shù)
/**
* audioSource 表示數(shù)據(jù)來(lái)源 一般為麥克風(fēng) MediaRecorder.AudioSource.MIC
* sampleRateInHz 表示采樣率 一般設(shè)置為 44100
* channelConfig 表示聲道 一般設(shè)置為 AudioFormat.CHANNEL_IN_MONO
* audioFormat 數(shù)據(jù)編碼方式 這里使用 AudioFormat.ENCODING_PCM_16BIT
* bufferSizeInBytes 數(shù)據(jù)大小 這里使用AudioRecord.getMinBufferSize 獲取?
*/
AudioRecord(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat,
? ? ? ? ? ? int bufferSizeInBytes)
- 創(chuàng)建AudioRecord對(duì)象
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);
? ? }
- 開始錄音
// 調(diào)用開始錄音
audioRecord.startRecording();
調(diào)用開始錄音后,需要從audioRecord中讀取錄音的數(shù)據(jù)
audioRecord.read(data, 0, recordBufsize);
- 結(jié)束錄音
audioRecord.stop();
- 釋放資源
audioRecord.release();
AudioRecord的關(guān)鍵方法已經(jīng)介紹了,下面貼上完整的代碼
二、使用步驟
清單文件聲明權(quán)限
<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" />
關(guān)于Android 6.0動(dòng)態(tài)申請(qǐng)權(quán)限這里就不重復(fù)了,可自行百度
完整代碼
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", "創(chuàng)建錄音文件->" + 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", "寫錄音數(shù)據(jù)->" + 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="結(jié)束錄音" /> </LinearLayout>
運(yùn)行程序,點(diǎn)擊開始錄音,本地會(huì)創(chuàng)建test.pcm文件并寫入錄音數(shù)據(jù)。
總結(jié)
通過(guò)上面的學(xué)習(xí),對(duì)AudioRecord有初步的了解,能使用AudioRecord來(lái)實(shí)現(xiàn)錄音功能保存到本地。
原文鏈接:https://blog.csdn.net/zhe_ge_sha_shou/article/details/112393401
相關(guān)推薦
- 2022-05-20 python循環(huán)控制之break和continue流程控制語(yǔ)句_python
- 2022-11-02 Qt5.14.2使用虛擬鍵盤的關(guān)鍵代碼_C 語(yǔ)言
- 2022-08-21 Golang中slice刪除元素的性能對(duì)比_Golang
- 2022-03-24 shell腳本查看k8s日志介紹_linux shell
- 2022-06-25 EF?Core的CRUD(增刪改查)基本操作_實(shí)用技巧
- 2022-03-26 C語(yǔ)言宏定義#define的使用_C 語(yǔ)言
- 2022-06-07 關(guān)于python調(diào)用c++動(dòng)態(tài)庫(kù)dll時(shí)的參數(shù)傳遞問(wèn)題_C 語(yǔ)言
- 2022-05-12 Kotlin set集合去重,獲取元素可變set集合,set與list轉(zhuǎn)換
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支