網站首頁 編程語言 正文
前言
最近混合開發的項目 在做語音識別時 h5拿不到麥克風的權限
幾經周折 開發任務又落到了原生開發這里
先寫一個demo實現錄音和播放功能 然后由web端同事調用交互方法
實現效果
代碼實現
public class MainActivity extends AppCompatActivity { private static final String LOG_TAG = "MainActivity"; //語音文件保存路徑 private String FileName = null; //界面控件 private Button startRecord; private Button startPlay; private Button stopRecord; private Button stopPlay; //語音操作對象 private MediaPlayer mPlayer = null; private MediaRecorder mRecorder = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); requestPermission();//請求麥克風權限 //開始錄音 startRecord = findViewById(R.id.startRecord); //綁定監聽器 startRecord.setOnClickListener(new startRecordListener()); //結束錄音 stopRecord = findViewById(R.id.stopRecord); stopRecord.setOnClickListener(new stopRecordListener()); //開始播放 startPlay = findViewById(R.id.startPlay); //綁定監聽器 startPlay.setOnClickListener(new startPlayListener()); //結束播放 stopPlay = findViewById(R.id.stopPlay); stopPlay.setOnClickListener(new stopPlayListener()); //設置sdcard的路徑 FileName = Environment.getExternalStorageDirectory().getAbsolutePath(); FileName += "/test.mp3"; } private void requestPermission() { PermissionGen.with(this) .addRequestCode(100) .permissions(Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WAKE_LOCK) .request(); } //開始錄音 class startRecordListener implements View.OnClickListener { @Override public void onClick(View v) { mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setOutputFile(FileName); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { mRecorder.prepare(); mRecorder.start(); } catch (IOException e) { Log.e(LOG_TAG, "prepare() failed ---" + e.getMessage()); } } } //停止錄音 class stopRecordListener implements View.OnClickListener { @Override public void onClick(View v) { mRecorder.stop(); mRecorder.release(); mRecorder = null; } } //播放錄音 class startPlayListener implements View.OnClickListener { @Override public void onClick(View v) { mPlayer = new MediaPlayer(); try { mPlayer.setDataSource(FileName); mPlayer.prepare(); mPlayer.start(); } catch (IOException e) { Log.e(LOG_TAG, "播放失敗"); } } } //停止播放錄音 class stopPlayListener implements View.OnClickListener { @Override public void onClick(View v) { mPlayer.release(); mPlayer = null; } } }
XML
<?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" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/startRecord" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="100dp" android:text="開始錄音" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/stopRecord" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="結束錄音" app:layout_constraintBottom_toTopOf="@id/startPlay" app:layout_constraintTop_toBottomOf="@id/startRecord" /> <Button android:id="@+id/startPlay" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="開始播放" app:layout_constraintBottom_toTopOf="@id/stopPlay" app:layout_constraintTop_toBottomOf="@id/stopRecord" /> <Button android:id="@+id/stopPlay" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="結束播放" app:layout_constraintTop_toBottomOf="@id/startPlay" /> </androidx.constraintlayout.widget.ConstraintLayout>
靜態權限
<!--錄音和寫磁盤權限--> <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" />
動態權限
PermissionGen.with(this) .addRequestCode(100) .permissions(Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WAKE_LOCK) .request();
總結
原文鏈接:https://blog.csdn.net/Life_s/article/details/122843630
相關推薦
- 2023-04-02 GoLang函數棧的使用詳細講解_Golang
- 2022-09-19 Android基于OkHttp實現文件上傳功能_Android
- 2022-06-16 golang默認Logger日志庫在項目中使用Zap日志庫_Golang
- 2022-06-23 python入門語句基礎之if語句、while語句_python
- 2022-10-03 Pandas數據集的分塊讀取的實現_python
- 2022-03-18 Android?Activity生命周期調用的理解_Android
- 2022-11-29 箭頭函數中this與call()方法的關系
- 2022-06-29 基于C++實現五子棋小游戲_C 語言
- 最近更新
-
- 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同步修改后的遠程分支