电流
直播中

凤毛麟角

8年用户 906经验值
私信 关注
[问答]

为什么AudioTrack播放服务端传过来的音频有强电流声

为什么AudioTrack播放服务端传过来的音频有强电流声?有什么解决办法吗?

回帖(1)

卢颖萍

2021-9-30 10:23:10
  最近一直接调国标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进行播放。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分