1、Android 的多媒体框架包括了对各种通用音频格式进行采集和编码的支持,因此你能够容易的把音频集成到你的应用程序中。如果设备硬件支持,你还能够使用 MediaRecorder API来记录音频。本文向你展示如何编写从设备的麦克中采集音频、保存并播放它的应用程序。注意:Android 模拟器没有采集音频的能力,但是实际的设备可能提供这种能力。执行音频采集从设备上采集音频比音视频播放要复杂一些,但依然相当简单:1. 创建一个新的 android.media.MediaRecorder 类的实例;2. 使用 MediaRecorder.setAudioSource()方法设置音频源。你可能还想使用
2、MediaRecorder.AudioSource.MIC;3. 使用 MediaRecorder.setOutputFormat()方法设置输出文件格式;4. 使用 MediaRecorder.setOutputFile()方法设置输出文件名;5. 使用 MediaRecorder.setAudioEncoder()方法设置音频编码;6. 调用 MediaRecorder 实例上的 MediaRecorder.prepare()方法;7. 调用 MediaRecorder.start()方法启动音频采集;8. 调用 MediaRecorder.stop()方法终止音频采集;9. 使用完 Me
3、diaRecorder 实例后,调用它的 MediaRecorder.release()方法。要始终调用这个方法来立即释放资源。示例:录音和播放录音这个例子的类演示了如何建立、启动和终止音频采集,并且播放录音文件。/* The application needs to have the permission to write to external storage* if the output file is written to the external storage, and also the* permission to record audio. These permissions
4、must be set in the* applications AndroidManifest.xml file, with something like:* * */package com.android.audiorecordtest;import android.app.Activity;import android.widget.LinearLayout;import android.os.Bundle;import android.os.Environment;import android.view.ViewGroup;import android.widget.Button;im
5、port android.view.View;import android.view.View.OnClickListener;import android.content.Context;import android.util.Log;import android.media.MediaRecorder;import android.media.MediaPlayer;import java.io.IOException;public class AudioRecordTest extends Activityprivate static final String LOG_TAG = “Au
6、dioRecordTest“;private static String mFileName = null;private RecordButton mRecordButton = null;private MediaRecorder mRecorder = null;private PlayButton mPlayButton = null;private MediaPlayer mPlayer = null;private void onRecord(boolean start) if (start) startRecording(); else stopRecording();priva
7、te void onPlay(boolean start) if (start) startPlaying(); else stopPlaying();private void startPlaying() mPlayer = new MediaPlayer();try mPlayer.setDataSource(mFileName);mPlayer.prepare();mPlayer.start(); catch (IOException e) Log.e(LOG_TAG, “prepare() failed“);private void stopPlaying() mPlayer.rele
8、ase();mPlayer = null;private void startRecording() mRecorder = new MediaRecorder();mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);mRecorder.setOutputFile(mFileName);mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);t
9、ry mRecorder.prepare(); catch (IOException e) Log.e(LOG_TAG, “prepare() failed“);mRecorder.start();private void stopRecording() mRecorder.stop();mRecorder.release();mRecorder = null;class RecordButton extends Button boolean mStartRecording = true;OnClickListener clicker = new OnClickListener() publi
10、c void onClick(View v) onRecord(mStartRecording);if (mStartRecording) setText(“Stop recording“); else setText(“Start recording“);mStartRecording = !mStartRecording;public RecordButton(Context ctx) super(ctx);setText(“Start recording“);setOnClickListener(clicker);class PlayButton extends Button boole
11、an mStartPlaying = true;OnClickListener clicker = new OnClickListener() public void onClick(View v) onPlay(mStartPlaying);if (mStartPlaying) setText(“Stop playing“); else setText(“Start playing“);mStartPlaying = !mStartPlaying;public PlayButton(Context ctx) super(ctx);setText(“Start playing“);setOnC
12、lickListener(clicker);public AudioRecordTest() mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();mFileName += “/audiorecordtest.3gp“;Overridepublic void onCreate(Bundle icicle) super.onCreate(icicle);LinearLayout ll = new LinearLayout(this);mRecordButton = new RecordButton(this
13、);ll.addView(mRecordButton,new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,0);mPlayButton = new PlayButton(this);ll.addView(mPlayButton,new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,0);setContentView(ll);Overridepublic void onPause() super.onPause();if (mRecorder != null) mRecorder.release();mRecorder = null;if (mPlayer != null) mPlayer.release();mPlayer = null;