Android录音功能--MediaRecorder

MediaRecorder

MediaRecorder集成了录音、编码、压缩等功能,所以使用起来相对比较简单。

缺点:支持的格式过少并且无法实时处理音频数据。

开始录音

package com.example.audiotest;

import android.media.MediaRecorder;

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

public class MyAudioRecord {

    private MediaRecorder mediaRecorder;

    public void start(File saveFile) throws IOException {

        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //设置输出文件类型3gp
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        //自适应多速率窄带编解码器,唯一的选择。採样率是8kHz,码率在 4.75~12.2kbps之间。不太适合语音之外其他内容
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        //输出的文件
        mediaRecorder.setOutputFile(saveFile.getAbsolutePath());
        mediaRecorder.prepare();
        mediaRecorder.start();//开始录音
    }

    public void stop() {
        if (mediaRecorder != null) {
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;

        }

    }

}

传入的start的文件为录音文件,需自定义:

package com.example.audiotest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author rb
 */
public class MainActivity extends Activity {

    private MyAudioRecord myAudioRecord;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View view) {
        int id = view.getId();
        switch (id){
            case R.id.start:
                startAudio();
                break;
            case R.id.stop:
                if (myAudioRecord !=null){
                    myAudioRecord.stop();
                }
                break;
            default:
                break;
        }
    }

    private void startAudio(){
        if (myAudioRecord != null) {
            myAudioRecord.stop();
            myAudioRecord = null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd-HH_mm_ss");
        String filePath;
        filePath = "mnt/sdcard/" + dateFormat.format(new Date()) + ".3gp";
        final File saveFile = new File(filePath);
        myAudioRecord = new MyAudioRecord();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    myAudioRecord.start(saveFile);
//                    Thread.sleep(time * 1000);
                    //如果想定时,在这里传入时间,然后调用myAudioRecord.stop()
//                    myAudioRecord.stop();
                } catch (Exception e) {
                        e.printStackTrace();
                }

            }
        }).start();
    }
}

记得添加权限

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页