Android development-write a recording demo software in two minutes

Source: Internet
Author: User

 

The SoundRecoder software provided by Android is easy to write. Three Java files have the most distinctive pointer. This is not to introduce the implementation process of the pointer. It is also simple. It is an algorithm that uses the amplitude obtained during the recording process to offset the pointer.

 

<Span style = "font-size: 16px;"> MediaRecorder. getmaxamplder (); // obtain the maximum amplitude of the recording. </span>

Get on the code. It will take two minutes to complete. The interface design is very simple. There are three buttons (Start recording, stop recording, and play recording ).

 

1. Create a project named SoundRecoderDemo

 

Ii. main. xml (layout file)

 

 

<Span style = "font-size: 16px;"> <? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: orientation = "horizontal">

 

<Button

Android: id = "@ + id/start"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_weight = "1"

Android: text = "start"/>

 

<Button

Android: id = "@ + id/stop"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_weight = "1"

Android: text = "stop"/>

 

<Button

Android: id = "@ + id/play"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_weight = "1"

Android: text = "play"/>

 

</LinearLayout> </span>

 

Iii. SoundRecorderActivity (recording implementation)

 

 

<Span style = "font-size: 16px;"> import java. io. File;

Import java. io. IOException;

 

Import android. app. Activity;

Import android. media. MediaRecorder;

Import android. OS. Bundle;

Import android. view. View;

Import android. view. View. OnClickListener;

Import android. widget. Button;

 

Public class SoundRecorderActivity extends Activity implements OnClickListener {

 

Private Button btnStart;

Private Button btnStop;

Private Button btnPlay;

 

Private MediaRecorder mMediaRecorder;

Private File recAudioFile;

Private MusicPlayer mPlayer;

 

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

 

SetupViews ();

}

 

Private void setupViews (){

BtnStart = (Button) findViewById (R. id. start );

BtnStop = (Button) findViewById (R. id. stop );

BtnPlay = (Button) findViewById (R. id. play );

BtnStart. setOnClickListener (this );

BtnStop. setOnClickListener (this );

BtnPlay. setOnClickListener (this );

RecAudioFile = new File ("/mnt/sdcard", "new. amr ");

}

 

@ Override

Public void onClick (View v ){

Switch (v. getId ()){

Case R. id. start:

StartRecorder ();

Break;

Case R. id. stop:

StopRecorder ();

Break;

Case R. id. play:

MPlayer = new MusicPlayer (SoundRecorderActivity. this );

MPlayer. playMicFile (recAudioFile );

Break;

Default:

Break;

}

}

 

Private void startRecorder (){

MMediaRecorder = new MediaRecorder ();

If (recAudioFile. exists ()){

RecAudioFile. delete ();

}

 

MMediaRecorder. setAudioSource (MediaRecorder. AudioSource. MIC );

MMediaRecorder. setOutputFormat (MediaRecorder. OutputFormat. DEFAULT );

MMediaRecorder. setAudioEncoder (MediaRecorder. AudioEncoder. DEFAULT );

MMediaRecorder. setOutputFile (recAudioFile. getAbsolutePath ());

Try {

MMediaRecorder. prepare ();

} Catch (IllegalStateException e ){

E. printStackTrace ();

} Catch (IOException e ){

E. printStackTrace ();

}

MMediaRecorder. start ();

}

Private void stopRecorder (){

If (recAudioFile! = Null ){

MMediaRecorder. stop ();

MMediaRecorder. release ();

}

}

} </Span>

 

4. MusicPlayer)

 

 

<Span style = "font-size: 16px;"> import java. io. File;

 

Import android. content. Context;

Import android. media. MediaPlayer;

Import android. media. MediaPlayer. OnCompletionListener;

Import android.net. Uri;

Import android. util. Log;

 

Public class MusicPlayer {

Private final static String TAG = "MusicPlayer ";

Private static MediaPlayer mMediaPlayer;

Private Context mContext;

Public MusicPlayer (Context context ){

MContext = context;

}

Public void playMicFile (File file ){

If (file! = Null & file. exists ()){

Uri uri = Uri. fromFile (file );

MMediaPlayer = MediaPlayer. create (mContext, uri );

MMediaPlayer. start ();

MMediaPlayer. setOnCompletionListener (new OnCompletionListener (){

Public void onCompletion (MediaPlayer mp ){

// TODO: finish

Log. I (TAG, "Finish ");

}

});

}

}

Public void stopPlayer (){

If (mMediaPlayer. isPlaying ()){

MMediaPlayer. stop ();

MMediaPlayer. release ();

}

}

}

</Span>

 

5. Add recording permission

 

 

<Span style = "font-size: 16px;"> <uses-permission android: name = "android. permission. RECORD_AUDIO"/>

<Uses-permission android: name = "android. permission. WAKE_LOCK"/>

<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/> </span>

Complete. Run the command to view the effect. The code is very simple, without comments. If you are interested, check the API !!

 

Original article. For more information, see htttp: // www.blog.csdn.net/tangcheng_ OK.

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.