Recording songs in Android...

Source: Internet
Author: User
Package com. king. android. media;

Import java. io. File;
Import java. io. FilenameFilter;
Import java. io. IOException;
Import java. util. ArrayList;
Import java. util. List;

Import android. app. ListActivity;
Import android. content. Intent;
Import android. media. MediaRecorder;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. ArrayAdapter;
Import android. widget. Button;
Import android. widget. ListView;

Import com. king. android. R;

/**

* Description: audio recording.
* Author: Andy. Liu
* Time: 07:27:06
**/
Public class RecordActivity extends ListActivity implements OnClickListener {
// The recorded audio file
Private File mRecAudioFile, mRecAudioPath;

Private MediaRecorder mMediaRecoder; // MediaRecorder object

Private List <String> mMusicList = new ArrayList <String> (); // List of Recording files

Private String strTempFile = "recaudio _"; // temporary file prefix

@ Override
Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );
SetContentView (R. layout. mediarecore_layout );

Button btnOperate = (Button) findViewById (R. id. btnStart );
BtnOperate. setOnClickListener (this );
BtnOperate = (Button) findViewById (R. id. btnStop );
BtnOperate. setOnClickListener (this );
MRecAudioPath = Environment. getExternalStorageDirectory ();

RefreshFile (mRecAudioPath. getAbsolutePath (), ". amr ");
// MusicList ();
}

@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. btnStop:
DoStop ();
Break;

Case R. id. btnStart:
DoStart ();
Break;
}
}

// Start recording
Private void doStart (){
Try {
// Create a recording file
MRecAudioFile = File. createTempFile (strTempFile, ". amr", mRecAudioPath );

// Instantiate the MediaRecorder object
MMediaRecoder = new MediaRecorder ();
// Set the microphone
MMediaRecoder. setAudioSource (MediaRecorder. AudioSource. MIC );
// Set the output file format
MMediaRecoder. setOutputFormat (MediaRecorder. OutputFormat. DEFAULT );
// Set the encoding of the audio file
MMediaRecoder. setAudioEncoder (MediaRecorder. AudioEncoder. DEFAULT );
// File output path
MMediaRecoder. setOutputFile (mRecAudioFile. getAbsolutePath ());
// Prepare
MMediaRecoder. prepare ();
// Start
MMediaRecoder. start ();
} Catch (IOException e ){

E. printStackTrace ();
} Finally {
}
}

Private void doStop (){
If (null! = MRecAudioFile ){
MMediaRecoder. stop (); // stop the recording
MMusicList. add (mRecAudioFile. getName ());
ArrayAdapter <String> musicList = new ArrayAdapter <String> (RecordActivity. this, R. layout. list, mMusicList );
SetListAdapter (musicList );
MMediaRecoder. release ();
MMediaRecoder = null;
}
}

/* Play the recording file */
Private void playMusic (File 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 );
}
@ Override
/* When we click the list, play the clicked music */
Protected void onListItemClick (ListView l, View v, int position, long id)
{
/* Get the clicked file */
File playfile = new File (mRecAudioPath. getAbsolutePath () + File. separator + mMusicList. get (position ));
/* Play */
PlayMusic (playfile );
}

/**
*
* TODO: playlist
* Author: Andy. Liu
* Create Time: 11:27:26
* TAG:
* Return: void
*/

Public void musicList (){
// Obtain the file at the specified position and display it to the playback list.
File home = mRecAudioPath;
If (home. listFiles (new MusicFilter (). length> 0 ){
For (File file: home. listFiles (new MusicFilter ())){
MMusicList. add (file. getName ());
}
ArrayAdapter <String> musicList = new ArrayAdapter <String> (RecordActivity. this, R. layout. list, mMusicList );
SetListAdapter (musicList );
}
}

/**
*
Recursively retrieve folder files
* TODO: TODO
* Author: Andy. Liu
* Create Time: 11:40:33
* TAG: @ param path: Specifies the file path. Generally, select the root directory.
* TAG: @ param fileType file type.
* Return: void
*/
Public void refreshFile (String path, String fileType ){
File file = new File (path );
File [] files = file. listFiles ();
If (null = files)
Return;

For (int I = 0; I <files. length; I ++ ){
If (files [I]. isDirectory ()){
RefreshFile (files [I]. getAbsolutePath (), fileType); // note the Recursive Method
} Else {
If (files [I]. getName (). endsWith (fileType ))
// AllFiles. add (files [I]);
MMusicList. add (file. getName ());
}
}
}


// Transition file type
Class MusicFilter implements FilenameFilter {

@ Override
Public boolean accept (File dir, String filename ){
Return filename. endsWith (". amr ");
}
}

}

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.