Android 音频系统播放延迟时间获取(latency)

1.系统AudioManager类里面有一个隐藏接口:

可以用反射获取到系统播放硬件延迟

            AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            Method m = am.getClass().getMethod("getOutputLatency", int.class);
            int latencyMs = (Integer) m.invoke(am, AudioManager.STREAM_MUSIC);

2.系统AudioTrack类里面也有一个隐藏接口:

           
            if (SDK_INT >= 18) {
                  Method getLatencyMethod =android.media.AudioTrack.class.getMethod("getLatency", (Class < ? > []) null);

                  int audioLatencyMs = (int) ((Integer) getLatencyMethod.invoke(audioTrack, (Object[]) null));

            }

这个延迟是AudioTrack buffer延迟+系统播放硬件延迟,详解如下:

AudioTrack延迟底层分析:

uint32_t AudioTrack::latency() const
{
    return mLatency;
}

AudioTrack::createTrack函数中对mLatency进行了赋值:

 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;

其中afLatency是硬件的延迟。
(1000*mCblk->frameCount) / sampleRate,这一坨,是根据AudioTrack中的audio_track_cblk_t的buffer,计算AudioTrack buffer导致的延迟,也可以在Java层获取到buffer大小,去计算延迟时间。

afLatency就是AudioManager里面的getOutputLatency,证据如下 :和getOutputLatency底层实现一致。

    uint32_t afLatency;
    if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
        return NO_INIT;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值