最近做一个声音文件,对方说是声音太小。找了个软件,也没看到哪里能够调整音量。怎么办?咱是程序员,写个程序吧。
试了一下,其实原来的声音不小,只是设备限制,所以播放声音小。于是吾将声音变小,效果就出来了。
放这里,供需要的程序员试试:
private short getShort(byte[] src, int start) { return (short)((src[start] & 0xFF) | (src[start+1] << 8)); } short SHRT_MAX = (short) 0x7F00; short SHRT_MIN = (short)-0x7F00; /** * 调节PCM数据音量 * src : * nLen : * dest : * nBitsPerSample: 16/8 * multiple: 放大倍数,如1.5 */ int amplifyPCMData(byte[] src, int nLen, byte[] dest, int nBitsPerSample, float multiple) { int nCur = 0; if (16 == nBitsPerSample) { while (nCur < nLen) { short volum = getShort(src, nCur); //Log.d(TAG, "volum="+volum); volum = (short)(volum * multiple); if (volum < SHRT_MIN) { volum = SHRT_MIN; } else if (volum > SHRT_MAX)//爆音的处理 { volum = SHRT_MAX; } dest[nCur] = (byte)( volum & 0xFF); dest[nCur+1] = (byte)((volum >> 8) & 0xFF); nCur += 2; } } /*else if (8 == nBitsPerSample) { while (nCur < nLen) { BYTE* volum = src + nCur; *volum = (*volum) * multiple; if (*volum > 255)//爆音的处理 { *volum = 255; } *src = *volum ; nCur ++; } } */ return 0; }