Android Audio recording Mediarecorder simple recording software implementation code

Source: Internet
Author: User

Original: http://www.jb51.net/article/46182.htm

Android Audio recording Mediarecorder simple recording software implementation code this article mainly introduces the Android audio recording Mediarecorder simple recording software implementation code, the need for friends can refer to

Steps to use Mediarecorder:
1. Create a Mediarecorder object
2. Call the Setaudiosource () method of the Medirecorder object to set the source of the sound, generally incoming mediarecorder.mic
3. Call the Mediarecorder object's Setoutputformat () to set the format of the recorded audio file
4. Call the Mediarecorder object's Setaudiorncoder (), setaudioencodingbitrate (int bitrate), setaudiosamlingrate (int SamplingRate) Set the encoding format, encoding bit rate, sample rate, etc. of the recording,
5. Call the Setoutputfile (String Path) method of the Mediarecorder object to set the location where the recorded audio file is saved
6. Call the Prepare () method of the Mediarecoder object to prepare the recording
7. Call the Start () method of the Mediarecoder object to begin recording
8. Call the Stop () method of the Mediarecoder object to stop recording and call the release () method to release the resource

Instance:

Copy CodeThe code is as follows:
<uses-permission android:name= "Android.permission.MOUNT_FORMAT_FILESYSTEMS"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.RECORD_AUDIO"/>

Copy CodeThe code is as follows:
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Tools:context= ". Mainactivity ">


<linearlayout
Android:id= "@+id/li1"
android:orientation= "Horizontal"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >
<button android:id= "@+id/start"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "@string/start"/>
<button android:id= "@+id/stop"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
android:text= "@string/stop"/>
</LinearLayout>
<listview
Android:id= "@+id/list"
android:layout_below= "@id/li1"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" ></ListView>

</RelativeLayout>

Copy CodeThe code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Horizontal" >

<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/show_file_name"/>

<button
Android:id= "@+id/bt_list_play"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/play"/>
<button android:id= "@+id/bt_list_stop"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/list_stop"/>

</LinearLayout>

Copy CodeThe code is as follows:
Package com.android.xiong.mediarecordertest;

Import Java.io.File;
Import java.io.IOException;
Import Java.text.SimpleDateFormat;

Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.app.AlertDialog.Builder;
Import Android.app.Dialog;
Import Android.content.DialogInterface;
Import Android.media.MediaPlayer;
Import Android.media.MediaRecorder;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.view.LayoutInflater;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.ListView;
Import Android.widget.TextView;

public class Mainactivity extends Activity implements Onclicklistener {

Private Button start;
Private Button stop;
Private ListView ListView;
Audio file Playback
Private MediaPlayer Myplayer;
Recording
Private Mediarecorder Myrecorder;
Audio File Save Address
Private String path;
Private String paths = path;
Private File Savefilepath;
The files that were recorded
string[] listfile = null;

Showrecorderadpter ShowRecord;
Alertdialog Aler = null;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Start = (Button) Findviewbyid (R.id.start);
Stop = (Button) Findviewbyid (r.id.stop);
ListView = (ListView) Findviewbyid (R.id.list);
Myplayer = new MediaPlayer ();
Myrecorder = new Mediarecorder ();
Recording from a microphone source
Myrecorder.setaudiosource (MediaRecorder.AudioSource.DEFAULT);
Setting the output format
Myrecorder.setoutputformat (MediaRecorder.OutputFormat.DEFAULT);
Set the encoding format
Myrecorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT);
ShowRecord = new Showrecorderadpter ();
if (Environment.getexternalstoragestate (). Equals (
environment.media_mounted)) {
try {
Path = Environment.getexternalstoragedirectory ()
. Getcanonicalpath (). ToString ()
+ "/xiongrecorders";
File files = new file (path);
if (!files.exists ()) {
Files.mkdir ();
}
ListFile = Files.list ();
} catch (IOException e) {
E.printstacktrace ();
}
}

Start.setonclicklistener (this);
Stop.setonclicklistener (this);
if (listfile! = null) {
Listview.setadapter (ShowRecord);
}

}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

Class Showrecorderadpter extends Baseadapter {

@Override
public int GetCount () {
return listfile.length;
}

@Override
Public Object getItem (int arg0) {
return arg0;
}

@Override
public long getitemid (int arg0) {
return arg0;

}

   @Override
  public view getView (final int postion, view arg1, ViewGroup arg2) {
 & Nbsp; view views = Layoutinflater.from (mainactivity.this). Inflate (
      R.layout.list_show_filerecorder, NULL);
   textview filename = (TextView) views
     .findviewbyid ( R.id.show_file_name);
   button plays = (Button) Views.findviewbyid (R.id.bt_list_play);
   button stop = (Button) Views.findviewbyid (r.id.bt_list_stop);

Filename.settext (Listfile[postion]);
Play recording
Plays.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
try {
Myplayer.reset ();
Myplayer.setdatasource (path + "/" + listfile[postion]);
if (!myplayer.isplaying ()) {

Myplayer.prepare ();
Myplayer.start ();
} else {
Myplayer.pause ();
}

} catch (IOException e) {
E.printstacktrace ();
}
}
});
Stop playing
Stop.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View arg0) {
if (myplayer.isplaying ()) {
Myplayer.stop ();
}
}
});
return views;
}

}

@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.start:
Final EditText filename = new EditText (this);
Builder Alerbuidler = new Builder (this);
Alerbuidler
. Settitle ("Please enter a file name to save")
. Setview (FileName)
. Setpositivebutton ("OK",
New Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog,
int which) {
String text = Filename.gettext (). toString ();
try {
paths = Path
+ "/"
+ Text
+ New SimpleDateFormat (
"YYYYMMDDHHMMSS"). Format (System
. Currenttimemillis ())
+ ". Amr";
Savefilepath = new File (paths);
Myrecorder.setoutputfile (Savefilepath
. GetAbsolutePath ());
Savefilepath.createnewfile ();
Myrecorder.prepare ();
Start recording
Myrecorder.start ();
Start.settext ("Recording in progress. ");
Start.setenabled (FALSE);
Aler.dismiss ();
Re-reading files
File files = new file (path);
ListFile = Files.list ();
Refresh ListView
Showrecord.notifydatasetchanged ();
} catch (Exception e) {
E.printstacktrace ();
}

}
});
Aler = Alerbuidler.create ();
Aler.setcanceledontouchoutside (FALSE);
Aler.show ();
Break
Case R.id.stop:
if (savefilepath.exists () && Savefilepath! = null) {
Myrecorder.stop ();
Myrecorder.release ();
Determine if save is deleted if not saved
New Alertdialog.builder (This)
. Settitle ("Save this recording")
. Setpositivebutton ("OK", NULL)
. Setnegativebutton ("Cancel",
New Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog,
int which) {
Savefilepath.delete ();
Re-reading files
File files = new file (path);
ListFile = Files.list ();
Refresh ListView
Showrecord.notifydatasetchanged ();
}
}). Show ();

}
Start.settext ("recording");
Start.setenabled (TRUE);
Default
Break
}

}

@Override
protected void OnDestroy () {
Freeing resources
if (myplayer.isplaying ()) {
Myplayer.stop ();
Myplayer.release ();
}
Myplayer.release ();
Myrecorder.release ();
Super.ondestroy ();
}

}

SOURCE Download: Http://xiazai.jb51.net/201401/yuanma/MediaRecorderTest (jb51.net). rar

Android Audio recording Mediarecorder simple recording software implementation code (GO)

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.