android录音格式raw转mp3

主要使用到LAME 这个东东,需要用到jni;要装NDK ,要编译so库文件.要这个那个.. 麻烦的很.... 

果断各种google,然后找到个别人编译好的so和jar包,爽歪歪;

用非很简单:

FLameUtils mFLameUtils = new FLameUtils(1, sampleRateInHz, 96);
			mFLameUtils.raw2mp3(lovaPath, path);
调他封装好的 工具类FLameUtils,然后传入对应的参数,相信用过AudioRecord 的都看得懂参数都是什么;这里说几个注意事项:

1.录音时, 读取数据记得用short[]类型,不然格式转换后会是噪音,short[] buffDate = new short[minBufferSizeInBytes];

2.导入工具类时记得要和他原来的包名一样,不然会调用不到本地jni方法;

3.格式转换记得丢在线程里,不然..你懂得;

4.格式转换完成后记得删掉原数据(raw原数据比较大,我记得录了12秒就1M 了... 我用的44100码率);

下面是我用的录音和格式转换代码:


	/**
	 * 开始录音
	 */
	public void startRecord() {
		if (mAudioRecord != null && !isRecording()) {
			if (mAudioRecord.getState() == AudioRecord.STATE_INITIALIZED) {
				LogUtil.i(TAG, "开始录音...");
				new StartRecordThread().start();
				isRecording = true;
			} else {
				LogUtil.e(TAG, "mAudioRecord=STATE_UNINITIALIZED");
			}
		}
	}
	
	/**
	 * 
	 ************************************************************************** 
	 * @Version
	 * @ClassName: AudioRecordHelper.StartRecordThread
	 * @Description: 录音线程
	 * @Author fans
	 *************************************************************************** 
	 */
	class StartRecordThread extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			mAudioRecord.startRecording();
			writeDateToFile(lovaPath);
		}
	}
	
	/**
	 * 写入原数据
	 * 
	 * @param path
	 */
	private void writeDateToFile(String path) {
		short[] buffDate = new short[minBufferSizeInBytes];
		DataOutputStream dos = null;
		int readSize = 0;
		try {
			File recordFile = new File(path);
			if (recordFile.exists()) {
				recordFile.delete();
				recordFile.createNewFile();
			}
			dos = new DataOutputStream(new BufferedOutputStream(
					new FileOutputStream(recordFile)));
			while (isRecording()) {
				readSize = mAudioRecord.read(buffDate, 0, minBufferSizeInBytes);
				for (int i = 0; i < readSize; i++) {
					dos.writeShort(buffDate[i]);
				}
				LogUtil.i(TAG, "录入音频大小:" + buffDate.length);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (dos != null) {
				try {
					dos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 
	 ************************************************************************** 
	 * @Version
	 * @ClassName: AudioRecordHelper.Raw2Mp3Thread
	 * @Description: 原数据转MP3
	 * @Author fans
	 *************************************************************************** 
	 */
	class Raw2Mp3Thread extends Thread {
		String path;

		public Raw2Mp3Thread(String path) {
			this.path = path;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			String drPath = path.substring(0, path.lastIndexOf("/"));
			LogUtil.i(TAG, "储存目录:" + drPath);
			File file = new File(drPath);
			if (!file.exists()) {
				file.mkdirs();
			}
			LogUtil.i(TAG, "录音原始数据:" + lovaPath + ";MP3数据:" + path);
			/* 原始raw文件转换成MP3 */
			if (new File(lovaPath).length() < 1) {
				LogUtil.e(TAG, "原始数据大小为0");
				return;
			}
			File loveFile = new File(lovaPath);
			LogUtil.i(TAG, "MP3格式转换开始...;文件大小=" + loveFile.length());
			FLameUtils mFLameUtils = new FLameUtils(1, sampleRateInHz, 96);
			mFLameUtils.raw2mp3(lovaPath, path);
			LogUtil.i(TAG,
					"转换完成:" + path + ";MP3文件大小=" + new File(path).length());
			loveFile.delete();
			CJApplication.getInstance().sendBroadcast(
					new Intent(CJConstant.RECORD_FINISH_ACTION));
		}
	}

工具类和库文件下载地址:下载地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值