美文网首页android开发Android资源收录Android知识
利用Lame库实现在Android上录音MP3文件

利用Lame库实现在Android上录音MP3文件

作者: clam314 | 来源:发表于2017-03-27 10:41 被阅读2193次

之前项目需要实现MP3的录音,于是使用上了Lame这个库。这次做一个demo,使用AndroidStudio+Cmake+NDK进行开发。利用Android SDK提供的AndroidRecorder进行录音,得到PCM数据,并使用jni调用Lame这个C库将PCM数据转换为MP3文件。并使用MediaPlayer对录音的MP3文件进行播放。另外此次的按键是仿微信的语音按键,按下录音,松开结束,若中途上滑松开即取消

项目地址: https://github.com/clam314/LameMp3ForAndroid

Lame的移植,参考之前的文章:http://www.jianshu.com/p/065bfe6d3ec2

一、主要类的介绍
  • Mp3Recorder—— 是负责调用AudioRecorder进行录音的类
  • SimpleLame——是负责将MP3Recorder录制出的PCM数据转换成MP3文件
  • DataEncodeThread——是负责执行PCM转MP3的线程
  • LameMp3Manager——是对Mp3Recorder的多一次封装,增加了取消后删除之前录制的数据的逻辑
  • MediaPlayerUtil——是对系统的MediaPlayer进行简单的封装,使其只需要三步就可以播放音频文件
  • MediaRecorderButton ——是一个仿微信录音按键的控件,按下录制,松开结束,录制时上滑则取消录制
二、录制的流程
  1. Mp3Recorder调用startRecording()开始录制并初始化DataEncoderThread线程,并定期将录制的PCM数据,传入DataEncoderThread中。
  2. 在DataEncoderThread里,SimpleLame将Mp3Recorder传入的PCM数据转换成MP3格式并写入文件,其中SimpleLame通过jni对Lame库进行调用
  3. Mp3Recorder调用stopRecording()停止录制,并通知DataEncoderThread线程录制结束,DataEncoderThread将剩余的数据转换完毕。
三、主要的实现代码

Mp3Recorder

public class Mp3Recorder {
    static {
        System.loadLibrary("lamemp3");
    }
    //默认采样率
    private static final int DEFAULT_SAMPLING_RATE = 44100;
    //转换周期,录音每满160帧,进行一次转换
    private static final int FRAME_COUNT = 160;
    //输出MP3的码率
    private static final int BIT_RATE = 32;
    //根据资料假定的最大值。 实测时有时超过此值。
    private static final int MAX_VOLUME = 2000;
    private AudioRecord audioRecord = null;
    private int bufferSize;
    private File mp3File;
    private int mVolume;
    private short[] mPCMBuffer;
    private FileOutputStream os = null;
    private DataEncodeThread encodeThread;
    private int samplingRate;
    private int channelConfig;
    private PCMFormat audioFormat;
    private boolean isRecording = false;
    private ExecutorService executor = Executors.newFixedThreadPool(1);
    private OnFinishListener finishListener;

    public interface OnFinishListener {
        void onFinish(String mp3SavePath);
    }

    public Mp3Recorder(int samplingRate, int channelConfig, PCMFormat audioFormat) {
        this.samplingRate = samplingRate;
        this.channelConfig = channelConfig;
        this.audioFormat = audioFormat;
    }

    public Mp3Recorder() {
        this(DEFAULT_SAMPLING_RATE, AudioFormat.CHANNEL_IN_MONO, PCMFormat.PCM_16BIT);
    }


    public void startRecording(File mp3Save) throws IOException {
        if (isRecording) return;
        this.mp3File = mp3Save;
        if (audioRecord == null) {
            initAudioRecorder();
        }
        audioRecord.startRecording();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                isRecording = true;
                //循环的从AudioRecord获取录音的PCM数据
                while (isRecording) {
                    int readSize = audioRecord.read(mPCMBuffer, 0, bufferSize);
                    if (readSize > 0) {
                        //待转换的PCM数据放到转换线程中
                        encodeThread.addChangeBuffer(mPCMBuffer,readSize);
                        calculateRealVolume(mPCMBuffer, readSize);
                    }
                }
                // 录音完毕,释放AudioRecord的资源
                try {
                    audioRecord.stop();
                    audioRecord.release();
                    audioRecord = null;
                    // 录音完毕,通知转换线程停止,并等待直到其转换完毕
                    Message msg = Message.obtain(encodeThread.getHandler(), DataEncodeThread.PROCESS_STOP);
                    msg.sendToTarget();
                    encodeThread.join();
                    //转换完毕后回调监听
                    if(finishListener != null) finishListener.onFinish(mp3File.getPath());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    if (os != null) {
                        try {
                            os.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
        executor.execute(runnable);
    }

    public void stopRecording() throws IOException {
        isRecording = false;
    }

    //计算音量大小
    private void calculateRealVolume(short[] buffer, int readSize) {
        double sum = 0;
        for (int i = 0; i < readSize; i++) {
            sum += buffer[i] * buffer[i];
        }
        if (readSize > 0) {
            double amplitude = sum / readSize;
            mVolume = (int) Math.sqrt(amplitude);
        }
    }

    public int getVolume(){
        if (mVolume >= MAX_VOLUME) {
            return MAX_VOLUME;
        }
        return mVolume;
    }

    public int getMaxVolume(){
        return MAX_VOLUME;
    }

    public void setFinishListener(OnFinishListener listener){
        this.finishListener = listener;
    }

    private void initAudioRecorder() throws IOException {
        int bytesPerFrame = audioFormat.getBytesPerFrame();
        //计算缓冲区的大小,使其是设置周期帧数的整数倍,方便循环
        int frameSize = AudioRecord.getMinBufferSize(samplingRate, channelConfig, audioFormat.getAudioFormat()) / bytesPerFrame;
        if (frameSize % FRAME_COUNT != 0) {
            frameSize = frameSize + (FRAME_COUNT - frameSize % FRAME_COUNT);
        }
        bufferSize = frameSize * bytesPerFrame;

        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, samplingRate, channelConfig, audioFormat.getAudioFormat(), bufferSize);
        mPCMBuffer = new short[bufferSize];
        SimpleLame.init(samplingRate, 1, samplingRate, BIT_RATE);
        os = new FileOutputStream(mp3File);
        // 创建转码的线程
        encodeThread = new DataEncodeThread(os, bufferSize);
        encodeThread.start();
        //给AudioRecord设置刷新监听,待录音帧数每次达到FRAME_COUNT,就通知转换线程转换一次数据
        audioRecord.setRecordPositionUpdateListener(encodeThread, encodeThread.getHandler());
        audioRecord.setPositionNotificationPeriod(FRAME_COUNT);
    }
}

DataEncodeThread

public class DataEncodeThread extends Thread implements AudioRecord.OnRecordPositionUpdateListener {

    public static final int PROCESS_STOP = 1;
    private StopHandler handler;
    private byte[] mp3Buffer;
    //用于存取待转换的PCM数据
    private List<ChangeBuffer> mChangeBuffers = Collections.synchronizedList(new LinkedList<ChangeBuffer>());
    private FileOutputStream os;
    private CountDownLatch handlerInitLatch = new CountDownLatch(1);

    private static class StopHandler extends Handler {
        WeakReference<DataEncodeThread> encodeThread;

        public StopHandler(DataEncodeThread encodeThread) {
            this.encodeThread = new WeakReference<>(encodeThread);
        }

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == PROCESS_STOP) {
                DataEncodeThread threadRef = encodeThread.get();
                //录音停止后,将剩余的PCM数据转换完毕
                for (;threadRef.processData() > 0;);
                removeCallbacksAndMessages(null);
                threadRef.flushAndRelease();
                getLooper().quit();
            }
            super.handleMessage(msg);
        }
    }

    public DataEncodeThread(FileOutputStream os, int bufferSize) {
        this.os = os;
        mp3Buffer = new byte[(int) (7200 + (bufferSize * 2 * 1.25))];
    }

    @Override
    public void run() {
        Looper.prepare();
        handler = new StopHandler(this);
        handlerInitLatch.countDown();
        Looper.loop();
    }

    public Handler getHandler() {
        try {
            handlerInitLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
            Log.e(TAG, "Error when waiting handle to init");
        }
        return handler;
    }

    @Override
    public void onMarkerReached(AudioRecord recorder) {
        // Do nothing
    }

    @Override
    public void onPeriodicNotification(AudioRecord recorder) {
        //由AudioRecord进行回调,满足帧数,通知数据转换
        processData();
    }

    //从缓存区ChangeBuffers里获取待转换的PCM数据,转换为MP3数据,并写入文件
    private int processData() {
        if(mChangeBuffers != null && mChangeBuffers.size() > 0) {
            ChangeBuffer changeBuffer = mChangeBuffers.remove(0);
            short[] buffer = changeBuffer.getData();
            int readSize = changeBuffer.getReadSize();
            Log.d(TAG, "Read size: " + readSize);
            if (readSize > 0) {
                int encodedSize = SimpleLame.encode(buffer, buffer, readSize, mp3Buffer);
                if (encodedSize < 0) {
                    Log.e(TAG, "Lame encoded size: " + encodedSize);
                }
                try {
                    os.write(mp3Buffer, 0, encodedSize);
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(TAG, "Unable to write to file");
                }
                return readSize;
            }
        }
        return 0;
    }

    private void flushAndRelease() {
        final int flushResult = SimpleLame.flush(mp3Buffer);

        if (flushResult > 0) {
            try {
                os.write(mp3Buffer, 0, flushResult);
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void addChangeBuffer(short[] rawData, int readSize){
        mChangeBuffers.add(new ChangeBuffer(rawData, readSize));
    }

    private class ChangeBuffer{
        private short[] rawData;
        private int readSize;
        public ChangeBuffer(short[] rawData, int readSize){
            this.rawData = rawData.clone();
            this.readSize = readSize;
        }
        public short[] getData(){
            return rawData;
        }
        public int getReadSize(){
            return readSize;
        }
    }
}

SimpleLame 主要的逻辑是通过jni调用Lame库

public class SimpleLame {

    public native static void close();

    public native static int encode(short[] buffer_l, short[] buffer_r, int samples, byte[] mp3buf);

    public native static int flush(byte[] mp3buf);

    public native static void init(int inSampleRate, int outChannel, int outSampleRate, int outBitrate, int quality);

    public static void init(int inSampleRate, int outChannel, int outSampleRate, int outBitrate) {
        init(inSampleRate, outChannel, outSampleRate, outBitrate, 7);
    }
}
#include <cwchar>
#include "SimpleLame.h"
#include "lamemp3/lame.h"

static lame_global_flags *glf = NULL;

void Java_com_clam314_lame_SimpleLame_close(JNIEnv *env, jclass type){
    lame_close(glf);
    glf = NULL;
}

jint Java_com_clam314_lame_SimpleLame_encode(JNIEnv *env, jclass type, jshortArray buffer_l_,
                                        jshortArray buffer_r_, jint samples, jbyteArray mp3buf_) {
    jshort *buffer_l = env->GetShortArrayElements(buffer_l_, NULL);
    jshort *buffer_r = env->GetShortArrayElements(buffer_r_, NULL);
    jbyte *mp3buf = env->GetByteArrayElements(mp3buf_, NULL);

    const jsize mp3buf_size = env->GetArrayLength(mp3buf_);

    int result =lame_encode_buffer(glf, buffer_l, buffer_r, samples, (u_char*)mp3buf, mp3buf_size);

    env->ReleaseShortArrayElements(buffer_l_, buffer_l, 0);
    env->ReleaseShortArrayElements(buffer_r_, buffer_r, 0);
    env->ReleaseByteArrayElements(mp3buf_, mp3buf, 0);

    return result;
}

jint Java_com_clam314_lame_SimpleLame_flush(JNIEnv *env, jclass type, jbyteArray mp3buf_) {
    jbyte *mp3buf = env->GetByteArrayElements(mp3buf_, NULL);

    const jsize  mp3buf_size = env->GetArrayLength(mp3buf_);

    int result = lame_encode_flush(glf, (u_char*)mp3buf, mp3buf_size);

    env->ReleaseByteArrayElements(mp3buf_, mp3buf, 0);

    return result;
}

void Java_com_clam314_lame_SimpleLame_init__IIIII(JNIEnv *env, jclass type, jint inSampleRate, jint outChannel,
                                             jint outSampleRate, jint outBitrate, jint quality) {
    if(glf != NULL){
        lame_close(glf);
        glf = NULL;
    }
    glf = lame_init();
    lame_set_in_samplerate(glf, inSampleRate);
    lame_set_num_channels(glf, outChannel);
    lame_set_out_samplerate(glf, outSampleRate);
    lame_set_brate(glf, outBitrate);
    lame_set_quality(glf, quality);
    lame_init_params(glf);
}

相关文章

网友评论

  • 2742889678f5:大神问下,MP3转回pcm怎么处理?
  • LukeMi:你好,我运行了你的demo,完美运行,但是集成到我自己的上面:
    java.lang.UnsatisfiedLinkError: No implementation found for void com.xxx.xx.audio.lamebmp3.SimpleLame.ini,这种错误是怎么回事
  • 27efec53a72d:楼主,wave转MP3怎么弄?
  • 崔老板:这个是边录边转的吗,还是录制结束在转
  • 坚持编程_lyz:录出的文件太大了
  • 9c903a452951: java.lang.UnsatisfiedLinkError: Couldn't load lamemp3 from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.greenstone.exp-1.apk", zip file "/data/data/....exp/code_cache/secondary-dexes/......exp-1.apk.classes2.zip"],nativeLibraryDirectories=[/data/app-lib/.....exp-1, /vendor/lib, /system/lib]]]: findLibrary returned null
    楼主,我移植到自己项目中后就碰到了这个问题
  • c990e9d19f46:楼主:demo报错:java.io.IOException: Prepare failed.: status=0x1 at android.media.MediaPlayer._prepare(Native Method) 有解决办法?
  • 看看又不会怀孕:楼主,demo报错:Error:error: redeclaration of 'strchr' must have the 'overloadable' attribute 有解决办法吗?:pray:
    proud2008:有什么库可以剪切Mp3吗
    proud2008:有解决吗?我也遇到了,gradle:2.3.3'
    clam314:我这边没问题,需要更多的错误信息,就这点信息我也不清楚是哪里问题

本文标题:利用Lame库实现在Android上录音MP3文件

本文链接:https://www.haomeiwen.com/subject/szsynttx.html