Android-implement self-made recording/playing recording programs

Source: Internet
Author: User

First, let's first look at the implementation: When a recording file exists, it will be displayed in the following ListView. The complete implementation code is as follows: 1. main program code [java] 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; im Port android. widget. checkedTextView; import android. widget. imageButton; import android. widget. listView; import android. widget. textView; import android. widget. toast; public class extends Activity {private ImageButton myButton1; private ImageButton myButton2; private ImageButton myButton3; private ImageButton myButton4; private ListView myListView1; private String strTempFile = "Your _"; Private File myRecAudioFile; private File extensions; private File myPlayFile; private MediaRecorder mMediaRecorder01; private ArrayList <String> recordFiles; private ArrayAdapter <String> adapter; private TextView myTextView1; private boolean sdCardExit; private boolean isStopRecord;/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {sup Er. 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 whether SD Card is inserted */sdCardExit = Environment. getExternalStorageState (). equals (android. OS. environment. MEDIA_MOUNTED);/* obtain the SD Card path as the recording file location */if (sdCardExit) myRecAudioDir = Environment. getExternalStorageDirectory ();/* get all in the SD Card directory. amr file */getRecordFiles (); adapter = new ArrayAdapter <Stri Ng> (this, R. layout. my_simple_list_item, recordFiles);/* save ArrayAdapter to the ListView object */myListView1.setAdapter (adapter);/* recording */myButton1.setOnClickListener (new ImageButton. onClickListener () {@ Override public void onClick (View arg0) {try {if (! SdCardExit) {Toast. makeText (EX07_11.this, "insert SD Card", Toast. LENGTH_LONG ). show (); return;}/* Create a recording File */myRecAudioFile = File. createTempFile (strTempFile ,". amr ", myRecAudioDir); mMediaRecorder01 = new MediaRecorder ();/* set the recording source to a microphone */mMediaRecorder01. setAudioSource (MediaRecorder. audioSource. MIC); mMediaRecorder01. setOutputFormat (MediaRecorder. outputFormat. DEFAULT); mMediaRecorder01. setAudioEn Coder (MediaRecorder. audioEncoder. DEFAULT); mMediaRecorder01.setOutputFile (myRecAudioFile. getAbsolutePath (); encrypt (); mMediaRecorder01.start (); myTextView1.setText ("Recording"); myButton2.setEnabled (true); myButton3.setEnabled (false); myButton4.setEnabled (false ); isStopRecord = false;} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}});/* stop */m YButton2.setOnClickListener (new ImageButton. OnClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub if (myRecAudioFile! = Null) {/* stop recording */mMediaRecorder01.stop ();/* Send the recording file name to the 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 arg0) {// TODO Auto -Generated method stub if (myPlayFile! = Null & myPlayFile. exists () {/* The Program for enabling playback */openFile (myPlayFile );}}});/*? Except */myButton4.setOnClickListener (new ImageButton. OnClickListener () {@ Override public void onClick (View arg0) {// TODO Auto-generated method stub if (myPlayFile! = Null) {/* because the Adapter is removed from the file name */adapter. remove (myPlayFile. getName ();/* delete file */if (myPlayFile. exists () myPlayFile. delete (); myTextView1.setText ("") ;}}); myListView1.setOnItemClickListener (new AdapterView. onItemClickListener () {@ Override public void onItemClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {/* Delete the play button and Enable */myButton3.setEnabled (true) when selecting a file name; myButton4.setEnabled (true ); myPlayFile = new File (myRecAudioDir. getAbsolutePath () + File. separator + (CheckedTextView) arg1 ). getText (); myTextView1.setText ("You selected:" + (CheckedTextView) arg1 ). getText () ;}}) ;}@ Override protected void onStop () {if (mMediaRecorder01! = Null &&! IsStopRecord) {/* stop recording */mMediaRecorder01.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 () ;}}}}/* enable the program for playing 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. getName (). length ()). toLowerCase (); String type = ""; if (end. equals ("mp3") | end. equals ("aac") | end. equals ("aac") | end. equals (" Amr ") | end. equals ("mpeg") | end. 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. code of the overall layout file [html] <? 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: layout_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 = "wrap_content" android: src = "@ drawable/stop"> </ImageButton> <ImageButton android: id = "@ + id/ImageButton03" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: src = "@ drawab Le/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_width = "wrap_content" android: layout_height = "wrap_content" android: textColor = "@ drawable/black"> </TextView> <ListView android: id = "@ + id/ListVi Ew01 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: background =" @ drawable/black "> </ListView> </LinearLayout> 3. subview layout in ListView [html] <? 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"/>

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.