Soundrecorder in Android

Source: Internet
Author: User

1. Recorder class Introduction

The recorder class is responsible for implementing all functions of soundrecorder. It contains a mediarecorder member and a mediaplayer member, and encapsulates the operations related to these two members. This class provides a series of interfaces to the soundrecorder class to control the recording and playing process. The following describes important members and methods.

Member:

Five statuses:

Public static final int idle_state = 0; // stop

Public static final int recording_state = 1; // recording

Public static final int playing_state = 2; // play the recording

Public static final int idle_hold_state = 3; // stop but hold a recording file

Public static final int playing_pause_state = 4; // pause

 

Storage path:

Private Static final string sdcard_path =

Environment. getexternalstoragedirectory (). getabsolutepath ()

+ Mediastore. recorder_path_sdcard;

Private Static final string local_path =

Environment. getdatadirectory (). getabsolutepath ()

+ Mediastore. recorder_path_phone;

 

Sound Recorder and player:

Mediarecorder mrecorder = NULL;

Mediaplayer mplayer = NULL;

 

Method:

Operations on the status:

State (); setstate (); \\read status and set status

 

Savestate (); restorestate (); \ stores and reads the storage path

Samplelength (); samplefile (); getsamplefileuri (); returns the corresponding member

Setsamplefileuri (); \ sets the file path

Setsaveinsdcard (); \ save to SD card

Setsaveinlocal (); \ save to mobile phone

Delete () ;\\ Delete the recording file and update the status.

Clear (); \ Save the recording file. You can perform the next recording.

Getcurrentstorage () \ get the current file path

Setsample () \ sets the storage path and length of the recorded file

 

Main functions:

Startrecording (): starts recording, including:

1. Set the recording file path

2. Construct the recording file name

3. Construct and initialize the recording file

4. Construct and set mrecorder, including setting the sound source, output file format, output file path, and encoding method.

5. Start mrecorder to start recording and set the recorder status to recording_state.

 

Stoprecording (): stops recording, including:

1. Stop mrecorder and release resources.

2. Set the recorder status to idle_hold_state.

 

Startplayback (): start playing the recorded file. The status is set to playing_state.

Stopplayback (): Stop playing. The status is set to idle_hold_state.

Stop (): Stop mrecorder and mplayer. The status is set to idle_hold_state.

Pauseplay (): pause and save the pause position. The status is set to playing_pause_state.

Continueplay (): continue playing. The status is set to playing_state.

 

In general, the recorder class is always in these five States. By calling each method, the recorder jumps between different States, for example (the dotted box is partially implemented in the soundrecorder class ):

For the recorder class, the actual recording implementation is implemented by calling the mrecorder method. For example, in startrecording (), the following code is provided:

Mrecorder = new mediarecorder ();

Mrecorder. setaudiosource (mediarecorder. audiosource. Mic );

Mrecorder. setoutputformat (outputfileformat );

Mrecorder. setaudioencoder (mediarecorder. audioencoder. amr_nb );

Mrecorder. setoutputfile (msamplefile. getabsolutepath ());

Mrecorder. Prepare ();

Mrecorder. Start ();

The recorder only encapsulates the mediarecorder class and provides interfaces to the soundrecorder class. The soundrecorder class is responsible for implementing all the parts presented to users. It is an activity. Shows the relationship between the two.

 

Ii. soundrecorder class Introduction

First, we will introduce the remainingtimecalculator class. It is responsible for calculating the number of Recording files that can be stored in the remaining storage space of the storage media. The timeremaining () function is used to calculate the remaining time and calculate the remaining time based on the smaller limit on the file size and space size.

Mcurrentlowerlimit = Result <result2

? Disk_space_limit: file_size_limit;

 

The soundrecorder class is an activity. This class is responsible for implementing all the parts displayed to the user, including the interface and response to various operations. Recording, recording storage, and playback functions are implemented by calling methods in the recorder class. The soundrecorder contains the mrecorder object of a recorder class.

As a subclass of the activity class, soundrecorder reloads multiple functions of the parent class:

Oncreate () ondestroy () (from startup to full destruction)

Onstart () onstop () (from visible to invisible)

Onpause () (from operable to unoperable)

These functions include the operations required for activity switching between different States. The status switching here is caused by the influence of the soundrecorder program, such as sleep on the mobile phone, program suspension, and switching from soundrecorder program to other programs.

 

In addition, soundrecorder also loads several functions:

Onconfigurationchanged (); when switching between landscape and landscape screens, refresh the UI.

Onsaveinstancestate (bundle outstate); saves the recorder state to the bundle. Actually, the savestate () function mentioned above is called. When the soundrecorder needs to be interrupted for some reason, the status is saved, and then the corresponding code in oncreate () reads the status value in the bundle at the restart to return the status of the program before the interruption, actually, the restorestate () function mentioned above is called.

Onprepareoptionsmenu (), onoptionsitemselected (), the two functions are responsible for implementing the menu key function.

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.