android 通過MediaRecorder實(shí)現(xiàn)簡單的錄音示例
整理文檔,搜刮出一個(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android MediaRecorder實(shí)現(xiàn)錄屏?xí)r帶錄音功能
- Android編程錄音工具類RecorderUtil定義與用法示例
- Android實(shí)現(xiàn)錄音功能實(shí)現(xiàn)實(shí)例(MediaRecorder)
- Android使用AudioRecord實(shí)現(xiàn)暫停錄音功能實(shí)例代碼
- Android錄音--AudioRecord、MediaRecorder的使用
- Android使用MediaRecorder實(shí)現(xiàn)錄音及播放
- Android App調(diào)用MediaRecorder實(shí)現(xiàn)錄音功能的實(shí)例
- Android音頻錄制MediaRecorder之簡易的錄音軟件實(shí)現(xiàn)代碼
- Android簡單的利用MediaRecorder進(jìn)行錄音的實(shí)例代碼
- Android使用AudioRecord實(shí)現(xiàn)錄音功能
相關(guān)文章
Android自定義View實(shí)現(xiàn)拖拽效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)拖拽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android中通過view方式獲取當(dāng)前Activity的屏幕截圖實(shí)現(xiàn)方法
這篇文章主要介紹了Android中通過view方式獲取當(dāng)前Activity的屏幕截圖實(shí)現(xiàn)方法,本文方法相對簡單,容易理解,需要的朋友可以參考下2014-09-09
Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯(cuò)誤解決方案
這篇文章主要介紹了 Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯(cuò)誤解決方案的相關(guān)資料,需要的朋友可以參考下2016-12-12
Android 7.0行為變更 FileUriExposedException解決方法
這篇文章主要介紹了Android 7.0行為變更 FileUriExposedException解決方法的相關(guān)資料,需要的朋友可以參考下2017-05-05
android開發(fā) eclipse alt+”/”自動(dòng)提示失效的解決方法
最近在學(xué)習(xí)android開發(fā)布局這塊。第一次學(xué)習(xí),很多代碼不熟悉。所以自動(dòng)提示對我來說很重要。但悲催的就是這個(gè)自動(dòng)提示失效。今天在網(wǎng)上搜索了一下解決辦法,主要有一下幾種方法2014-05-05

