Android之音頻錄音


必須在AndroidManifest中設置相應的權限:android:name="android.permission.RECORD_AUDIO" 

1. 首先判定是否存在SD卡,並得到相應的路徑 

/* 檢測是否存在SD卡 */  

if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))

{

/* 得到SD卡得路徑 */

mRecAudioPath = Environment.getExternalStorageDirectory();

/* 更新所有錄音文件到List中 */

musicList(); }

2. 錄音開始

/* 創建錄音文件,第一個參數是文件名前綴,第二個參數是后綴,第三個參數是SD路徑 */  

mRecAudioFile = File.createTempFile(strTempFile, ".amr", mRecAudioPath);

/* 實例化MediaRecorder對象 */

mMediaRecorder = new MediaRecorder();

/* 設置麥克風 */

mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

/* 設置輸出文件的格式 */

mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);

/* 設置音頻文件的編碼 */

mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

/* 設置輸出文件的路徑 */

mMediaRecorder.setOutputFile(mRecAudioFile.getAbsolutePath());

/* 准備 */

mMediaRecorder.prepare();

/* 開始 */

mMediaRecorder.start();

 

3. 錄音關閉

mMediaRecorder.stop(); 

4. 播放錄音文件

Intent intent = new Intent();

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
/* 設置文件類型 */
intent.setDataAndType(Uri.fromFile(file), "audio");
startActivity(intent);

 

5. 過濾文件類型,實現FilenameFilter

class MusicFilter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return (name.endsWith(".amr"));
}

 

 

 代碼下載:/Files/lee0oo0/Examples_07_05.rar


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM