欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年02月25日 11:29:14   作者:Big_Centaur  
本篇文章中主要介紹了android 通過MediaRecorder實(shí)現(xiàn)簡(jiǎn)單的錄音示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

整理文檔,搜刮出一個(gè)android 通過MediaRecorder實(shí)現(xiàn)簡(jiǎn)單的錄音示例,稍微整理精簡(jiǎ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è)簡(jiǎn)單的判斷文件夾是不是存在 不存在就創(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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論