Android realizes multimedia recording pen _android

Source: Internet
Author: User
Tags prepare

Notepad involves only the storage of string, and there is no difficulty in reading, direct display with TextView can be. The main thing to do is to use SQLite to do a collation of the data.
The recording pen needs to consider a relatively more: such as recording the interruption, recording when the user clicks the play button, not recording, the user clicks the Stop button, in the recording or playback of the Activity;listview to turn off the item to set a different click effect and so on.

For beginners to learn, here is a list of the major points of knowledge involved:

    • 1, Baseadapter
    • 2, Java file
    • 3, Mediarecorder
    • 4, more Alertdialog
    • 5, MediaPlayer

The problems encountered:
When the button control in ListView item can get the focus, set the item length to monitor the event directly for ListView. A case where the ListView item length was invalid by event occurred.

Workaround:
Directly in the baseadapter of the item's layout of the long by-event monitoring (in the author demo is LinearLayout), that is, the button in the item's parent layout for the long event of the monitor.

Effect:

Mainactivity:

Package com.example.recorder; 
Import android.app.Activity; 
Import Android.app.AlertDialog; 
Import Android.app.AlertDialog.Builder; 
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.View; 
Import Android.view.View.OnClickListener; 
Import Android.view.ViewGroup; 
Import Android.widget.AdapterView; 
Import Android.widget.BaseAdapter; 
Import Android.widget.Button; 
Import Android.widget.EditText; 
Import Android.widget.LinearLayout; 
Import Android.widget.ListView; 
Import Android.widget.TextView; 
 
Import Android.widget.Toast; 
Import Java.io.File; 
 
Import java.io.IOException; 
  public class Mainactivity extends activity implements Onclicklistener {private Button start; 
  Private Button stop; 
  Private ListView ListView; 
 
  Showrecorderadpter ShowRecord; 
Audio/video recording/audio file save address  Private MediaPlayer Myplayer; 
  Private Mediarecorder myrecorder = null; 
  Private String path; 
  Private File Savefilepath; 
 
 
  The recorded filename string[] listfile = null; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
    Setcontentview (R.layout.activity_main); 
 
  Initialize control Initview (); 
    private void Initview () {start = (Button) Findviewbyid (R.id.start); 
    Stop = (Button) Findviewbyid (r.id.stop); 
 
    ListView = (ListView) Findviewbyid (r.id.list); 
    Myplayer = new MediaPlayer (); 
 
    ShowRecord = new Showrecorderadpter (); 
        If the cell phone has SD card if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {try { Path = Environment.getexternalstoragedirectory (). Getcanonicalpath (). toString () +/myrecor 
        DERs "; 
        File files = new file (path); if (!files.exists ()) {//If there is no folder to create folder fileS.mkdir (); 
      } listfile = Files.list (); 
      catch (IOException e) {e.printstacktrace (); 
    } start.setonclicklistener (this); 
    Stop.setonclicklistener (this); 
 
 
  Listview.setadapter (ShowRecord); //Because the control's click Effect is involved in the item, use Baseadapter class Showrecorderadpter extends Baseadapter {@Override Pub 
    LIC 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) {View views = LAYOUTINFL 
      Ater.from (Mainactivity.this). Inflate (R.layout.list_item, NULL); 
      LinearLayout parent = (linearlayout) Views.findviewbyid (r.id.list_parent); 
      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); 
      Remove ". Amr" from the Filename.settext (listfile[postion].substring (0, Listfile[postion].length ()-4) when shown in TextView; Parent.setonlongclicklistener (New View.onlongclicklistener () {@Override public boolean onlongclick (Vi EW view) {Alertdialog Aler = new Alertdialog.builder (mainactivity.this). Settitle ("OK to delete the recording?") 
                "). Setpositivebutton (" OK ", new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {File File = new Fil 
                  E (path + "/" + listfile[postion]); 
                  File.delete (); 
                  Refresh the file name list after deleting the file = files = new (path); 
 
                  ListFile = Files.list (); When the file is deleted refresh ListView Showrecord.notifydatasetchangEd (); 
          }). Setnegativebutton ("Cancel", null). Create (); 
          Set the area aler.setcanceledontouchoutside (false) that is not allowed to click outside the Cue box; 
          Aler.show (); 
        return false; 
      } 
      });  Play recording Plays.setonclicklistener (new Onclicklistener () {@Override public void OnClick (View arg0) 
              {//Confirm that the IF (Myrecorder = null) {try {Myplayer.reset () is not played during recording); 
              Myplayer.setdatasource (path + "/" + listfile[postion]); 
                if (!myplayer.isplaying ()) {myplayer.prepare (); 
              Myplayer.start (); 
              else {myplayer.pause (); 
            } catch (IOException e) {e.printstacktrace (); } else {Toast.maketext (mainactivity.this, "Please do not play during recording!") 
          ", Toast.length_short). Show (); 
      }  } 
      }); Stop playing Stop.setonclicklistener (new Onclicklistener () {@Override public void OnClick (View arg0 
          {if (Myrecorder = = null && 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); Alertdialog Aler = new Builder (this). Settitle ("Please enter the file name to save"). Setview (filename). SE 
                  Tpositivebutton (OK), new Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {String tex 
                    t = Filename.gettext (). toString (); If the file name is empty, jump out of the prompt if (text.equals (")) {Toast.maketext (Mainactivity.this, "Please do not enter a blank filename!", Toast.length_short. Show (); 
 
                      else {//Open recording Recorderstart (text); Start.settext ("is in the middle of a recording.") 
                      "); 
                      Start.setenabled (FALSE); 
                      Stop.setenabled (TRUE); 
                      Refresh the file name list after adding files = Files = new (path); 
                      ListFile = Files.list (); 
                    When the file is added Refresh ListView showrecord.notifydatasetchanged (); 
        }}). Setnegativebutton ("Cancel", null). Create (); 
        Set the area aler.setcanceledontouchoutside (false) that is not allowed to click outside the Cue box; 
        Aler.show (); 
      Break 
        Case R.id.stop:myrecorder.stop (); 
        Myrecorder.release (); 
        Myrecorder = null; To determine whether or not to save delete Aler = new Alertdialog.builder (This). Settitle ("Save this Recording"). Setpositivebutton (OK, null). Setnegativebutton ("Cancel", New Dialoginterface.onclicklistener () {@Override public void oncli 
                    CK (dialoginterface dialog, int which) {savefilepath.delete (); 
                    Refresh the file name list after deleting the file = files = new (path); 
 
                    ListFile = Files.list (); 
                  When the file is deleted Refresh ListView showrecord.notifydatasetchanged (); 
        ). Create (); 
        Set the area aler.setcanceledontouchoutside (false) that is not allowed to click outside the Cue box; 
 
        Aler.show (); 
        Start.settext ("recording"); 
        Start.setenabled (TRUE); 
      Stop.setenabled (FALSE); 
    Default:break; 
      } private void Recorderstart (String text) {try {myrecorder = new Mediarecorder (); Recording from a microphone source 
      Myrecorder.setaudiosource (MediaRecorder.AudioSource.DEFAULT); 
      Set output format Myrecorder.setoutputformat (MediaRecorder.OutputFormat.DEFAULT); 
 
      Sets the encoding format Myrecorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT); 
      String paths = path + "/" + text + ". Amr"; 
      Savefilepath = new File (paths); 
      Myrecorder.setoutputfile (Savefilepath.getabsolutepath ()); 
      Myrecorder.prepare (); 
    Start recording Myrecorder.start (); 
    catch (Exception e) {e.printstacktrace (); 
    }} @Override protected void OnDestroy () {Super.ondestroy (); 
      If Myplayer is playing, stop playing and release the resource if (myplayer.isplaying ()) {myplayer.stop (); 
    Myplayer.release (); 
      //If Myrecorder has content (representing a recording), then directly release the resource if (myrecorder!=null) {myrecorder.release (); 
    Myplayer.release ();  
 } 
  } 
 
}

Activity_main:

<linearlayout 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 "Android:orien" tation= "Vertical" tools:context= ". 
    Mainactivity "> <textview android:layout_width=" match_parent "android:layout_height=" Wrap_content " Android:background= "#000" android:padding= "13DP" android:text= "Voice pen" android:textcolor= "#fff" Andro Id:textsize= "22SP" android:textstyle= "bold"/> <listview android:id= "@+id/list" Android:layout_w Idth= "Match_parent" android:layout_height= "0DP" android:layout_weight= "1" android:padding= "10DP" >& lt;/listview> <linearlayout android:id= "@+id/li1" android:padding= "10DP" Android:layout_width= "M 
      Atch_parent "android:layout_height=" wrap_content "android:orientation=" Horizontal "> <Button Android:iD= "@+id/start" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= 
      "1" android:textsize= "20SP" android:text= "Start recording"/> <button android:id= "@+id/stop" Android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:en Abled= "false" android:textsize= "20SP" android:text= "Stop recording"/> </LinearLayout> </linearlay 
 Out>

List_item:

 <?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 "and roid:padding= "10DP" android:id= "@+id/list_parent" android:orientation= "horizontal" > <textview Androi D:id= "@+id/show_file_name" android:layout_width= "0DP" android:layout_weight= "1" android:layout_height= "wrap 
    _content "android:text=" filename "android:textcolor=" #000 "android:textsize=" 20sp "/> <button 
    Android:id= "@+id/bt_list_play" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:textsize= "18SP" android:text= "Play"/> <button android:id= "@+id/bt_list_stop" Android:layo Ut_width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "18SP" android:text= "Stop"/> 
 
; </LinearLayout> 

The above is the entire contents of this article, I hope to realize the Android software programming help.

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.