侧边栏壁纸
博主头像
DevWiki博主等级

不怕慢,只怕站!

  • 累计撰写 156 篇文章
  • 累计创建 90 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Android中Pcm文件转Amr文件

DevWiki
2015-06-23 / 0 评论 / 0 点赞 / 7 阅读 / 9629 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2024-03-31,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

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

录音-上传-接收-播放

录音

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

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

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

压缩库

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

AmrInputStream

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

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 khz
    private final static int SAMPLES_PER_FRAME = 8000 * 20 / 1000;
     
    // pcm input stream
    private InputStream mInputStream;
     
    // native handle
    private int mGae;
     
    // result amr stream
    private 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);
    }

    @Override
    public int read() throws IOException {
        int rtn = read(mOneByte, 0, 1);
        return rtn == 1 ? (0xff & mOneByte[0]) : -1;
    }
     
    @Override
    public int read(byte[] b) throws IOException {
        return read(b, 0, b.length);
    }

    @Override
    public int read(byte[] b, int offset, int length) throws IOException {
        if (mGae == 0) throw new IllegalStateException("not open");
     
        // local buffer of amr encoded audio empty
        if (mBufOut >= mBufIn) {
            // reset the buffer
            mBufOut = 0;
            mBufIn = 0;
         
            // fetch a 20 msec frame of pcm
            for (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 it
            mBufIn = GsmAmrEncoderEncode(mGae, mBuf, 0, mBuf, 0);
        }
  
        // return encoded audio to user
        if (length > mBufIn - mBufOut) length = mBufIn - mBufOut;
        System.arraycopy(mBuf, mBufOut, b, offset, length);
        mBufOut += length;
     
        return length;
    }

    @Override
    public 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;
                }
            }
        }
   }

    @Override
    protected 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);
}

AmrEmcoder

只有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{

    @Override
    protected 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);
	}

	@Override
	public 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() {
		
			@Override
			public void onSuccess() {
				transferSuccess();
			}

			@Override
			public void onFailed() {
			}
		}).start();
	}

	private void transferSuccess() {
		runOnUiThread(new Runnable() {
		
			@Override
			public 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;
	}

	@Override
	public 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


重要说明

想随时获取最新博客文章更新,请关注公共账号DevWiki,或扫描下面的二维码:

微信公共号

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区