国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

android 通過MediaRecorder實(shí)現(xiàn)簡單的錄音示例

2019-10-23 19:45:08
字體:
供稿:網(wǎng)友

整理文檔,搜刮出一個(gè)android 通過MediaRecorder實(shí)現(xiàn)簡單的錄音示例,稍微整理精簡一下做下分享。

MainActivity

package com.centaur.collectvoice;import android.media.MediaRecorder;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Toast;import java.io.File;import java.io.IOException;public class MainActivity extends AppCompatActivity {  private final static String TAG = "collectvoice";  MediaRecorder mediaRecorder = new MediaRecorder();  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);  }  /**   * 開始按鈕   * @param view   * @throws IOException   */  public void onStart(View view) throws IOException {    Toast.makeText(this, "開始收集", Toast.LENGTH_SHORT).show();    // 第1步:設(shè)置音頻來源(MIC表示麥克風(fēng))    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);    //第2步:設(shè)置音頻輸出格式(默認(rèn)的輸出格式)    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);    //第3步:設(shè)置音頻編碼方式(默認(rèn)的編碼方式)    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);    //創(chuàng)建一個(gè)臨時(shí)的音頻輸出文件//    audioFile = File.createTempFile("record_", ".amr");    if (FileUtils.makeFolder("VOICE")){//一個(gè)簡單的判斷文件夾是不是存在 不存在就創(chuàng)建      String path = Environment.getExternalStorageDirectory().toString() + "/" + "VOICE/";      String filePath =path+"record_.amr";      File file = new File(filePath);      //第4步:指定音頻輸出文件      mediaRecorder.setOutputFile(file.getAbsolutePath());      //第5步:調(diào)用prepare方法      mediaRecorder.prepare();      //第6步:調(diào)用start方法開始錄音      mediaRecorder.start();    }  }  /**   * 關(guān)閉按鈕   * @param view   */  public void onStop(View view) {    Toast.makeText(this, "停止收集", Toast.LENGTH_SHORT).show();    mediaRecorder.stop();  }}

工具類中用到的方法

 public static boolean makeFolder(String folder){    File filefolder = new File(Environment.getExternalStorageDirectory().toString() + "/" + folder);    if(!filefolder.exists()){      filefolder.mkdir();      if(filefolder.exists()){        Log.d(TAG,folder+"創(chuàng)建成功");      }      else {        Log.d(TAG,folder+"創(chuàng)建失敗");      }    }    return true;  }

布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:id="@+id/activity_main"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  android:paddingBottom="@dimen/activity_vertical_margin"  android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  tools:context="com.centaur.collectvoice.MainActivity">  <Button    android:onClick="onStart"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="收集聲音" />  <Button    android:onClick="onStop"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="停止聲音" /></LinearLayout>

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 姜堰市| 东乡县| 香格里拉县| 仙居县| 灵丘县| 衢州市| 始兴县| 定兴县| 涡阳县| 青州市| 会昌县| 城固县| 渝北区| 蕲春县| 兴宁市| 富阳市| 广丰县| 东兰县| 文成县| 长顺县| 紫金县| 海原县| 南投市| 无为县| 耒阳市| 饶河县| 夏津县| 民县| 谷城县| 巍山| 若羌县| 延边| 富蕴县| 尼木县| 淳安县| 宁明县| 沾化县| 呼伦贝尔市| 梅河口市| 海伦市| 阿拉尔市|