码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-09-20 10:32:51      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:android   多媒体   media   recorder   录音   

MediaRecorder即录音
要想使用首先查看API,可以看到MediaRecorder的生命周期
技术分享

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;
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:android   多媒体   media   recorder   录音   

原文地址:http://blog.csdn.net/taoolee/article/details/48594563

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!