簡體   English   中英

Android中的默認錄制語音

[英]Default Record Voice in Android

我注意到Android默認錄音機可以感知你的聲音有多大,並在UI參數中顯示給你

我是否可以從意圖中使用此功能,或者如何編寫能夠感知Android中語音響度的代碼。

對於錄制Android,可以使用android.media.MediaRecorder。 所有API都列在此頁面http://developer.android.com/reference/android/media/MediaRecorder.html中 這應該可以解決您的所有問題。 示例代碼

 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
 ...
 while(recordingNotOver)
 {
    int lastMaxAmplitude = recorder.getMaxAmplitude();
    // you have the value here in lastMaxAmplitude, do what u want to
 }

 recorder.stop();
 recorder.reset();   // You can reuse the object by going back to setAudioSource() step
 recorder.release(); // Now the object cannot be reused

我把一些代碼。

這是主要的課程。

    final AudioRecorder recorder = new AudioRecorder("/calls");
    try {
        recorder.start();
        try {
            this.wait(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        recorder.stop();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
}

輔助類是:

public class AudioRecorder {

  final MediaRecorder recorder = new MediaRecorder();
  final String path;

  public AudioRecorder(String path) {

    this.path = sanitizePath(path);
  }

  private String sanitizePath(String path) {
    if (!path.startsWith("/")) {
      path = "/" + path;
    }
    if (!path.contains(".")) {
      path += ".3gp";
    }

//This is the command I would like to change, because I want to save the audio 
//files in the internal memory instead of the SD card

    return Environment.getDataDirectory().getAbsolutePath() + path;
  }

  public void start() throws IOException {
    String state = android.os.Environment.getExternalStorageState();
    if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
        throw new IOException("SD Card is not mounted.  It is " + state + ".");
    }

    // make sure the directory we plan to store the recording in exists
    File directory = new File(path).getParentFile();
    if (!directory.exists() && !directory.mkdirs()) {
      throw new IOException("Path to file could not be created.");
    }

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(path);
    recorder.prepare();
    recorder.start();
  }

  public void stop() throws IOException {
    recorder.stop();
    recorder.release();
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM