Android一個(gè)類(lèi)實(shí)現(xiàn)錄音與播放實(shí)例
前言
最近混合開(kāi)發(fā)的項(xiàng)目 在做語(yǔ)音識(shí)別時(shí) h5拿不到麥克風(fēng)的權(quán)限
幾經(jīng)周折 開(kāi)發(fā)任務(wù)又落到了原生開(kāi)發(fā)這里
先寫(xiě)一個(gè)demo實(shí)現(xiàn)錄音和播放功能 然后由web端同事調(diào)用交互方法
實(shí)現(xiàn)效果
代碼實(shí)現(xiàn)
public class MainActivity extends AppCompatActivity { private static final String LOG_TAG = "MainActivity"; //語(yǔ)音文件保存路徑 private String FileName = null; //界面控件 private Button startRecord; private Button startPlay; private Button stopRecord; private Button stopPlay; //語(yǔ)音操作對(duì)象 private MediaPlayer mPlayer = null; private MediaRecorder mRecorder = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); requestPermission();//請(qǐng)求麥克風(fēng)權(quán)限 //開(kāi)始錄音 startRecord = findViewById(R.id.startRecord); //綁定監(jiān)聽(tīng)器 startRecord.setOnClickListener(new startRecordListener()); //結(jié)束錄音 stopRecord = findViewById(R.id.stopRecord); stopRecord.setOnClickListener(new stopRecordListener()); //開(kāi)始播放 startPlay = findViewById(R.id.startPlay); //綁定監(jiān)聽(tīng)器 startPlay.setOnClickListener(new startPlayListener()); //結(jié)束播放 stopPlay = findViewById(R.id.stopPlay); stopPlay.setOnClickListener(new stopPlayListener()); //設(shè)置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(); } //開(kāi)始錄音 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="開(kāi)始錄音" 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="結(jié)束錄音" 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="開(kāi)始播放" 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="結(jié)束播放" app:layout_constraintTop_toBottomOf="@id/startPlay" /> </androidx.constraintlayout.widget.ConstraintLayout>
靜態(tài)權(quán)限
<!--錄音和寫(xiě)磁盤(pán)權(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" />
動(dòng)態(tài)權(quán)限
PermissionGen.with(this) .addRequestCode(100) .permissions(Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WAKE_LOCK) .request();
總結(jié)
到此這篇關(guān)于Android一個(gè)類(lèi)實(shí)現(xiàn)錄音與播放實(shí)例的文章就介紹到這了,更多相關(guān)Android實(shí)現(xiàn)錄音與播放內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解Android開(kāi)發(fā)錄音和播放音頻的步驟(動(dòng)態(tài)獲取權(quán)限)
- Android實(shí)現(xiàn)語(yǔ)音播放與錄音功能
- Android編程實(shí)現(xiàn)錄音及保存播放功能的方法【附demo源碼下載】
- android語(yǔ)音即時(shí)通訊之錄音、播放功能實(shí)現(xiàn)代碼
- Android 錄音與播放功能的簡(jiǎn)單實(shí)例
- Android使用MediaRecorder實(shí)現(xiàn)錄音及播放
- Android錄音播放管理工具
- Android實(shí)現(xiàn)自制和播放錄音程序
- Android編程開(kāi)發(fā)錄音和播放錄音簡(jiǎn)單示例
- Android實(shí)現(xiàn)音頻錄音與播放
相關(guān)文章
Android系統(tǒng)自帶樣式 (android:theme)
Android系統(tǒng)中自帶樣式分享,需要的朋友可以參考下2013-01-01Android實(shí)現(xiàn)授權(quán)訪問(wèn)網(wǎng)頁(yè)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)授權(quán)訪問(wèn)網(wǎng)頁(yè)的方法,需要的朋友可以參考下2014-07-07Android 創(chuàng)建/驗(yàn)證/刪除桌面快捷方式(已測(cè)試可用)
桌面快捷方式的出現(xiàn)方便了用戶操作,在某些程度上提高了用戶體驗(yàn),接下來(lái)將介紹下Android創(chuàng)建/驗(yàn)證/刪除桌面快捷方式的實(shí)現(xiàn)思路及代碼,感興趣的朋友可以了解下,或許本文可以幫助到你2013-02-02Android開(kāi)發(fā)之文本內(nèi)容自動(dòng)朗讀功能實(shí)現(xiàn)方法
這篇文章主要介紹了Android開(kāi)發(fā)之文本內(nèi)容自動(dòng)朗讀功能實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android自動(dòng)朗讀TTS功能的操作步驟、相關(guān)函數(shù)使用方法與注意事項(xiàng),需要的朋友可以參考下2017-09-09Android SDK Manager無(wú)法更新問(wèn)題解決辦法
這篇文章主要介紹了Android SDK Manager無(wú)法更新問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04Android 如何獲取手機(jī)總內(nèi)存和可用內(nèi)存等信息
這篇文章主要介紹了Android系統(tǒng)檢測(cè)程序內(nèi)存占用各種方法,并對(duì)內(nèi)存信息的詳細(xì)介紹,需要的朋友可以參考下2016-07-07android 關(guān)于webview 加載h5網(wǎng)頁(yè)開(kāi)啟定位的方法
今天小編就為大家分享一篇android 關(guān)于webview 加載h5網(wǎng)頁(yè)開(kāi)啟定位的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Android studio2.3.3升級(jí)到3.1.2坑(小記)
這篇文章主要介紹了Android studio2.3.3升級(jí)3.1.2坑(小記),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Spi機(jī)制在Android開(kāi)發(fā)的應(yīng)用示例詳解
這篇文章主要為大家介紹了Spi機(jī)制在Android開(kāi)發(fā)的應(yīng)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08