1
完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
最近一直接调国标28181协议,有关音频的也调了很久,发现服务端下发的音频,在Android端用AudioTrack播放,过了好几秒后就渐渐地出现电流声,而且电流声也越来越强。
其实这个问题,之前我的文章也有阐述过,不过后来发现是治标不治本orz,传送门: 文章所说就是把minBufSize设为4倍,这样前十几秒声音还正常,但随着时间递增,还是会出现电流声。 解决办法也不是很难,就是每隔一段时间,清理下内存,并且释放不必要的未播放的数据audioTrack.flush(),此方法文档的解析如下: //--------------------------------------------------------- // Audio data supply //-------------------- /** * Flushes the audio data currently queued for playback. Any data that has * been written but not yet presented will be discarded. No-op if not stopped or paused, * or if the track‘s creation mode is not {@link #MODE_STREAM}。 * 《BR》 Note that although data written but not yet presented is discarded, there is no * guarantee that all of the buffer space formerly used by that data * is available for a subsequent write. * For example, a call to {@link #write(byte[], int, int)} with 《code》sizeInBytes《/code》 * less than or equal to the total buffer size * may return a short actual transfer count. */ public void flush() { if (mState == STATE_INITIALIZED) { // flush the data in native layer native_flush(); mAvSyncHeader = null; mAvSyncBytesRemaining = 0; } } 最后贴上解决电流声的代码: public void playSound(int encodeType, int channel, int samplerate, int bitrate, int length, byte[] data){ //Log.e(“TAG”,“AAAAA》》》》》》encodeType=” + encodeType +“nchannel=” + channel +“nsamplerate=” + samplerate +“nbitrate=” + bitrate + “nlength=” + length + “ndata=” + data); if (audioTrack == null){ init(); frameCount = 0; Log.e(“TAG”,“voice to voice init complete!”); } frameCount++; if (frameCount % 5 == 0) { Log.i(“writeAudio”, “gc+”); audioTrack.flush(); System.gc(); Log.i(“writeAudio”, “gc-”); } if (length 》 0){ try{ audioTrack.write(data, 0, length); }catch (Exception e){ e.printStackTrace(); } } } 顺便说下,服务端传过来的音频数据,是G77.a的音频,然后我再转为pcm,最后再放到AudioTrack进行播放。 |
|
|
|
只有小组成员才能发言,加入小组>>
小黑屋| 手机版| Archiver| 德赢Vwin官网 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-31 02:02 , Processed in 0.610633 second(s), Total 73, Slave 57 queries .
Powered by 德赢Vwin官网 网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
德赢Vwin官网 观察
版权所有 © 湖南华秋数字科技有限公司
德赢Vwin官网 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号