android多媒体——6步学会MediaRecorder录音

MediaRecorder即录音
要想使用首先查看API,可以看到MediaRecorder的生命周期
android多媒体——6步学会MediaRecorder录音_第1张图片

API不仅给了生命周期图,还有例子如下

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

查看完API我们可以试着写一写了;
【1】搭界面:两个按钮,一个播放一个停止
【2】加点击事件
【3】别忘加权限

<uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

【4】得到MediaRecorder并设置参数

 recorder=new MediaRecorder();
            //音频来源    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
             //设置输出格式   recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                //音频编码recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
          //设置保存在sdcard下面名为  myrecord.3gp    recorder.setOutputFile(Environment.getExternalStorageDirectory() + "/myrecord.3gp");

【5】开始录制

 recorder.prepare();
                    recorder.start();

【6】停止录制

 recorder.stop();
                recorder.reset();
                recorder.release();

这样子就已经完全实现

下面是完整源码

public class MainActivity extends Activity implements View.OnClickListener{

    private Button mButtonStart;
    private Button mButtonStop;
    private MediaRecorder recorder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButtonStart= (Button) findViewById(R.id.button_start);
        mButtonStop= (Button) findViewById(R.id.button_stop);
        mButtonStop.setOnClickListener(this);
        mButtonStart.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_start:
                recorder=new MediaRecorder();
                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                recorder.setOutputFile(Environment.getExternalStorageDirectory() + "/myrecord.3gp");

                try {
                    recorder.prepare();
                    recorder.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button_stop:
                recorder.stop();
                recorder.reset();
                recorder.release();
                break;

            default:break;
        }
    }
}

你可能感兴趣的:(media,android,多媒体,录音,recorder)