android开发文档中有一个关于录音的类MediaRecord,一张图介绍了基本的流程:


给出了一个常用的例子:
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

我在这里实现了一个简单的程序,过程和上述类似,录音以及录音的播放。 1.基本界面如下:

2.工程中各文件内容如下: 2.1 Activity——RecordActivity
package com.cxf;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 += "/audiorecordtest.3gp";    }    //开始录音    class startRecordListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stub mRecorder = new MediaRecorder();     mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);     mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);     mRecorder.setOutputFile(FileName);     mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);     try {         mRecorder.prepare();     } catch (IOException e) {         Log.e(LOG_TAG, "prepare() failed");     }     mRecorder.start();}        }    //停止录音    class stopRecordListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stub mRecorder.stop();     mRecorder.release();     mRecorder = null;}        }    //播放录音    class startPlayListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubmPlayer = new MediaPlayer();try{mPlayer.setDataSource(FileName);mPlayer.prepare();mPlayer.start();}catch(IOException e){Log.e(LOG_TAG,"播放失败");}}        }    //停止播放录音    class stopPlayListener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubmPlayer.release();        mPlayer = null;}        }}
2.2 main.xml
<?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/hello" /><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>
2.3 Manifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.cxf"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="4" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name=".RecordActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>                    </activity>           </application>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>    <uses-permission android:name="android.permission.RECORD_AUDIO" /></manifest>
2.4 string.xml
<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello"></string>    <string name="app_name">Record</string><string name="startRecord">开始录音</string><string name="stopRecord">结束录音</string><string name="startPlay">开始播放</string><string name="stopPlay">结束播放</string></resources>


更多相关文章

  1. Android之SurfaceView实现视频播放
  2. Android之SurfaceView实现视频播放
  3. Android两种播放视频的方法(SurfaceView、MediaPlayer、SeekBar)
  4. android 视频播放器的INTENT-FILTER属性
  5. [置顶] 我的Android进阶之旅------>Android(安卓)MediaPlayer播
  6. .NET 开源了,Visual Studio 开始支持 Android(安卓)和 iOS 程序编
  7. Android(安卓)Flash 10.1与Lite版视频播放能力对比
  8. Android程序开发初级教程(一) 开始 Hello Android
  9. Android(安卓)来去电自动录音(一)

随机推荐

  1. android 获取通讯录联系人信息
  2. AndroidMainfest.xml
  3. ADT 不能在线更新问题
  4. Android Studio 3.0 Canary 8无法安装apk
  5. Android 学习之Layout
  6. android adb shell dumpsys
  7. Android通过openGL实现视频贴纸功能
  8. android利用反射来解决版本兼容问题。
  9. Android中的文件上传下载
  10. Android Studio 使用config.gradle 管理