Audio Capture Recording

Source: Internet
Author: User

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:

  1. Create a new instance of android.media.MediaRecorder .
  2. Set the audio source using MediaRecorder.setAudioSource() . You'll 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 on the MediaRecorder.prepare() Mediarecorder instance.
  7. To start audio capture, call MediaRecorder.start() .
  8. To stop audio capture, call MediaRecorder.stop() .
  9. 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: * &LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.