Android利用MediaRecorder實(shí)現(xiàn)錄音功能
本文實(shí)例為大家分享了Android利用MediaRecorder實(shí)現(xiàn)錄音功能 的具體代碼,供大家參考,具體內(nèi)容如下
android用手機(jī)錄音保存到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.準(zhǔn)備保存文件的路徑及文件;
2.創(chuàng)建MediaRecorder對(duì)象,
3.調(diào)用MediaRecorder的start方法;
4.結(jié)束錄音
5.調(diào)用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());//設(shè)置寫出文件; ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? recorder.prepare(); ? ? ? ? ? ? ? ? recorder.start(); ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? } ? ? }
結(jié)束錄音:
private void endRecord(){ ? ? ? ? if (recorder!=null){ ? ? ? ? ? ? recorder.stop(); ? ? ? ? ? ? recorder.release(); ? ? ? ? ? ? recorder=null; ? ? ? ? } ? ? }
具體代碼實(shí)現(xiàn):
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());//設(shè)置寫出文件; ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? recorder.prepare(); ? ? ? ? ? ? ? ? recorder.start(); ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? private void endRecord(){ ? ? ? ? if (recorder!=null){ ? ? ? ? ? ? recorder.stop(); ? ? ? ? ? ? recorder.release(); ? ? ? ? ? ? recorder=null; ? ? ? ? } ? ? } }
最后記得添加權(quán)限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
寫入文件的權(quán)限,調(diào)用錄音的權(quán)限
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android MediaRecorder實(shí)現(xiàn)錄屏?xí)r帶錄音功能
- Android實(shí)現(xiàn)錄音功能實(shí)現(xiàn)實(shí)例(MediaRecorder)
- Android錄音--AudioRecord、MediaRecorder的使用
- android 通過MediaRecorder實(shí)現(xiàn)簡(jiǎn)單的錄音示例
- Android使用MediaRecorder實(shí)現(xiàn)錄音及播放
- Android App調(diào)用MediaRecorder實(shí)現(xiàn)錄音功能的實(shí)例
- Android音頻錄制MediaRecorder之簡(jiǎn)易的錄音軟件實(shí)現(xiàn)代碼
- Android簡(jiǎn)單的利用MediaRecorder進(jìn)行錄音的實(shí)例代碼
- Android實(shí)現(xiàn)語音播放與錄音功能
- Android開發(fā)四大組件之實(shí)現(xiàn)電話攔截和電話錄音
相關(guān)文章
Android 可拖動(dòng)的seekbar自定義進(jìn)度值
這篇文章主要介紹了Android 可拖動(dòng)的seekbar自定義進(jìn)度值的相關(guān)資料,有需要的朋友參考下2016-04-04基于Android SQLiteOpenHelper && CRUD 的使用
本篇文章小編為大家介紹,基于Android SQLiteOpenHelper && CRUD的使用。需要的朋友可以參考一下2013-04-04Android中關(guān)于百度糯米app關(guān)閉網(wǎng)頁或窗口的方法(99%人不知)
這篇文章主要介紹了Android中關(guān)于百度糯米app中關(guān)閉網(wǎng)頁或窗口的方法,其實(shí)解決方法到很簡(jiǎn)單,但是很多人都不知道如何解決的,在網(wǎng)上也很難找到答案的,下面小編給大家揭曉答案,需要的朋友可以參考下2016-08-08Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android View.onMeasure方法詳解及實(shí)例
這篇文章主要介紹了Android View.onMeasure方法詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05Android Drawerlayout實(shí)現(xiàn)側(cè)滑菜單效果
這篇文章主要為大家詳細(xì)介紹了Android Drawerlayout實(shí)現(xiàn)側(cè)滑菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10淺談Android開發(fā)者2017年最值得關(guān)注的25個(gè)實(shí)用庫
本篇文章主要介紹了Android開發(fā)者2017年最值得關(guān)注的25個(gè)庫,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09