码迷,mamicode.com
首页 > 移动开发 > 详细

Android 使用MediaRecorder录音

时间:2014-05-17 19:09:09      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   class   code   

bubuko.com,布布扣
package com.example.HyyRecord;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import java.io.File;
import java.io.IOException;

public class MyActivity extends Activity {
    private MediaRecorder mediaRecorder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //首先要保证这个文件夹存在
        File file = new File("/sdcard/0hyyRecord");
        if (!file.exists()) {
            file.mkdir();
        }
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setOutputFile("/sdcard/0hyyRecord/" + System.currentTimeMillis() + ".3pg");

    }

    public void startRecord(View view) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            try {
                mediaRecorder.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaRecorder.start();
        } else {
            System.out.println("sd卡不可用");
        }
    }

    public void stopRecord(View view) {
        mediaRecorder.stop();
        mediaRecorder.reset();
    }

    @Override
    protected void onDestroy() {
        try {
            mediaRecorder.release();
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

   
}
bubuko.com,布布扣

 界面文件:

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <Button android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="开始录音"
            android:onClick="startRecord"
            />

    <Button android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="停止录音"
            android:onClick="stopRecord"
            />

  
</LinearLayout>
bubuko.com,布布扣

 

Android 使用MediaRecorder录音,布布扣,bubuko.com

Android 使用MediaRecorder录音

标签:des   android   style   blog   class   code   

原文地址:http://www.cnblogs.com/wuyou/p/3733417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!