支持国产操作系统国产应用!

捉蛋网

当前位置: 捉蛋网>教程>Android开发>

Android音频录音实现

时间:2013-06-05 16:34来源: 作者: 点击:
  本里使用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;
                  }
          }

(责任编辑:捉蛋网-刷机)
............................................................................................... ...............................................................................................

 

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------