Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 6步學會android多媒體MediaRecorder錄音

6步學會android多媒體MediaRecorder錄音

編輯:關於Android編程

MediaRecorder即錄音
要想使用首先查看API,可以看到MediaRecorder的生命周期
這裡寫圖片描述

API不僅給了生命周期圖,還有例子如下<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;"> 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】別忘加權限

 

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

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved