網站首頁 編程語言 正文
本文實例為大家分享了Android利用MediaRecorder實現錄音功能?的具體代碼,供大家參考,具體內容如下
android用手機錄音保存到sd卡中;
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" ? ? android:orientation="vertical" ? ? tools:context=".MainActivity"> ? ?<Button ? ? ? ?android:id="@+id/bt_start" ? ? ? ?android:layout_width="match_parent" ? ? ? ?android:text="start" ? ? ? ?android:layout_height="wrap_content"></Button> ? ? <Button ? ? ? ? android:id="@+id/bt_end" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="stop"></Button> </LinearLayout>
1.準備保存文件的路徑及文件;
2.創建MediaRecorder對象,
3.調用MediaRecorder的start方法;
4.結束錄音
5.調用MediaRecorder的stop方法;
6.釋放資源;
開始錄音:
private void startRecord(){ ? ? ? ? if (recorder==null){ ? ? ? ? ? ? File dir = new File(Environment.getExternalStorageDirectory(),"sound"); ? ? ? ? ? ? if (!dir.exists()){ ? ? ? ? ? ? ? ? dir.mkdir(); ? ? ? ? ? ? } ? ? ? ? ? ? File file=new File(dir,System.currentTimeMillis()+".amr"); ? ? ? ? ? ? if (!file.exists()){ ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? file.createNewFile(); ? ? ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? recorder =new MediaRecorder(); ? ? ? ? ? ? recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//輸入源通過話筒錄音; ? ? ? ? ? ? recorder.setOutputFormat(MediaRecorder.AudioEncoder.AMR_WB);//輸出格式 ? ? ? ? ? ? recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);//音頻編碼 ? ? ? ? ? ? recorder.setOutputFile(file.getAbsolutePath());//設置寫出文件; ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? recorder.prepare(); ? ? ? ? ? ? ? ? recorder.start(); ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? } ? ? }
結束錄音:
private void endRecord(){ ? ? ? ? if (recorder!=null){ ? ? ? ? ? ? recorder.stop(); ? ? ? ? ? ? recorder.release(); ? ? ? ? ? ? recorder=null; ? ? ? ? } ? ? }
具體代碼實現:
package com.example.record; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; import java.io.File; import java.io.IOException; public class MainActivity extends AppCompatActivity { ? ? private Button bt_1,bt2; ? ? private MediaRecorder recorder ; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? findViewById(R.id.bt_start).setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? startRecord(); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? findViewById(R.id.bt_end).setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? endRecord(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? private void startRecord(){ ? ? ? ? if (recorder==null){ ? ? ? ? ? ? File dir = new File(Environment.getExternalStorageDirectory(),"sound"); ? ? ? ? ? ? if (!dir.exists()){ ? ? ? ? ? ? ? ? dir.mkdir(); ? ? ? ? ? ? } ? ? ? ? ? ? File file=new File(dir,System.currentTimeMillis()+".amr"); ? ? ? ? ? ? if (!file.exists()){ ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? file.createNewFile(); ? ? ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? recorder =new MediaRecorder(); ? ? ? ? ? ? recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//輸入源通過話筒錄音; ? ? ? ? ? ? recorder.setOutputFormat(MediaRecorder.AudioEncoder.AMR_WB);//輸出格式 ? ? ? ? ? ? recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);//音頻編碼 ? ? ? ? ? ? recorder.setOutputFile(file.getAbsolutePath());//設置寫出文件; ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? recorder.prepare(); ? ? ? ? ? ? ? ? recorder.start(); ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? private void endRecord(){ ? ? ? ? if (recorder!=null){ ? ? ? ? ? ? recorder.stop(); ? ? ? ? ? ? recorder.release(); ? ? ? ? ? ? recorder=null; ? ? ? ? } ? ? } }
最后記得添加權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
寫入文件的權限,調用錄音的權限
原文鏈接:https://blog.csdn.net/xujingqing/article/details/106035905
相關推薦
- 2023-07-26 node中的內置模塊path和fs
- 2022-06-25 React服務端渲染和同構的實現_React
- 2022-11-29 Mybatis中如何傳入map參數呢?
- 2022-10-16 淺談Python3中打開文件的方式(With?open)_python
- 2022-02-13 如何寫一個自己的strcpy函數
- 2022-05-04 python工具dtreeviz決策樹可視化和模型可解釋性_python
- 2022-06-11 Python?tkinter庫繪圖實例分享_python
- 2022-09-04 python開發任意表達式求值全功能示例_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支