Android 使用mp3格式录音

这个Demo是用前文的Lame库写的,分两步走,第一步使用raw格式录音,第二步转换。部分代码抄自三星的demo.
android lame库:Android开发 使用Lame把音频文件转换成mp3格式


package com.pocketdigi.recordermp3;

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

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.pocketdigi.utils.FLameUtils;

public class MainActivity extends Activity {
	
	Button record,raw2mp3;
	boolean isRecording=false;
	private short[] mBuffer;
	AudioRecord mRecorder;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		record=(Button)findViewById(R.id.record);
		raw2mp3=(Button)findViewById(R.id.raw2mp3);
		initRecorder();
		final String tempFile=Environment.getExternalStorageDirectory()+"/aa.raw";
		final String mp3File=Environment.getExternalStorageDirectory()+"/bb.mp3";
		record.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(isRecording)
				{
					record.setText("录音");
					isRecording=false;
					mRecorder.stop();
				}else{
					record.setText("停止");
					isRecording=true;
					mRecorder.startRecording();
					startBufferedWrite(new File(tempFile));
				}
			}
		});
		raw2mp3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(isRecording)
				{
					return;
				}
				File file=new File(tempFile);
				if(!file.exists())
				{
					return;
				}
				new Thread()
				{
					public void run()
					{
						FLameUtils lameUtils=new FLameUtils(1, 16000, 96);
						System.out.println(lameUtils.raw2mp3(tempFile, mp3File));
					}
				}.start();
				
			}
		});

	}
	/**
	 * 初始化Recorder
	 */
	public void initRecorder()
	{
		int bufferSize = AudioRecord.getMinBufferSize(16000, AudioFormat.CHANNEL_IN_MONO,
				AudioFormat.ENCODING_PCM_16BIT);
		mBuffer = new short[bufferSize];
		mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 16000, AudioFormat.CHANNEL_IN_MONO,
				AudioFormat.ENCODING_PCM_16BIT, bufferSize);
	}
	/**
	 * 写入到文件
	 * @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 (isRecording) {
						int readSize = mRecorder.read(mBuffer, 0, mBuffer.length);
						for (int i = 0; i < readSize; i++) {
							output.writeShort(mBuffer[i]);
						}
					}
				} catch (IOException e) {
					Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
				} finally {
					if (output != null) {
						try {
							output.flush();
						} catch (IOException e) {
							Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
						} finally {
							try {
								output.close();
							} catch (IOException e) {
								Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
							}
						}
					}
				}
			}
		}).start();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mRecorder.release();
	}

}

© 2013, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记