Android Audio recording Mediarecorder simple recording software implementation code _android

Source: Internet
Author: User
Tags prepare

Steps to use Mediarecorder:
1. Create Mediarecorder objects
2, call the Medirecorder object's Setaudiosource () method to set the source of the sound, general incoming mediarecorder.mic
3, call the Mediarecorder object's Setoutputformat () to set the format of the recorded audio file
4, invoke the Mediarecorder object's Setaudiorncoder (), setaudioencodingbitrate (int bitrate), setaudiosamlingrate (int SamplingRate) Set the recorded encoding format, coding bit rate, sampling rate, etc.
5, call the Mediarecorder object's Setoutputfile (String path) method to set the location where the recorded audio files are saved
6, call the Mediarecoder object's prepare () method to prepare the recording
7, call the Mediarecoder object's start () method to begin recording
8. Call the Stop () method of the Mediarecoder object to stop recording and call the release () method to free the resource

Instance:

Copy Code code 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 Code code 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 Code code 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 Code code 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 file that was 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);
Set output format
Myrecorder.setoutputformat (MediaRecorder.OutputFormat.DEFAULT);
Set 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 the 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 the 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 ("is in the middle of a recording.") ");
Start.setenabled (FALSE);
Aler.dismiss ();
Re-read 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 saving 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-read 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 () {
Releasing 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

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.