First, let's look at the screenshot of the implementation:
When a recording file exists, it is shown in the ListView below.
The complete code for implementation is given below:
1. Main program code
Package irdc.ex07_11;
Import Java.io.File;
Import java.io.IOException;
Import java.util.ArrayList;
Import android.app.Activity;
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.widget.AdapterView;
Import Android.widget.ArrayAdapter;
Import Android.widget.CheckedTextView;
Import Android.widget.ImageButton;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import Android.widget.Toast;
public class Ex07_11 extends activity {private ImageButton myButton1;
Private ImageButton MyButton2;
Private ImageButton MyButton3;
Private ImageButton MyButton4;
Private ListView MyListView1;
Private String Strtempfile = "ex07_11_";
Private File Myrecaudiofile;
Private File Myrecaudiodir;
Private File Myplayfile;
Private Mediarecorder MMediaRecorder01;
Private arraylist<string> recordfiles; Private Arrayadapter<string> AdapteR
Private TextView MyTextView1;
Private Boolean sdcardexit;
Private Boolean Isstoprecord; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
MyButton1 = (ImageButton) Findviewbyid (R.ID.IMAGEBUTTON01);
MyButton2 = (ImageButton) Findviewbyid (R.ID.IMAGEBUTTON02);
MyButton3 = (ImageButton) Findviewbyid (R.ID.IMAGEBUTTON03);
MyButton4 = (ImageButton) Findviewbyid (r.id.imagebutton04);
MyListView1 = (ListView) Findviewbyid (R.ID.LISTVIEW01);
MyTextView1 = (TextView) Findviewbyid (R.ID.TEXTVIEW01);
Mybutton2.setenabled (FALSE);
Mybutton3.setenabled (FALSE);
Mybutton4.setenabled (FALSE); /* Determine if SD card is inserted/Sdcardexit = Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED)
;
/* Obtain SD card path as recording file location * * (sdcardexit) Myrecaudiodir = Environment.getexternalstoragedirectory (); /* Get all of the. Amr in the SD card cataloguePieces of */getrecordfiles ();
adapter = new Arrayadapter<string> (this, R.layout.my_simple_list_item, recordfiles);
/* will arrayadapter deposited in the ListView object * * Mylistview1.setadapter (adapter); /* Recording/Mybutton1.setonclicklistener (new Imagebutton.onclicklistener () {@Override public void OnClick (View ar G0) {try {if (!sdcardexit) {Toast.maketext (ex07_11.this, "Please insert SD card", Toast.length_
LONG). Show ();
Return
* * Establish Recording file * * * myrecaudiofile = File.createtempfile (Strtempfile, ". Amr", Myrecaudiodir);
MMediaRecorder01 = new Mediarecorder ();
/* Set recording source for microphone/MMediaRecorder01. Setaudiosource (MediaRecorder.AudioSource.MIC);
MMediaRecorder01. Setoutputformat (MediaRecorder.OutputFormat.DEFAULT);
MMediaRecorder01. Setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT);
Mmediarecorder01.setoutputfile (Myrecaudiofile. GetAbsolutePath ()); Mmediarecorder01.prepare();
Mmediarecorder01.start ();
Mytextview1.settext ("recorded in");
Mybutton2.setenabled (TRUE);
Mybutton3.setenabled (FALSE);
Mybutton4.setenabled (FALSE);
Isstoprecord = false;
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
}
}
}); /* Stop/Mybutton2.setonclicklistener (new Imagebutton.onclicklistener () {@Override public void OnClick (View ar G0) {//TODO auto-generated Method stub if (myrecaudiofile!= null) {/* Stop recording/* MMEDIARECORDER0
1.stop ();
/* The recording file name to Adapter/Adapter.add (Myrecaudiofile.getname ());
Mmediarecorder01.release ();
MMediaRecorder01 = null;
Mytextview1.settext ("STOP:" + myrecaudiofile.getname ());
Mybutton2.setenabled (FALSE);
Isstoprecord = true;
}
}
}); /* Play */Mybutton3.setonclicklistener (new Imagebutton.onclicklistener () {@Override public void OnClick (View ar G0) {//TODO Auto-generated method Stub if (myplayfile!= null && myplayfile.exists ()) {/* Open Playback Program/* OpenFile (MYPLA
Yfile);
}
}
}); * * except */Mybutton4.setonclicklistener (new Imagebutton.onclicklistener () {@Override public void OnClick (View ar G0) {//TODO auto-generated Method stub if (myplayfile!= null) {/* Because the file name is removed from adapter/* ADAPTER.R
Emove (Myplayfile.getname ());
/* Delete file */if (myplayfile.exists ()) Myplayfile.delete ();
Mytextview1.settext ("complete deletion");
}
}
}); Mylistview1.setonitemclicklistener (New Adapterview.onitemclicklistener () {@Override public void Onitemclick
(adapterview<?> arg0, View arg1, int arg2, long arg3)
{/* The Delete and Play button to enable/mybutton3.setenabled (TRUE) when a file name is selected;
Mybutton4.setenabled (TRUE); Myplayfile = new File (Myrecaudiodir.getabsolutepath () + File.separator + ((checkedtextview) arg1). GetText (
)); MyTextView1. SetText ("You choose:" + (Checkedtextview) arg1). GetText ());
}
}); @Override protected void OnStop () {if (MMediaRecorder01!= null &&!isstoprecord) {/* Stop recording */MM
Ediarecorder01.stop ();
Mmediarecorder01.release ();
MMediaRecorder01 = null;
} super.onstop ();
private void Getrecordfiles () {recordfiles = new arraylist<string> ();
if (sdcardexit) {File files[] = Myrecaudiodir.listfiles (); if (Files!= null) {for (int i = 0; i < files.length i++) {if (Files[i].getname (). IndexOf (".") > = 0) {/* read. amr file */String files = files[i].getname (). substring (Files[i].getname (). IndexOf ("."
));
if (Files.tolowercase (). Equals (". Amr")) Recordfiles.add (Files[i].getname ());
}}}/* Open the program that plays the recording file * * private void OpenFile (File f) {Intent Intent = new Intent ();
Intent.addflags (Intent.flag_activity_new_task); Intent.setaction (android.content.IntEnt.
Action_view);
String type = GetMimeType (f);
Intent.setdataandtype (Uri.fromfile (f), type);
StartActivity (Intent); private string GetMimeType (File f) {string end = F.getname (). substring (F.getname (). LastIndexOf (".") + 1, f.ge
Tname (). Length ()). toLowerCase ();
String type = ""; if (End.equals ("MP3") | | | end.equals ("AAC") | | end.equals ("AAC") | | end.equals ("AMR") | | end.equals ("MPEG") | |
Equals ("mp4")) {type = ' audio ';
else if (end.equals ("jpg") | | end.equals ("GIF") | | end.equals ("PNG") | | end.equals ("JPEG")) {type = "image";
else {type = ' * ';
Type = = "/*";
return type;
}
}
2. Overall layout file Code
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/"
Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " android:background= "@drawable/white" > <linearlayout android:id= @+id/linearlayout01 "Android:layout_width=" Wrap_content "android:layout_height=" wrap_content "> <imagebutton android:id=" @+id/imagebutton01 "Android:lay" Out_width= "Wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/record" > </ImageButton > <imagebutton android:id= "@+id/imagebutton02" android:layout_width= wrap_content "android:layout_height=" WR
Ap_content "android:src=" @drawable/stop "> </ImageButton> <imagebutton android:id=" @+id/imagebutton03 " Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/play" > </ imagebutton> <imagebutton android:id= "@+id/iMageButton04 "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:src=" @drawable Delete "> </ImageButton> </LinearLayout> <textview android:id=" @+id/textview01 "Android:layout_wid Th= "Wrap_content" android:layout_height= "wrap_content" android:textcolor= "@drawable/black" > </TextView> & Lt ListView android:id= "@+id/listview01" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" and
roid:background= "@drawable/black" > </ListView> </LinearLayout>
Layout of the child view in 3.ListView
<?xml version= "1.0" encoding= "Utf-8"?> <checkedtextview xmlns:android=
"http://" Schemas.android.com/apk/res/android "
android:id=" @+id/mycheckedtextview1 "
android:layout_width=" Fill_ Parent "
android:layout_height=" fill_parent "
android:textcolor=" "@drawable/white"/>
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.