你如何确定音频延迟(AudioTrack)在Android? [英] How do you determine the audio latency (AudioTrack) on Android?

查看:5074
本文介绍了你如何确定音频延迟(AudioTrack)在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序中,我使用了 AudioTrack 流模式播放动态生成的音频。该应用程序不必立即以输入的响应,所以延迟问题不要打扰我的计划的那一边。

I have an app in which I use an AudioTrack in streaming mode to play dynamically generated audio. The app doesn't have to respond instantaneously to inputs, so the latency issues don't bother me for that side of the program.

的问题是,我有需要为在同步'precisely尽可能与音频的动画并且看起来不同的设备有不同的时间量之间时 AudioTrack 停止阻塞的write()电话,要求更多的数据,而当这种声音从扬声器播放。

The problem is that I have an animation that needs to be as precisely 'in-sync' as possible with the audio and it seems that different devices have different amounts of time between when the AudioTrack stops blocking the write() call and asks for more data, and when that audio is played from the speaker.

我目前的解决方案,让我最的方式出现 - 我算我传递给 AudioTrack 到目前为止,并把它比作<帧数code> getPlaybackHeadPosition()。它基本上是这样:

My current solution gets me most of the way there -- I count the number of frames I've passed in to the AudioTrack so far, and compare it to getPlaybackHeadPosition(). It looks basically like:

long currentTimeInFrames = 0;
while(playingAudio) {
  currentTimeInFrames += numberOfFramesToWrite;
  long delayInFrames = (currentTimeInFrames - audioTrack.getPlaybackHeadPosition());
  audioTrack.write(frameBuffer,0,sampleSize);
  doAnimationAfterDelay(delayInFrames);
}

不过,还是有一些延迟的 getPlaybackHeadPosition()似乎并没有考虑到这由各个设备。

However, there's still some latency that getPlaybackHeadPosition() doesn't seem to account for that varies by device.

有没有办法来轮询系统的AudioTrack?

Is there a way to poll the system for the latency of the AudioTrack?

推荐答案

考虑驾驶者的反应时间。有隐藏的功能AudioManager.getOutputLatency(INT)得到这个。

Consider driver's latency. There's hidden function AudioManager.getOutputLatency(int) to get this.

这样称呼它:

AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
try{
   Method m = am.getClass().getMethod("getOutputLatency", int.class);
   latency = (Integer)m.invoke(am, AudioManager.STREAM_MUSIC);
}catch(Exception e){
}

我得到大约45 - 不同设备上的50毫秒。 使用结果的计算。

I get about 45 - 50 ms on different devices. Use the result in your calculations.

这篇关于你如何确定音频延迟(AudioTrack)在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆