分享一个音频录制成文件的工具类

音频录制完成之后格式为raw格式,但是为了节省服务器空间,一般会将raw格式文件进行压缩MP3
本文实现了录音生成本地文件后,然后将raw压缩为MP3文件
废话不多说上代码



import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;

import com.pocketdigi.utils.FLameUtils;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class AudioRecorder2Mp3Util {

    private Activity activity;

    public static final int RAW = 0X00000001;
    public static final int MP3 = 0X00000002;

    private String rawPath = null;
    private String mp3Path = null;

    private static final int SAMPLE_RATE = 8000;

    private short[] mBuffer;
    private AudioRecord mRecorder;

    public AudioRecorder2Mp3Util(Activity activity) {
        this.activity = activity;
    }

    private boolean isRecording = false;

    private boolean convertOk = false;

    public AudioRecorder2Mp3Util(Activity activity, String rawPath, String mp3Path) {
        this.activity = activity;
        this.rawPath = rawPath;
        this.mp3Path = mp3Path;
    }

    public boolean startRecording() {

        boolean valid = false;

        if (isRecording) {
            return isRecording;
        }

        if (mRecorder == null) {
            valid = initRecorder();
        }

        if (valid) {
            getFilePath();
            try {
                mRecorder.startRecording();

                startBufferedWrite(new File(rawPath));

                isRecording = true;
            } catch (Exception e) {
                e.printStackTrace();

                isRecording = false;
            }
        } else {
            isRecording = false;
        }

        return isRecording;
    }

    public boolean stopRecordingAndConvertFile() {
        if (!isRecording) {
            return isRecording;
        }

        mRecorder.stop();
        isRecording = false;

        FLameUtils lameUtils = new FLameUtils(2, SAMPLE_RATE, 96);
        convertOk = lameUtils.raw2mp3(rawPath, mp3Path);

        return isRecording ^ convertOk;// convertOk==true,return true
    }

    public boolean stopRecording() {
        if (!isRecording) {
            return isRecording;
        }

        mRecorder.stop();
        isRecording = false;

        return true;
    }

    public String getFilePath(int fileAlias) {
        if (fileAlias == RAW) {
            return rawPath;
        } else if (fileAlias == MP3) {
            return mp3Path;
        } else
            return null;
    }

    /**
     * @param cleanFlag RAW,MP3 or RAW|MP3
     */
    public void cleanFile(int cleanFlag) {
        File f = null;
        try {
            switch (cleanFlag) {
                case MP3:
                    f = new File(mp3Path);
                    if (f.exists())
                        f.delete();
                    break;
                case RAW:
                    f = new File(rawPath);
                    if (f.exists())
                        f.delete();
                    break;
                case RAW | MP3:
                    f = new File(rawPath);
                    if (f.exists())
                        f.delete();
                    f = new File(mp3Path);
                    if (f.exists())
                        f.delete();
                    break;
            }
            f = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void close() {
        if (mRecorder != null)
            mRecorder.release();
    }

    private boolean initRecorder() {
        boolean valid = false;
        try {
            int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
            mBuffer = new short[bufferSize];
            mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
            valid = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return valid;
    }

    private void getFilePath() {
        try {
            String folder = "audio_recorder_2_mp3";
            String fileName = String.valueOf(System.currentTimeMillis());
            if (rawPath == null) {
                File raw = new File(activity.getDir(folder, activity.MODE_PRIVATE), fileName + ".raw");
                raw.createNewFile();
                rawPath = raw.getAbsolutePath();
                raw = null;
            }else {
                File file = new File(rawPath);
                if (!file.exists()){
                    file.createNewFile();
                }
            }
            if (mp3Path == null) {
                File mp3 = new File(activity.getDir(folder, activity.MODE_PRIVATE), fileName + ".mp3");
                mp3.createNewFile();
                mp3Path = mp3.getAbsolutePath();
                mp3 = null;
            }

            runCommand("chmod 777 " + rawPath);
            runCommand("chmod 777 " + mp3Path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean runCommand(String command) {
        boolean ret = false;
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(command);
            process.waitFor();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                process.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    private void startBufferedWrite(final File file) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                DataOutputStream output = null;
                try {
                    output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
                    while (isRecording) {
                        int readSize = mRecorder.read(mBuffer, 0, mBuffer.length);
                        for (int i = 0; i < readSize; i++) {
                            output.writeShort(mBuffer[i]);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (output != null) {
                        try {
                            output.flush();
                        } catch (IOException e) {
                            e.printStackTrace();

                        } finally {
                            try {
                                output.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }).start();
    }

    class Sample {
        AudioRecorder2Mp3Util util = null;
        String mp3 = null;
        String raw = null;

        void start() {

            if (util == null)
                util = new AudioRecorder2Mp3Util(null, "/sdcard/sample.raw", "/sdcard/sample.mp3");

            if (mp3 != null || raw != null) {
                util.cleanFile(RAW | MP3);
            }
            util.startRecording();
        }

        void stop() {
            boolean isOK = util.stopRecordingAndConvertFile();

            raw = util.getFilePath(RAW);
            mp3 = util.getFilePath(MP3);

            util.cleanFile(RAW);

            util.close();
        }
    }
}

压缩MP3使用的是lame库我直接分享出来拷到项目就可以直接用了
链接:http://pan.baidu.com/s/1kVcgqMz 密码:ezrc
下面是lame库的下载地址 jar包libs文件夹 .so文件放jniLibs文件夹你懂得~~

    原文作者:胖头是真的胖
    原文地址: https://www.jianshu.com/p/282a52f16326
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞