首页 > 系统 > Android > 正文

Android简单的利用MediaRecorder进行录音的实例代码

2020-04-11 12:03:40
字体:
来源:转载
供稿:网友

复制代码 代码如下:

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);// 让界面横屏 
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉界面标题 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN); 
        // 重新设置界面大小 
        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();// 停止刻录 
                // recorder.reset(); // 重新启动MediaRecorder. 
                recorder.release(); // 刻录完成一定要释放资源 
                // recorder = null; 
            } 
        } 

        private void initializeAudio() { 
            recorder = new MediaRecorder();// new出MediaRecorder对象 
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
            // 设置MediaRecorder的音频源为麦克风 
            recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR); 
            // 设置MediaRecorder录制的音频格式 
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
            // 设置MediaRecorder录制音频的编码为amr. 
            recorder.setOutputFile("/sdcard/peipei.amr"); 
            // 设置录制好的音频文件保存路径 
            try { 
                recorder.prepare();// 准备录制 
                recorder.start();// 开始录制 
            } 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> 
    <!-- 联网权限 --> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <!-- 往SDCard写入数据权限 --> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <!-- 录音权限 --> 
    <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
    <!-- 在SDCard中创建与删除文件权限 --> 
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 

</manifest> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表