Audio Capture recording and audiocapture recording

Source: Internet
Author: User

Audio Capture recording and audiocapture recording

The Android multimedia framework provided des 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 usingMediaRecorderAPIs 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.

// The simulator does not support recording.

Ming Audio Capture

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

Example: Record audio and play the recorded audio

The example class below extends strates 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. xml file, with something like: * required permissions: * <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE "/> * <uses-permission android: name =" Ndroid. 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. mediaR Ecorder; import android. media. mediaPlayer; import java. io. IOException; public class AudioRecordTest extends Activity {private static final 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 () {mPlaye R. 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 () faile D ");} 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 (mStartRecording ); 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 (mStartPlaying); 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 Reco RdButton (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 (m Recorder! = Null) {mRecorder. release (); mRecorder = null;} if (mPlayer! = Null) {mPlayer. release (); mPlayer = null ;}}}

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.