Android audio recording

Source: Internet
Author: User

The corresponding permission must be set in androidmanifest: Android: Name = "android. Permission. record_audio"

1. first determine whether an SD card exists and obtain the corresponding path

/* Check whether an SD card exists */

If (environment. getexternalstoragestate (). Equals (Android. OS. environment. media_mounted ))

{

/* Get the path of the SD card */

Mrecaudiopath = environment. getexternalstoragedirectory ();

/* Update all Recording files to the list */

Musiclist ();}

2. Start recording

/* Create a recording file. The first parameter is the prefix of the file name, the second parameter is the suffix, and the third parameter is the SD path */

Mrecaudiofile = file. createtempfile (strtempfile, ". Amr", mrecaudiopath );

/* Instantiate the mediarecorder object */

Mmediarecorder = new mediarecorder ();

/* Set the microphone */

Mmediarecorder. setaudiosource (mediarecorder. audiosource. Mic );

/* Set the output file format */

Mmediarecorder. setoutputformat (mediarecorder. outputformat. Default );

/* Set the encoding of the audio file */

Mmediarecorder. setaudioencoder (mediarecorder. audioencoder. Default );

/* Set the path of the output file */

Mmediarecorder. setoutputfile (mrecaudiofile. getabsolutepath ());

/* Prepare */

Mmediarecorder. Prepare ();

/* Start */

Mmediarecorder. Start ();

 

3. Disable recording

Mmediarecorder. Stop ();

4. Play the recording file

Intent intent = new intent ();

Intent. addflags (intent. flag_activity_new_task); intent. setaction (Android. content. intent. action_view);/* set the file type */intent. setdataandtype (URI. fromfile (file), "audio"); startactivity (intent );

 

5. filter file types to implement filenamefilter

Class musicfilter implements filenamefilter {public Boolean accept (File Dir, string name) {return (name. endswith (". Amr "));}

}

 

 

Download the code:/files/lee0oo0/examples_07_05.rar

Related Article

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.