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

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

android 通過MediaRecorder實現簡單的錄音示例

2019-12-12 03:36:29
字體:
來源:轉載
供稿:網友

整理文檔,搜刮出一個android 通過MediaRecorder實現簡單的錄音示例,稍微整理精簡一下做下分享。

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步:設置音頻來源(MIC表示麥克風)    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);    //第2步:設置音頻輸出格式(默認的輸出格式)    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);    //第3步:設置音頻編碼方式(默認的編碼方式)    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);    //創(chuàng)建一個臨時的音頻輸出文件//    audioFile = File.createTempFile("record_", ".amr");    if (FileUtils.makeFolder("VOICE")){//一個簡單的判斷文件夾是不是存在 不存在就創(chuàng)建      String path = Environment.getExternalStorageDirectory().toString() + "/" + "VOICE/";      String filePath =path+"record_.amr";      File file = new File(filePath);      //第4步:指定音頻輸出文件      mediaRecorder.setOutputFile(file.getAbsolutePath());      //第5步:調用prepare方法      mediaRecorder.prepare();      //第6步:調用start方法開始錄音      mediaRecorder.start();    }  }  /**   * 關閉按鈕   * @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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 望江县| 乌恰县| 阿图什市| 邢台县| 深圳市| 旅游| 永济市| 博客| 柏乡县| 聂荣县| 广元市| 临朐县| 司法| 西乌珠穆沁旗| 东海县| 大宁县| 宁远县| 井陉县| 永德县| 象州县| 贵南县| 洞头县| 那坡县| 江孜县| 喀喇沁旗| 威海市| 高雄市| 交口县| 彩票| 时尚| 云龙县| 宜州市| 永春县| 张家港市| 丽水市| 望都县| 比如县| 临城县| 新郑市| 沾益县| 洪洞县|