录音及播放的方法如下:
package com.example.audiorecord; import java.io.File; import java.io.IOException; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class RecordActivity extends Activity { private static final String LOG_TAG = "AudioRecordTest"; // 语音文件保存路径 private String FileName = null; // 界面控件 private Button startRecord; private Button startPlay; private Button stopRecord; private Button stopPlay; // 语音操作对象 private MediaPlayer mPlayer = null; private MediaRecorder mRecorder = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 开始录音 startRecord = (Button) findViewById(R.id.startRecord); startRecord.setText(R.string.startRecord); // 绑定监听器 startRecord.setOnClickListener(new startRecordListener()); // 结束录音 stopRecord = (Button) findViewById(R.id.stopRecord); stopRecord.setText(R.string.stopRecord); stopRecord.setOnClickListener(new stopRecordListener()); // 开始播放 startPlay = (Button) findViewById(R.id.startPlay); startPlay.setText(R.string.startPlay); // 绑定监听器 startPlay.setOnClickListener(new startPlayListener()); // 结束播放 stopPlay = (Button) findViewById(R.id.stopPlay); stopPlay.setText(R.string.stopPlay); stopPlay.setOnClickListener(new stopPlayListener()); // 设置sdcard的路径 FileName = Environment.getExternalStorageDirectory().getAbsolutePath(); FileName = FileName + File.separator + "audiorecordtest.arm"; } // 开始录音 class startRecordListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); mRecorder.setOutputFile(FileName); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); try { mRecorder.prepare(); } catch (IOException e) { Log.e(LOG_TAG, "prepare() failed"); } mRecorder.start(); } } // 停止录音 class stopRecordListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub mRecorder.stop(); mRecorder.release(); mRecorder = null; } } // 播放录音 class startPlayListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub mPlayer = new MediaPlayer(); try { mPlayer.setDataSource(FileName); mPlayer.prepare(); mPlayer.start(); } catch (IOException e) { Log.e(LOG_TAG, "播放失败"); } } } // 停止播放录音 class stopPlayListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub mPlayer.release(); mPlayer = null; } } }
界面布局如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/app_name" /> <Button android:id="@+id/startRecord" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/stopRecord" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/startPlay" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/stopPlay" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
完整项目代码: http://download.csdn.net/detail/abc13939746593/6920293