简体   繁体   English

恢复时的Android AudioTrack延迟

[英]Android AudioTrack delay when resuming

I have an application that needs to pause and resume an AudioTrack quite frequently and this issue has been tested and seen on multiple devices. 我有一个需要频繁暂停和恢复AudioTrack的应用程序,并且已经在多个设备上测试并看到了此问题。 When the AudioTrack is played and paused, don't do anything in a few seconds, then resume the audio, then there's a delay, about 0.3 secs. 当AudioTrack播放和暂停时,几秒钟不执行任何操作,然后恢复音频,然后会有大约0.3秒的延迟。 If you then pause and resume it immediately, there will be no delay. 如果您暂停然后立即恢复,将不会有延迟。

Here's the logcat when there is no delay: (from cm-11-20140521-NIGHTLY-i9500) 这是没有延迟的日志记录器:(来自cm-11-20140521-NIGHTLY-i9500)

D/AudioFlinger( 2578): start(4098), calling pid 13180 session 3204
D/AudioFlinger( 2578): PAUSED => RESUMING (4098) on thread 0xb730ae40

Here's the logcat when there is delay: 这是延迟时的日志:

D/AudioFlinger( 2578): start(4098), calling pid 13180 session 3204
D/AudioFlinger( 2578): PAUSED => RESUMING (4098) on thread 0xb730ae40
V/audio_hw_primary( 2578): start_output_stream: starting stream
V/audio_hw_primary( 2578): start_output_stream: stream out device: 2, actual: 2

And I guess it's the start_output_stream thing or GC that causes the delay. 而且我猜想是造成延迟的原因是start_output_stream或GC。 Does anyone know how to fix this? 有谁知道如何解决这一问题?

PS Here's the background code: PS这是背景代码:

public boolean onTouch(View view, MotionEvent motionevent) {
    switch (motionevent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            pressed = true;
            (new Thread(new Runnable() {
                public void run() {
                    /* Generate savedTrack */
                    if (pressed) savedTrack.play();
                }
            })).start();
            return true;
        case MotionEvent.ACTION_UP:
            pressed = false;
            if (savedTrack != null) savedTrack.pause();
            return true;
    }
    return false;
}

Okay it turns out that if there is no STREAM_MUSIC audio playing for a while, the output device/stream will shut itself down and causes a delay when resuming the audio. 好的,事实证明,如果一段时间内没有STREAM_MUSIC音频播放,则输出设备/流将自行关闭,并在恢复音频时引起延迟。 So the solution is simple: play a muted looped track in the background when the app is foreground. 因此解决方案很简单:当应用程序为前台时,在后台播放静音的循环轨道。

Another thing I noticed is that if your AudioTrack is less than 16 samples, it won't loop. 我注意到的另一件事是,如果您的AudioTrack少于16个样本,它将不会循环播放。 Hope this help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM