Android simply USES MediaRecorder for recording instance code

  • 2020-05-17 06:25:21
  • OfStack


package com.ppmeet;  

import java.io.IOException;  

import android.app.Activity;  
import android.graphics.PixelFormat;  
import android.media.MediaRecorder;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.Window;  
import android.view.WindowManager;  
import android.widget.Button;  

/** 
 * class name : TestBasicAudio<BR> 
 * class description : Basic Record Audio Demo<BR> 
 *  
 * @version 1.00 2011/12/01 
 * @author CODYY)peijiangping 
 */  
public class TestBasicAudio extends Activity {  
    private Button button_start;  
    private Button button_stop;  
    private MediaRecorder recorder;  

    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        getWindow().setFormat(PixelFormat.TRANSLUCENT);//  Make the screen landscape   
        requestWindowFeature(Window.FEATURE_NO_TITLE);//  Remove interface title   
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  
        //  Resize the interface   
        setContentView(R.layout.main);  
        init();  
    }  

    private void init() {  
        button_start = (Button) this.findViewById(R.id.start);  
        button_stop = (Button) this.findViewById(R.id.stop);  
        button_stop.setOnClickListener(new AudioListerner());  
        button_start.setOnClickListener(new AudioListerner());  
    }  

    class AudioListerner implements OnClickListener {  
        @Override  
        public void onClick(View v) {  
            if (v == button_start) {  
                initializeAudio();  
            }  
            if (v == button_stop) {  
                recorder.stop();//  Stop the recording   
                // recorder.reset(); //  Restart the MediaRecorder.  
                recorder.release(); //  Burn complete 1 Resources must be released   
                // recorder = null;  
            }  
        }  

        private void initializeAudio() {  
            recorder = new MediaRecorder();// new Out of the MediaRecorder object   
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);  
            //  Set up the MediaRecorder The audio source is the microphone   
            recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);  
            //  Set up the MediaRecorder Recorded audio format   
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);  
            //  Set up the MediaRecorder The recorded audio is encoded as amr.  
            recorder.setOutputFile("/sdcard/peipei.amr");  
            //  Set the path to save the recorded audio file   
            try {  
                recorder.prepare();//  To prepare the recording   
                recorder.start();//  Start recording   
            } catch (IllegalStateException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  

        }  
    }  
}  

AndroidMainfest.xml


<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.ppmeet"  
    android:versionCode="1"  
    android:versionName="1.0" >  

    <uses-sdk android:minSdkVersion="8" />  

    <application  
        android:icon="@drawable/ic_launcher"  
        android:label="@string/app_name" >  
        <activity  
            android:name=".TestBasicAudio"  
            android:screenOrientation="landscape" >  
            <intent-filter >  
                <action android:name="android.intent.action.MAIN" />  

                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
    </application>  
    <!--  Connected to the Internet access  -->  
    <uses-permission android:name="android.permission.INTERNET" />  
    <!--  to SDCard Write data permission  -->  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
    <!--  The recording permissions  -->  
    <uses-permission android:name="android.permission.RECORD_AUDIO" />  
    <!--  in SDCard Create and delete file permissions in  -->  
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  

</manifest>  


Related articles: