Android Audio Application Example Tutorial _android

Source: Internet
Author: User
Tags current time xmlns

This article takes the example form to display in detail the Android recording realization method, shares for everybody to use for the reference. The specific methods are as follows:

The first is the XML layout file:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
  xmlns:tools= "http:// Schemas.android.com/tools "
  android:layout_width=" fill_parent "
  android:layout_height=" Fill_parent
  " android:layout_gravity= "center"
  android:gravity= "center"
  android:orientation= "vertical"
  android: paddingbottom= "@dimen/activity_vertical_margin"
  android:paddingleft= "@dimen/activity_horizontal_margin"
  android:paddingright= "@dimen/activity_horizontal_margin"
  android:paddingtop= "@dimen/activity_ Vertical_margin "
  tools:context=". Mainactivity ">
 
  <button
    android:id=" @+id/btn_talk "android:layout_width=" Wrap_content "
    android:layout_height= "wrap_content"
    android:layout_gravity= "center"
    android:enabled= "false"
    android:text= "TALK"
    android:textsize= "30DP"
    android:textstyle= "bold"/>
 
</ Linearlayout>

The effect is as shown in the following illustration:

Mainactivity to define the button's click Listener, press the button to start recording, release the button to stop recording, similar to the micro-letter operation method.

Gets the control public
void Get_con () {
   
  Btn_talk = (Button) Findviewbyid (r.id.btn_talk);
  Btn_talk.setontouchlistener (New Ontouchlistener () {
    @Override public
    boolean Ontouch (View V, motionevent e) {
      if (e.getaction () = = Motionevent.action_down) {
        //start recording
        Start_record ();
      }
      else if (e.getaction () = = motionevent.action_up) {
        //Stop recording
        Stop_record ();
      }
      return false;}}
  );


The way to start recording is to use the Android.media.MediaRecorder recording. First of all to determine whether the SD card exists, if there is based on the current time to create a recording file, save to the intended directory, with the Mediarecorder class to start recording.

Start recording public void Start_record () {if (! Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED)) {show_status ("SD card does not exist, please insert SD card
  !");     
  }
      else{try {//Get the current time Cur_date = new Date (System.currenttimemillis ()); 
      Str_file = Formatter.format (cur_date); Create an audio file to save recordings Send_sound_file = new file (Environment.getexternalstoragedirectory (). Getcanonicalfile () +/talk/send
      ");
      If the directory does not exist if (!send_sound_file.exists ()) {send_sound_file.mkdirs ();  } send_sound_file = new file (Environment.getexternalstoragedirectory (). Getcanonicalfile () + "/talk/send/" + str_file
      + ". Amr");
      Recorder = new Mediarecorder ();
      Set the sound source of the recording Recorder.setaudiosource (MediaRecorder.AudioSource.MIC);
      Sets the output format of the recorded sound (must be set before setting the sound encoding format) Recorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP);
      Set the format of the sound encoding Recorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); ReCorder.setoutputfile (Send_sound_file.getabsolutepath ());
      Recorder.prepare ();
    Start recording Recorder.start ();
    catch (Exception e) {show_status (e.tostring ());

 }
  }
}

The way to stop recording is relatively simple.

Stop recording public
void Stop_record () {
  if (send_sound_file!= null && send_sound_file.exists ())
  {
    ses_id = ses_id + 1;
    Stop recording
    recorder.stop ();
    Releasing Resources
    recorder.release ();
    Recorder = null;
  }
  Super.ondestroy ();
}

After testing, the recorded 3GP files can be played correctly.

I hope this article will help you with your Android program.

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.