Android录音与播放

转载自::http://blog.csdn.net/nkmnkm/article/details/7804153

Android多媒体框架包含获取和编码多种音频格式的支持,所以你可以轻松地把音频合并到你的应用中.如果设备支持,你可以使用MediaRecorder APIs 进行录音.

本章向你展示如何写一个应用从设备上的microphone获取音频,然后保存并回放.

注:Android模拟器不具有录音的能力,但是真实的设备一般都具有此功能.


执行音频获取

从设备获取音频比回放音频或视频要复杂一点,但是也还算简单:

  • 创建一个android.media.MediaRecorder的新实例.
  • 使用MediaRecorder.setAudioSource()设置音频源,一般要使用MediaRecorder.AudioSource.MIC.
  • 使用MediaRecorder.setOutputFormat()设置输出文件的格式.
  • 使用MediaRecorder.setOutputFile()设置输出文件的名字.
  • 使用MediaRecorder.setAudioEncoder()设置音频编码.
  • 调用MediaRecorder 实例的MediaRecorder.prepare()
  • MediaRecorder.start()开始获取音频.
  • 调用MediaRecorder.stop().停止.
  • 当你用完MediaRecorder实例后,调用MediaRecorder.release(),就会立即释放资源.


示例:录音并回放录音

下面的例子演示了如何设置,开始以及停止音频获取,以及回放录制的文件.


 * 如果输出文件被写入外部存储, 
 * 本应用需要具有写外部存储的权限,  
 * 还要具有录音的权限.这些权限必须 
 * 在AndroidManifest.xml 文件中声明,像这样: 
 * 
 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
 * <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
 * 
 */  
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;  
import 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 Activity  
{  
    private static final String LOG_TAG = "AudioRecordTest";  
    private static String mFileName = null;  
   //录音按钮  
    private RecordButton mRecordButton = null;  
    private MediaRecorder mRecorder = null;  
   //回放按钮  
    private PlayButton   mPlayButton = null;  
    private MediaPlayer   mPlayer = null;  
  
   //当录音按钮被click时调用此方法,开始或停止录音  
    private void onRecord(boolean start) {  
        if (start) {  
            startRecording();  
        } else {  
            stopRecording();  
        }  
    }  
  
   //当播放按钮被click时调用此方法,开始或停止播放  
    private 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.release();  
        mPlayer = null;  
    }  
  
    private void startRecording() {  
        mRecorder = new MediaRecorder();  
        //设置音源为Micphone  
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);  
        //设置封装格式  
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);  
        mRecorder.setOutputFile(mFileName);  
        //设置编码格式  
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);  
  
        try {  
            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() {  
            public 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 {  
        boolean 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");  
            setOnClickListener(clicker);  
        }  
    }  
  
    //构造方法  
    public AudioRecordTest() {  
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();  
        mFileName += "/audiorecordtest.3gp";  
    }  
  
    @Override  
    public void onCreate(Bundle icicle) {  
        super.onCreate(icicle);  
        //构造界面  
        LinearLayout ll = new LinearLayout(this);  
        mRecordButton = new RecordButton(this);  
        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);  
    }  
  
    @Override  
    public void onPause() {  
        super.onPause();  
        //Activity暂停时释放录音和播放对象  
        if (mRecorder != null) {  
            mRecorder.release();  
            mRecorder = null;  
        }  
  
        if (mPlayer != null) {  
            mPlayer.release();  
            mPlayer = null;  
        }  
    }  
}  




你可能感兴趣的:(Android录音与播放)