Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android音頻錄音實現

Android音頻錄音實現

編輯:關於android開發

  本裡使用MediaRecorder實現音頻錄音的功能:

  public void recording() {
                  if (checkSDCard()) {
                          mRecAudioPath = Environment.getExternalStorageDirectory();
                          File fPath = new File(mRecAudioPath.getPath() + File.separator
                                          + "WJ_RecordList");
                          fPath.mkdirs();

                          mRecAudioFile = fPath;
                  } else {
                          Toast.makeText(this.getContext(), "請插入SDcard", 0).show();
                       
                          return ;
                  }
               
                  try {
                          mRecAudioFile = File.createTempFile(String.valueOf("tmp_record"), ".3gp", mRecAudioFile);
                  } catch (IOException e) {
                          Log.e("Jarvis", "mRec", e);
                  }
               
                  // instance
                  mRecorder = new MediaRecorder();
                  // 設置麥克風
                  mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                  // 輸出文件格式
                  mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                  // 音頻文件編碼
                  mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                  // 輸出文件路徑
                  mRecorder.setOutputFile(mRecAudioFile.getAbsolutePath());
                  Log.e("Jarvis", "輸出文件路徑:" + mRecAudioFile.getAbsolutePath());
               
                  // 准備--開始
                  try {
                          mRecorder.prepare();
                          mRecorder.start();
                  } catch (IllegalStateException e) {
                          e.printStackTrace();
                  } catch (IOException e) {
                          e.printStackTrace();
                  }
          }

  public void stopRec() {
                  if(mRecorder != null) {
                          mRecorder.stop();
                          mRecAudioFile.getName();
                          Log.e("Jarvis", "Name:" + mRecAudioFile.getName()+ "~" + mRecAudioFile.getAbsolutePath());
                          mRecorder.release();
                          mRecorder = null;
                  }
          }

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