@DevWiki
2018-12-06T07:40:54.000000Z
字数 5530
阅读 4892
Android
更多内容详见:DevWiki's Blog
最经在做一个聊天的模块,聊天的模块很简单:
录音-上传-接收-播放
录音部分可以采用MediaRecord和AudioRecord两个类进行录音.但是各有优缺点.
MediaRecord已经封装了很多方法,方便使用.
AudioRecord能获取录音的原生数据,以便对录音二次加工.
在录音过程我采用的是AudioRecord.但是AudioRecord录音数据是PCM格式,数据占用存储空间很大.必须压缩后才能传输.项目中的压缩是项目其他成员写的一个so库进行压缩的,有点不太方便.在网上翻阅了下,其实Android系统内部已经携带有压缩的库文件了.
Android自带的有一个Pcm转amr的库:media_jni.so.
但是由于是Android系统内部的库,无法直接使用.根据网上的说明,最终终于弄明白如何使用了.
在要使用压缩库的项目中新建包:
com.android.media
在此包中新建AmrInputStream类,代码如下:
public final class AmrInputStream extends InputStream {static {System.loadLibrary("media_jni");}private final static String TAG = "AmrInputStream";// frame is 20 msec at 8.000 khzprivate final static int SAMPLES_PER_FRAME = 8000 * 20 / 1000;// pcm input streamprivate InputStream mInputStream;// native handleprivate int mGae;// result amr streamprivate byte[] mBuf = new byte[SAMPLES_PER_FRAME * 2];private int mBufIn = 0;private int mBufOut = 0;// helper for bytewise read()private byte[] mOneByte = new byte[1];/*** Create a new AmrInputStream, which converts 16 bit PCM to AMR* @param inputStream InputStream containing 16 bit PCM.*/public AmrInputStream(InputStream inputStream) {mInputStream = inputStream;mGae = GsmAmrEncoderNew();GsmAmrEncoderInitialize(mGae);}@Overridepublic int read() throws IOException {int rtn = read(mOneByte, 0, 1);return rtn == 1 ? (0xff & mOneByte[0]) : -1;}@Overridepublic int read(byte[] b) throws IOException {return read(b, 0, b.length);}@Overridepublic int read(byte[] b, int offset, int length) throws IOException {if (mGae == 0) throw new IllegalStateException("not open");// local buffer of amr encoded audio emptyif (mBufOut >= mBufIn) {// reset the buffermBufOut = 0;mBufIn = 0;// fetch a 20 msec frame of pcmfor (int i = 0; i < SAMPLES_PER_FRAME * 2; ) {int n = mInputStream.read(mBuf, i, SAMPLES_PER_FRAME * 2 - i);if (n == -1) return -1;i += n;}// encode itmBufIn = GsmAmrEncoderEncode(mGae, mBuf, 0, mBuf, 0);}// return encoded audio to userif (length > mBufIn - mBufOut) length = mBufIn - mBufOut;System.arraycopy(mBuf, mBufOut, b, offset, length);mBufOut += length;return length;}@Overridepublic void close() throws IOException {try {if (mInputStream != null) mInputStream.close();} finally {mInputStream = null;try {if (mGae != 0) GsmAmrEncoderCleanup(mGae);} finally {try {if (mGae != 0) GsmAmrEncoderDelete(mGae);} finally {mGae = 0;}}}}@Overrideprotected void finalize() throws Throwable {if (mGae != 0) {close();throw new IllegalStateException("someone forgot to close AmrInputStream");}}//// AudioRecord JNI interface//private static native int GsmAmrEncoderNew();private static native void GsmAmrEncoderInitialize(int gae);private static native int GsmAmrEncoderEncode(int gae,byte[] pcm, int pcmOffset, byte[] amr, int amrOffset) throws IOException;private static native void GsmAmrEncoderCleanup(int gae);private static native void GsmAmrEncoderDelete(int gae);}
只有AmrInputStream类是不够,还需要一个转码的类AmrEncoder,代码如下:
public class AmrEncoder {public static void pcm2Amr(String pcmPath , String amrPath) {FileInputStream fis;try {fis = new FileInputStream(pcmPath);pcm2Amr(fis, amrPath);fis.close();} catch (FileNotFoundException e1) {e1.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void pcm2Amr(InputStream pcmStream, String amrPath) {try {AmrInputStream ais = new AmrInputStream(pcmStream);OutputStream out = new FileOutputStream(amrPath);byte[] buf = new byte[4096];int len = -1;/** 下面的AMR的文件头,缺少这几个字节是不行的*/out.write(0x23);out.write(0x21);out.write(0x41);out.write(0x4D);out.write(0x52);out.write(0x0A);while((len = ais.read(buf)) >0){out.write(buf,0,len);}out.close();ais.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
这里有两个方法:
1. pcm2Amr(String pcmPath , String amrPath): 将pcm文件转为amr文件
2. pcm2Amr(InputStream pcmStream, String amrPath): 将pcm数据流转为amr文件
测试的界面很简单,就一个按钮一个文本显示,布局界面就不再给出,下面是MainActivity代码:
public class MainActivity extends Activity implements OnClickListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private TextView hintView;private Button startButton;private void initView() {startButton = (Button) findViewById(R.id.btn_start);startButton.setOnClickListener(this);hintView = (TextView) findViewById(R.id.hint);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.btn_start) {transferButtonClicked();}}private void transferButtonClicked(){showWaitDialog();startTransfer();}private ProgressDialog waitDialog;private void showWaitDialog(){waitDialog = new ProgressDialog(this);waitDialog.setTitle(getResources().getString(R.string.transfer_wait_title));waitDialog.setMessage(getResources().getString(R.string.transfer_wait_message));waitDialog.show();}private void startTransfer() {new TransferThread(this, new TransferCallback() {@Overridepublic void onSuccess() {transferSuccess();}@Overridepublic void onFailed() {}}).start();}private void transferSuccess() {runOnUiThread(new Runnable() {@Overridepublic void run() {waitDialog.dismiss();hintView.setText(getResources().getString(R.string.transfer_result));ToastUtil.showShort(MainActivity.this, R.string.success_hint);}});}}
由于文件转换是耗时操作,所以需要一个转换线程来实现文件转换.
public class TransferThread extends Thread{private TransferCallback callback;private Context context;public TransferThread(Context context, TransferCallback callback){this.callback = callback;this.context = context;}@Overridepublic void run() {transfer();}private void transfer(){String rootPath = Environment.getExternalStorageDirectory().getPath();String amrPath = rootPath + "/test.amr";try {InputStream pcmStream = context.getAssets().open("test.pcm");AmrEncoder.pcm2Amr(pcmStream, amrPath);callback.onSuccess();} catch (IOException e) {callback.onFailed();e.printStackTrace();}}public static interface TransferCallback{void onSuccess();void onFailed();}}
经过测试,160KB的test.pcm压缩后的amr文件大小为15KB,且可以正常播放.
本文的项目文件在此:Pcm2Amr