Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android MediaRecorder錄制音頻

android MediaRecorder錄制音頻

編輯:關於Android編程

\

使用MediaRecorder錄制音頻步驟:

創建MediaRecorder對象<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+tffTw01lZGlhUmVjb3JkZXK1xHNldEF1ZGlvU291cmNlKCm3vbeoyejWw8n50vTAtNS0o6zSu7DjtKvI601lZGlhUmVjb3JkZXIuQXVkaW9Tb3VyY2UuTUlDss7K/da4tqjCvNbGwLTX1MLzv8u357XEyfnS9DwvcD4KPHA+tffTw01lZGlhUmVjb3JkZXK21M/ztcRzZXRPdXRwdXRGb3JtYXQoKcno1sPL+cK81sa1xNL0xrUgzsS8/rXEJiMyNjY4NDvKvTwvcD4KPHA+tffTw01lZGlhUmVjb3JkZXK21M/ztcRzZXRBdWRpb0VuY29kZXIoKSAsc2V0QXVkaW9FbmNvZGluZ0JpdFJhdGUoaW50IGJpdFJhdGUpLHNldEF1ZGlvU2FtcGxpbmdSYXRlKGludCBzYW1wbGluZ1JhdGUpyejWw8v5wrzWxrXEyfnS9LXEseDC6yYjMjY2ODQ7yr2jrLHgwuvOu8LKo6yyyTwvcD4KPHA+0fnCyrXIo6zV4tCpss7K/b2rv8nS1L/Y1sbL+cK81sa1xMn50vS1xMa31sqjrM7EvP61xLTz0KEgo6zSu7DjwLTLtca31srUvbrDo6zJ+dL0zsS8/tS9tPM8L3A+CjxwPrX308NNZWRpYVJlY29yZGVytcRzZXRPdXRwdXRGaWxlKFN0cmluZyBwYXRoKbe9t6jJ6NbDIMK81sa1xNL0xrXOxLz+tcSxo7TmzrvWwzwvcD4KPHA+tffTw01lZGlhUmVjb3JkZXK1xHByZXBhcmUoKbe9t6jXvLG4wrzWxjwvcD4KPHA+tffTw01lZGlhUmVjb3JkZXK21M/ztcRzdGFydCgpt723qL+qyrzCvNbGPC9wPgo8cD7CvNbGzeqzyaOstffTw01lZGlhUmVjb3JkZXK21M/ztcRzdG9wKCm3vbeozaPWucK81sajrLKitffTw3JlbGVhc2UoKbe9t6jKzbfF18rUtDwvcD4KPHA+yOfPwqO6PC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;">MediaRecorder recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(PATH_NAME); recorder.prepare(); recorder.start(); // Recording is now started ... recorder.stop(); recorder.reset(); // You can reuse the object by going back to setAudioSource() step recorder.release(); // Now the object cannot be reused


例子程序:

Activity:

import java.io.File;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class RecordSound extends Activity
	implements OnClickListener
{
	// 定義界面上的兩個按鈕
	ImageButton record, stop;
	// 系統的音頻文件
	File soundFile;
	MediaRecorder mRecorder;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 獲取程序界面中的兩個按鈕
		record = (ImageButton) findViewById(R.id.record);
		stop = (ImageButton) findViewById(R.id.stop);
		// 為兩個按鈕的單擊事件綁定監聽器
		record.setOnClickListener(this);
		stop.setOnClickListener(this);
	}

	@Override
	public void onDestroy()
	{
		if (soundFile != null && soundFile.exists())
		{
			// 停止錄音
			mRecorder.stop();
			// 釋放資源
			mRecorder.release();
			mRecorder = null;
		}
		super.onDestroy();
	}

	@Override
	public void onClick(View source)
	{
		switch (source.getId())
		{
		// 單擊錄音按鈕
			case R.id.record:
				if (!Environment.getExternalStorageState().equals(
					android.os.Environment.MEDIA_MOUNTED))
				{
					Toast.makeText(RecordSound.this, "SD卡不存在,請插入SD卡!",
						Toast.LENGTH_SHORT).show();
					return;
				}
				try
				{
					// 創建保存錄音的音頻文件
					soundFile = new File(Environment
						.getExternalStorageDirectory().getCanonicalFile()
						+ "/sound.amr");
					mRecorder = new MediaRecorder();
					// 設置錄音的聲音來源
					mRecorder.setAudioSource(MediaRecorder
						.AudioSource.MIC);
					// 設置錄制的聲音的輸出格式(必須在設置聲音編碼格式之前設置)
					mRecorder.setOutputFormat(MediaRecorder
						.OutputFormat.THREE_GPP);
					// 設置聲音編碼的格式
					mRecorder.setAudioEncoder(MediaRecorder
						.AudioEncoder.AMR_NB);
					mRecorder.setOutputFile(soundFile.getAbsolutePath());
					mRecorder.prepare();
					// 開始錄音
					mRecorder.start();  //①
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				break;
			// 單擊停止按鈕
			case R.id.stop:
				if (soundFile != null && soundFile.exists())
				{
					// 停止錄音
					mRecorder.stop();  //②
					// 釋放資源
					mRecorder.release();  //③
					mRecorder = null;
				}
				break;
		}
	}
}

清單文件:

	
	
	
	


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved