有关AudioTrack播放服务端传过来的音频有强电流声的问题

最近一直接调国标28181协议,有关音频的也调了很久,发现服务端下发的音频,在Android端用AudioTrack播放,过了好几秒后就渐渐地出现电流声,而且电流声也越来越强。。。。

其实这个问题,之前我的文章也有阐述过,不过后来发现是治标不治本orz,传送门:https://blog.csdn.net/toyauko/article/details/84849997

 

文章所说就是把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进行播放。。。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值