android audio capture
Android provides a variety of APIs for capturing audio from the device's microphone. Here are some of the main APIs:
AudioRecord: This is a low-level API that provides direct access to the audio hardware. It allows developers to capture raw audio data and process it in real-time.
MediaRecorder: This is a high-level API that provides a simple way to record audio and save it to a file. MediaRecorder takes care of many of the details, such as setting up the audio source and encoding the audio data.
AudioPlaybackCapture: This API allows capturing audio played by other applications. It can be used to create applications that capture audio from games, music players, and other sources.
Here is an example code snippet that shows how to use MediaRecorder to capture audio and save it to a file:
// Create a new MediaRecorder instance MediaRecorder recorder = new MediaRecorder(); // Set the audio source to the device's microphone recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // Set the output format to MP3 recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); // Set the audio encoder to AAC recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); // Set the output file path recorder.setOutputFile("/sdcard/my_audio.mp4"); // Prepare the recorder recorder.prepare(); // Start the recording recorder.start(); // Stop the recording after 10 seconds Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { recorder.stop(); recorder.release(); } }, 10000);
In this example, we create a new MediaRecorder instance and set the audio source to the device's microphone. We then set the output format to MP3, the audio encoder to AAC, and the output file path to "/sdcard/my_audio.mp4". Finally, we prepare the recorder, start the recording, and stop the recording after 10 seconds using a Handler.