[关闭]
@DevWiki 2018-12-06T15:40:54.000000Z 字数 5530 阅读 4722

Android中Pcm文件转Amr文件

Android


更多内容详见:DevWiki's Blog

最经在做一个聊天的模块,聊天的模块很简单:

录音-上传-接收-播放

录音

录音部分可以采用MediaRecord和AudioRecord两个类进行录音.但是各有优缺点.

MediaRecord已经封装了很多方法,方便使用.
AudioRecord能获取录音的原生数据,以便对录音二次加工.

在录音过程我采用的是AudioRecord.但是AudioRecord录音数据是PCM格式,数据占用存储空间很大.必须压缩后才能传输.项目中的压缩是项目其他成员写的一个so库进行压缩的,有点不太方便.在网上翻阅了下,其实Android系统内部已经携带有压缩的库文件了.

压缩库

Android自带的有一个Pcm转amr的库:media_jni.so.
但是由于是Android系统内部的库,无法直接使用.根据网上的说明,最终终于弄明白如何使用了.

AmrInputStream

在要使用压缩库的项目中新建包:

  1. com.android.media

在此包中新建AmrInputStream类,代码如下:

  1. public final class AmrInputStream extends InputStream {
  2. static {
  3. System.loadLibrary("media_jni");
  4. }
  5. private final static String TAG = "AmrInputStream";
  6. // frame is 20 msec at 8.000 khz
  7. private final static int SAMPLES_PER_FRAME = 8000 * 20 / 1000;
  8. // pcm input stream
  9. private InputStream mInputStream;
  10. // native handle
  11. private int mGae;
  12. // result amr stream
  13. private byte[] mBuf = new byte[SAMPLES_PER_FRAME * 2];
  14. private int mBufIn = 0;
  15. private int mBufOut = 0;
  16. // helper for bytewise read()
  17. private byte[] mOneByte = new byte[1];
  18. /**
  19. * Create a new AmrInputStream, which converts 16 bit PCM to AMR
  20. * @param inputStream InputStream containing 16 bit PCM.
  21. */
  22. public AmrInputStream(InputStream inputStream) {
  23. mInputStream = inputStream;
  24. mGae = GsmAmrEncoderNew();
  25. GsmAmrEncoderInitialize(mGae);
  26. }
  27. @Override
  28. public int read() throws IOException {
  29. int rtn = read(mOneByte, 0, 1);
  30. return rtn == 1 ? (0xff & mOneByte[0]) : -1;
  31. }
  32. @Override
  33. public int read(byte[] b) throws IOException {
  34. return read(b, 0, b.length);
  35. }
  36. @Override
  37. public int read(byte[] b, int offset, int length) throws IOException {
  38. if (mGae == 0) throw new IllegalStateException("not open");
  39. // local buffer of amr encoded audio empty
  40. if (mBufOut >= mBufIn) {
  41. // reset the buffer
  42. mBufOut = 0;
  43. mBufIn = 0;
  44. // fetch a 20 msec frame of pcm
  45. for (int i = 0; i < SAMPLES_PER_FRAME * 2; ) {
  46. int n = mInputStream.read(mBuf, i, SAMPLES_PER_FRAME * 2 - i);
  47. if (n == -1) return -1;
  48. i += n;
  49. }
  50. // encode it
  51. mBufIn = GsmAmrEncoderEncode(mGae, mBuf, 0, mBuf, 0);
  52. }
  53. // return encoded audio to user
  54. if (length > mBufIn - mBufOut) length = mBufIn - mBufOut;
  55. System.arraycopy(mBuf, mBufOut, b, offset, length);
  56. mBufOut += length;
  57. return length;
  58. }
  59. @Override
  60. public void close() throws IOException {
  61. try {
  62. if (mInputStream != null) mInputStream.close();
  63. } finally {
  64. mInputStream = null;
  65. try {
  66. if (mGae != 0) GsmAmrEncoderCleanup(mGae);
  67. } finally {
  68. try {
  69. if (mGae != 0) GsmAmrEncoderDelete(mGae);
  70. } finally {
  71. mGae = 0;
  72. }
  73. }
  74. }
  75. }
  76. @Override
  77. protected void finalize() throws Throwable {
  78. if (mGae != 0) {
  79. close();
  80. throw new IllegalStateException("someone forgot to close AmrInputStream");
  81. }
  82. }
  83. //
  84. // AudioRecord JNI interface
  85. //
  86. private static native int GsmAmrEncoderNew();
  87. private static native void GsmAmrEncoderInitialize(int gae);
  88. private static native int GsmAmrEncoderEncode(int gae,
  89. byte[] pcm, int pcmOffset, byte[] amr, int amrOffset) throws IOException;
  90. private static native void GsmAmrEncoderCleanup(int gae);
  91. private static native void GsmAmrEncoderDelete(int gae);
  92. }

AmrEmcoder

只有AmrInputStream类是不够,还需要一个转码的类AmrEncoder,代码如下:

  1. public class AmrEncoder {
  2. public static void pcm2Amr(String pcmPath , String amrPath) {
  3. FileInputStream fis;
  4. try {
  5. fis = new FileInputStream(pcmPath);
  6. pcm2Amr(fis, amrPath);
  7. fis.close();
  8. } catch (FileNotFoundException e1) {
  9. e1.printStackTrace();
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. public static void pcm2Amr(InputStream pcmStream, String amrPath) {
  15. try {
  16. AmrInputStream ais = new AmrInputStream(pcmStream);
  17. OutputStream out = new FileOutputStream(amrPath);
  18. byte[] buf = new byte[4096];
  19. int len = -1;
  20. /*
  21. * 下面的AMR的文件头,缺少这几个字节是不行的
  22. */
  23. out.write(0x23);
  24. out.write(0x21);
  25. out.write(0x41);
  26. out.write(0x4D);
  27. out.write(0x52);
  28. out.write(0x0A);
  29. while((len = ais.read(buf)) >0){
  30. out.write(buf,0,len);
  31. }
  32. out.close();
  33. ais.close();
  34. } catch (FileNotFoundException e) {
  35. e.printStackTrace();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }

这里有两个方法:
1. pcm2Amr(String pcmPath , String amrPath): 将pcm文件转为amr文件
2. pcm2Amr(InputStream pcmStream, String amrPath): 将pcm数据流转为amr文件

测试

测试的界面

测试的界面很简单,就一个按钮一个文本显示,布局界面就不再给出,下面是MainActivity代码:

  1. public class MainActivity extends Activity implements OnClickListener{
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. initView();
  7. }
  8. private TextView hintView;
  9. private Button startButton;
  10. private void initView() {
  11. startButton = (Button) findViewById(R.id.btn_start);
  12. startButton.setOnClickListener(this);
  13. hintView = (TextView) findViewById(R.id.hint);
  14. }
  15. @Override
  16. public void onClick(View v) {
  17. if (v.getId() == R.id.btn_start) {
  18. transferButtonClicked();
  19. }
  20. }
  21. private void transferButtonClicked(){
  22. showWaitDialog();
  23. startTransfer();
  24. }
  25. private ProgressDialog waitDialog;
  26. private void showWaitDialog(){
  27. waitDialog = new ProgressDialog(this);
  28. waitDialog.setTitle(getResources().getString(R.string.transfer_wait_title));
  29. waitDialog.setMessage(getResources().getString(R.string.transfer_wait_message));
  30. waitDialog.show();
  31. }
  32. private void startTransfer() {
  33. new TransferThread(this, new TransferCallback() {
  34. @Override
  35. public void onSuccess() {
  36. transferSuccess();
  37. }
  38. @Override
  39. public void onFailed() {
  40. }
  41. }).start();
  42. }
  43. private void transferSuccess() {
  44. runOnUiThread(new Runnable() {
  45. @Override
  46. public void run() {
  47. waitDialog.dismiss();
  48. hintView.setText(getResources().getString(R.string.transfer_result));
  49. ToastUtil.showShort(MainActivity.this, R.string.success_hint);
  50. }
  51. });
  52. }
  53. }

转换线程

由于文件转换是耗时操作,所以需要一个转换线程来实现文件转换.

  1. public class TransferThread extends Thread{
  2. private TransferCallback callback;
  3. private Context context;
  4. public TransferThread(Context context, TransferCallback callback){
  5. this.callback = callback;
  6. this.context = context;
  7. }
  8. @Override
  9. public void run() {
  10. transfer();
  11. }
  12. private void transfer(){
  13. String rootPath = Environment.getExternalStorageDirectory().getPath();
  14. String amrPath = rootPath + "/test.amr";
  15. try {
  16. InputStream pcmStream = context.getAssets().open("test.pcm");
  17. AmrEncoder.pcm2Amr(pcmStream, amrPath);
  18. callback.onSuccess();
  19. } catch (IOException e) {
  20. callback.onFailed();
  21. e.printStackTrace();
  22. }
  23. }
  24. public static interface TransferCallback{
  25. void onSuccess();
  26. void onFailed();
  27. }
  28. }

测试结果

经过测试,160KB的test.pcm压缩后的amr文件大小为15KB,且可以正常播放.

本文的项目文件在此:Pcm2Amr

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注