Android Using tape recorder

Source: Internet
Author: User

Main code:

package com.shao.mediarecord;import java.io.file;import java.io.ioexception;import  android.app.activity;import android.media.mediarecorder;import android.os.bundle;import  Android.os.environment;import android.view.view;import android.view.view.onclicklistener;import  android.widget.button;import android.widget.toast;public class mainactivity extends  Activity {public Button btnStartMedia = null;public Button  btnstopmedia = null;public mediarecorder mediarecorder = null;private  Onclicklistener onclicklistener = new onclicklistener ()  {@Overridepublic  void  onclick (view v)  {switch (V.getid ())  {case r.id.btnstartmedia:startmediarecord (); break ; Case r.id.btnstopmedia:stopmediarecord (); break;default:break;}}; @Overrideprotected  void oncreate (bundle savedinstancestate)  {super.OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main);this.btnstartmedia =  (Button)  findviewbyid (R.id.btnstartmedia);this.btnstopmedia =  (Button)  findviewbyid ( R.id.btnstopmedia); This.btnStartMedia.setOnClickListener (Onclicklistener); This.btnStopMedia.setOnClickListener (Onclicklistener);} Public void startmediarecord ()  {if (mediarecorder!=null)  {toast.maketext (this,  "ERR",  toast.length_long). Show (); return ;} Mediarecorder = new mediarecorder (); Mediarecorder.setaudiosource (MediaRecorder.AudioSource.MIC) ; Mediarecorder.setoutputformat (MediaRecorder.OutputFormat.AMR_WB); Mediarecorder.setaudioencoder ( MediaRecorder.AudioEncoder.AMR_WB); File file = new file (Environment.getexternalstoragedirectory (),  "Bianbian"); File.exists ())  {file.mkdirs ();} File filepath = new file (File, system.currenttimemillis () + ". Amr"); if (filePath!=null)  {try {filepAth.createnewfile ();}  catch  (ioexception e)  {e.printstacktrace ();}} Mediarecorder.setoutputfile (Filepath.getabsolutepath ()); Try {mediarecorder.prepare (); MediaRecorder.start ( );}  catch  (illegalstateexception e)  {e.printstacktrace ();}  catch  (ioexception e)  {e.printstacktrace ();}} Public void stopmediarecord ()  {this.mediarecorder.stop (); This.mediaRecorder.release (); This.mediarecorder = null;}}



Description: Open app appears two buttons, one starts recording, one stops recording. After clicking Start Recording, speak and be able to output the sound to a file.

Implementation Introduction:

Start playing the sound, mainly used in the Mediarecorder class, you can see that this class can be directly instantiated. Setaudiosource is to set the sound source, Setoutputformat is to set the output file format, Setaudioencoder is to set the sound encoding.

Mediarecorder = new Mediarecorder (); Mediarecorder.setaudiosource (MediaRecorder.AudioSource.MIC); Mediarecorder.setoutputformat (MediaRecorder.OutputFormat.AMR_WB); Mediarecorder.setaudioencoder ( MediaRecorder.AudioEncoder.AMR_WB);

Then set the output file and path, call the start playback method.

Mediarecorder.setoutputfile (Filepath.getabsolutepath ()); Mediarecorder.prepare (); Mediarecorder.start ();

Attach the layout configuration file

<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"     tools:context= " Com.shao.mediarecord.MainActivity "    android:orientation=" vertical " >     <button        android:id= "@+id/btnStartMedia"          android:layout_width= "Fill_parent"          android:layout_height= "Wrap_content"         android: text= "Start Recording"  />    <Button         Android:id= "@+id/btnstopmedia"         android:layout_width= "Fill_ Parent "        android:layout_height= "Wrap_content"         android:text= "Stop Recording"  /></LinearLayout>


The best thing to say is to read the device and store the file. To add the appropriate permissions.

<uses-permission android:name= "Android.permission.RECORD_AUDIO"/><uses-permission android:name= " Android.permission.READ_EXTERNAL_STORAGE "/><uses-permission android:name=" android.permission.WRITE_ External_storage "/>



Android Using tape recorder

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.