[操作系统]Audio Capture 录音
The Android multimedia framework includes support for capturing and encoding a variety of common audio formats, so that you can easily integrate audio into your applications. You can record audio using the
MediaRecorder APIs if supported by the device hardware.
This document shows you how to write an application that captures audio from a device microphone, save the audio and play it back.
Note: The Android Emulator does not have the ability to capture audio, but actual devices are likely to provide these capabilities.
Performing Audio Capture
Audio capture from the device is a bit more complicated than audio and video playback, but still fairly simple:
- Create a new instance of
- Set the audio source using
MediaRecorder.setAudioSource(). You will probably want to use
- Set output file format using
- Set output file name using
- Set the audio encoder using
MediaRecorder.prepare() on the MediaRecorder instance.
- To start audio capture, call
- To stop audio capture, call
- When you are done with the MediaRecorder instance, call
MediaRecorder.release() on it. Calling
MediaRecorder.release() is always recommended to free the resource immediately. //调用release总是被推荐的释放立刻释放资源的办法
Example: Record audio and play the recorded audio
The example class below illustrates how to set up, start and stop audio capture, and to play the recorded audio file.
/* * The application needs to have the permission to write to external storage * if the output file is written to the external storage, and also the * permission to record audio. These permissions must be set in the * application's AndroidManifest.