The Android multimedia framework includes support for capturing and encoding a variety of common audio formats, so that yo U can easily integrate audio into your applications. You can record audio using the MediaRecorder
APIs if supported by the device hardware.
This document shows the "to write" an application-captures audio from a device microphone, save the audio and play it back.
Note: The Android Emulator does not has the ability to capture audio, but actual devices is likely to provide these Capabiliti Es.
Simulator can not record
Performing Audio Capture
Audio capture from the device are a bit more complicated than audio and video playback, but still fairly simple:
- Create a new instance of
android.media.MediaRecorder
.
- Set the audio source using
MediaRecorder.setAudioSource()
. You'll probably want to use MediaRecorder.AudioSource.MIC
.
- Set output file format using
MediaRecorder.setOutputFormat()
.
- Set output file name using
MediaRecorder.setOutputFile()
.
- Set the audio encoder using
MediaRecorder.setAudioEncoder()
.
- Call on the
MediaRecorder.prepare()
Mediarecorder instance.
- To start audio capture, call
MediaRecorder.start()
.
- To stop audio capture, call
MediaRecorder.stop()
.
- When you do with the Mediarecorder instance, call on
MediaRecorder.release()
it. Calling is always recommended to free the MediaRecorder.release()
resource immediately. Call release is always the recommended way to release resources immediately
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 has the permission to write to external storage * If the output file was written to the Exte Rnal storage, and also the * permission to record audio. These permissions must is set in the * application's Androidmanifest.xml file, with something like: * Required Permissions: * <USES-PE Rmission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> * <uses-permission android:name= " Android.permission.RECORD_AUDIO "/> * */package com.android.audiorecordtest;import Android.app.activity;import Android.widget.linearlayout;import Android.os.bundle;import Android.os.environment;import Android.view.ViewGroup; Import Android.widget.button;import Android.view.view;import Android.view.view.onclicklistener;import Android.content.context;import Android.util.log;import Android.media.mediarecorder;import Android.media.mediaplayer;import Java.io.ioexception;public class Audiorecordtest extends activity{private static fin Al String Log_tag = "Audiorecordtest"; Private Static String mfilename = null; Private Recordbutton Mrecordbutton = null; Private Mediarecorder mrecorder = null; Private PlayButton Mplaybutton = null; Private MediaPlayer mPlayer = null; private void Onrecord (Boolean start) {if (start) {startrecording (); } else {stoprecording (); }} private void Onplay (Boolean start) {if (start) {startplaying (); } else {stopplaying (); }} private void Startplaying () {mPlayer = new MediaPlayer (); try {mplayer.setdatasource (mfilename); Mplayer.prepare (); Mplayer.start (); } catch (IOException e) {log.e (Log_tag, "prepare () failed"); }} private void Stopplaying () {mplayer.release (); MPlayer = null; } private void Startrecording () {mrecorder = new Mediarecorder (); Mrecorder.setaudiosource (MediaRecorder.AudioSource.MIC); Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP); Mrecorder.setoutputfile (Mfilename); Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); try {mrecorder.prepare (); } catch (IOException e) {log.e (Log_tag, "prepare () failed"); } mrecorder.start (); } private void Stoprecording () {mrecorder.stop (); Mrecorder.release (); Mrecorder = null; } class Recordbutton extends Button {Boolean mstartrecording = true; Onclicklistener clicker = new Onclicklistener () {public void OnClick (View v) {Onrecord (mstartr ecording); if (mstartrecording) {setText ("Stop recording"); } else {SetText ("Start recording"); } mstartrecording =!mstartrecording; } }; Public Recordbutton (Context ctx) {super (CTX); SetText ("Start recording"); Setonclicklistener (clicker); }} class PlayButton extends Button {Boolean mstartplaying = true; Onclicklistener clicker = new Onclicklistener () {public void OnClick (View v) {Onplay (Mstartpla Ying); if (mstartplaying) {setText ("Stop playing"); } else {SetText ("Start playing"); } mstartplaying =!mstartplaying; } }; Public PlayButton (Context ctx) {super (CTX); SetText ("Start playing"); Setonclicklistener (clicker); }} public Audiorecordtest () {mfilename = Environment.getexternalstoragedirectory (). GetAbsolutePath (); Mfilename + = "/audiorecordtest.3gp"; } @Override public void OnCreate (Bundle icicle) {super.oncreate (icicle); LinearLayout ll = new LinearLayout (this); Mrecordbutton = new Recordbutton (this); Ll.addview (Mrecordbutton, New Linearlayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0)); Mplaybutton = new PlayButton (this); Ll.addview (Mplaybutton, New Linearlayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0)); Setcontentview (LL); } @Override public void OnPause () {super.onpause (); if (Mrecorder! = null) {mrecorder.release (); Mrecorder = null; } if (MPlayer! = null) {mplayer.release (); MPlayer = null; } }}
Audio Capture Recording