How to record sound by using inbuilt microphone in android

爱⌒轻易说出口 提交于 2019-12-12 08:14:21

问题


I need to record sound by using mobile's own microphone... How to do it?


回答1:


It's explained here

Audio capture from the device is a bit more complicated than audio/video playback, but still fairly simple:

  1. Create a new instance of android.media.MediaRecorder using new
  2. Set the audio source using MediaRecorder.setAudioSource(). You will probably want to use MediaRecorder.AudioSource.MIC
  3. Set output file format using MediaRecorder.setOutputFormat()
  4. Set output file name using MediaRecorder.setOutputFile()
  5. Set the audio encoder using MediaRecorder.setAudioEncoder()
  6. Call MediaRecorder.prepare() on the MediaRecorder instance.
  7. To start audio capture, call MediaRecorder.start().
  8. To stop audio capture, call MediaRecorder.stop().
  9. When you are done with the MediaRecorder instance, call MediaRecorder.release() on it. Calling MediaRecorder.release() is always recommended to free the resource immediately.



回答2:


Example:

To start recording:

        MediaRecorder audioRecorder = new MediaRecorder();
        audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        audioRecorder.setOutputFile(AUDIO_FILE_PATH);

        try {
            audioRecorder.prepare();

        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        audioRecorder.start();

To stop recording:

        audioRecorder.stop();
        audioRecorder.release();


来源:https://stackoverflow.com/questions/6261241/how-to-record-sound-by-using-inbuilt-microphone-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!