Android将录制的.raw录音文件转为.mp3格式的文件

1.部分变量声明

 /**
    * 采样频率 通用:44100 此处:8000
    */
    private static final int SAMPLE_RATE = 8000;
/**
    * 录音需要的一些变量
    */
    private short[] mBuffer;
    private AudioRecord mRecorder;
/**
    * 是否转换ok
    */
    private boolean convertOk = false;
    private int volume=0;

2.初始化AudioRecorder并开始录制.raw文件

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);
File file = new File(dir,rawPath);

mCurrentFilePath = file.getAbsolutePath();//raw文件路径
mRecorder.startRecording();
startBufferedWrite(file);
        /**
         * 写入到raw文件
         * 
         * @param file
         */
        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 (isPrepared) {
                            int readSize = mRecorder.read(mBuffer, 0,
                                    mBuffer.length);
                            long v = 0;
                            for (int i = 0; i < readSize; i++) {
                                output.writeShort(mBuffer[i]);
                                v += mBuffer[i]*mBuffer[i];
                            }
                            double mean = v/(double)readSize;
                            volume = (int) (10 * Math.log10(mean));
//                          Log.e("-----音量AudioManger-----", ""+volume);
                        }
                    } 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();
        }

3.停止录音,转换格式

 /**
         * 停止录音,并且转换文件,<br/>
         * <b>这很可能是个耗时操作,建议在后台中做
         * 
         * @return
         */
        public boolean stopRecordingAndConvertFile() {
            if (!isPrepared) {
                return isPrepared;
            }

            // 停止
            mRecorder.stop();
            isPrepared = false;

            // 开始转换
            FLameUtils lameUtils = new FLameUtils(1, SAMPLE_RATE, 16);
            convertOk = lameUtils.raw2mp3(mCurrentFilePath, mp3Path);//.raw文件路径,.mp3文件路径
            if (convertOk) {//删除源文件
                 if(mCurrentFilePath != null) {
                        File file = new File(mCurrentFilePath);
                        file.delete();
                        mCurrentFilePath = null;
                    }
            }

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

注:以上仅为部分重要代码,格式转换时,需要用的第三方flame.jar包。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值