关于安卓上pcm文件转wav全是噪音解决办法

1.一开始发现8bit的pcm能正常转换,但换成16bit转换出来全是噪音,网上资料也不全,思考了很久,突然想起大小端的问题,进行大小端处理后再进行转换,完美播放!

下面贴出大小端转换方法:

public class BigorLittle {

    public static String bigtolittle( String fileName) throws IOException {

        File file = new File(fileName);    //filename为pcm文件,请自行设置

        InputStream in = null;
        byte[] bytes = null;
        in = new FileInputStream(file);
        bytes = new byte[in.available()];//in.available()是得到文件的字节数
        int length=bytes.length;
        while (length!=1){
          long i=  in.read(bytes,0,bytes.length);
          if(i==-1){
             break;
          }
          length-=i;
        }

        int dataLength = bytes.length;
        int shortlength = dataLength / 2;
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes, 0, dataLength);
        ShortBuffer shortBuffer = byteBuffer.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();//此处设置大小端
        short[] shorts = new short[shortlength];
        shortBuffer.get(shorts, 0, shortlength);
        File file1 = File.createTempFile("pcm",null);//输出为临时文件
        String pcmtem = file1.getPath();
        FileOutputStream fos1 = new FileOutputStream(file1);
        BufferedOutputStream bos1 = new BufferedOutputStream(fos1);
        DataOutputStream dos1 = new DataOutputStream(bos1);
        for (int i = 0; i < shorts.length; i++) {
            dos1.writeShort(shorts[i]);

        }
        dos1.close();
        Log.d("gg", "bigtolittle: "+"="+shorts.length);
        return pcmtem;
    }

2.pcm转wav参考文章:

  https://blog.csdn.net/mcgrady_tracy/article/details/52502263

  https://blog.csdn.net/JenseaChen/article/details/46883319


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值