声音使Android上的应用滞后-任何解决方案? [英] Sound makes app lags on Android - any solution?

查看:123
本文介绍了声音使Android上的应用滞后-任何解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的游戏在PC上流畅运行(赢得10),但在android手机(Mi 9T pro,SD 855、6GB ram)上却落后了.我并不是说声音只会滞后,图形也会滞后.当我禁用声音时,它运行得非常顺畅(应该如此).

So my game runs smoothly on PC (win 10), but lags on android phone (Mi 9T pro, SD 855, 6GB of ram). I don't mean that only sound lags, but graphics also. When I disable sounds it runs perfectly smooth (as it should).

是的,我知道这个问题已经问过很多次了.我尝试了所有提供的解决方案",但没有任何帮助.使用22KHz样本,44.1Khz,48Khz进行了尝试.尝试在每个样本的末尾添加1秒的静音.没有帮助.

Yes I know that this question is asked and answered many times. And I tried all offered "solutions", but nothing helps. Tried using 22KHz samples, 44.1Khz, 48Khz. Tried adding 1 second of silence at end of each sample. Didn't help.

我正在使用这种方法加载样品:

I'm loading samples with this method:

private void loadSounds(String path)
{
    FileHandle dirHandle;
    dirHandle = Gdx.files.internal(path);
    for (FileHandle entry: dirHandle.list()) {
        Sound sound = new Sound(entry.nameWithoutExtension(), path+"/"+entry.name());
        sounds.add(sound);
    }
}

声音类如下所示:

public class Sound {
    public String name;
    private com.badlogic.gdx.audio.Sound soundObject;

public Sound(String name, String filePath) {
    this.name = name;
    soundObject = Gdx.audio.newSound(Gdx.files.internal(filePath));
}

public void play()
{
    soundObject.play();
}


public void play (float pitch)
{
    soundObject.play(1,pitch,0);
}


public void stop()
{
    soundObject.stop();
}

public void dispose()
{
    soundObject.dispose();
}

}

只是一种通常的方式,没有什么特别的.所以我要在应用启动时预加载所有声音样本,然后在游戏中播放它们.玩是造成滞后,这很明显. 任何有关如何摆脱android上的这种滞后的建议都值得赞赏.

Just a usual way, nothing special. So I'm pre-loading all the sounds samples when app starts and playing them during the game. Playing is causing the lag, to be clear. Any advice on how to get rid of this lag on android is appreciated.

推荐答案

使用更具限制性的声音管理器类解决了此问题.

Solved this with more restrictive sound manager class.

首先,在加载声音样本期间,我要计算每个声音样本的持续时间(大约).原因是LibGDX Sound类未提供该功能.由于我所有的样本都采用相同的格式,因此计算就足够了:

First during loading sound sample I'm calculating how long (approximately) each sound sample lasts. Reason for this is that LibGDX Sound class doesn't provide that functionality. Since all my samples are in same format calculation is good enough:

final long WAV_HEADER_SIZE = 2415;
final long WAV_BPS = 41137;
FileHandle file = Gdx.files.internal(filePath);
duration = (long)(1000f * (file.length() - WAV_HEADER_SIZE) / WAV_BPS);

所以我以这种方式获得采样持续时间(以毫秒为单位).采样率为22Khz,352Kbps.

So I'm getting this way sample duration in milliseconds. Samples are 22Khz, 352Kbps.

然后,每当我播放声音时,我都会记住播放的开始时间,因此只要使用此持续时间变量,我就可以计算出声音是否还在播放.

Then, when ever I play some sound I remember the time playing started so with this duration variable at any time I can calculate is sound still playing or not.

这样我可以检查我同时播放了多少声音.当时我最多可以到达8个电话.而且这种情况很少发生,并且不会持续很长时间.常见样本数为0到4或5.

With this I could check how many sounds I'm playing at the same time. Max number I ever reached is 8 at the time. And that happens very rarely and doesn't last long. Common number of samples is 0 to 4 or 5.

然后,我为每个播放调用(值1-10)添加了优先级"字段,在播放声音之前,我正在计算的声音旁边是当前正在播放的采样数,而该声音的优先级最低.

Then, I added "priority" field for each play call (values 1 - 10) and before I play the sound I'm calculating beside number of currently playing sample also which sounds has lowest priority.

    final int MAX_PRIORITY = 10;
    final int MAX_NUMBER_OF_SOUNDS_PLAYED_SIMULTANEITY = 2;

    if (muted > 0 || soundName.isEmpty()) return;
    Array<Sound> matchedSounds = new Array<Sound>(10);
    int playingCount = 0;

    int lowestPriority = MAX_PRIORITY + 1;
    int lowestPriorityIndex = -1;

    for (int i = 0; i<sounds.size; i++){
        Sound sound = sounds.get(i);
        if (sound.isPlaying()){
            if (sound.priority < lowestPriority){
                lowestPriority = sound.priority;
                lowestPriorityIndex = i;
            }
            playingCount++;
        }
        if (sound.name.startsWith(soundName)) {
            matchedSounds.add(sounds.get(i));
        }
    }

    if (playingCount > MAX_NUMBER_OF_SOUNDS_PLAYED_SIMULTANEITY){
        if (priority <= lowestPriority){
            return;
        }else{
            sounds.get(lowestPriorityIndex).stop();
        }
    }

因此,我首先检查允许的声音数量是否大于当前播放的声音数量.如果不是,我会定期播放新声音.

So I'm checking first if number of allowed sounds is bigger of number of currently played sounds. If it's not I'm playing new sound regularly.

如果是这样,并且新声音的优先级低于当前正在播放的所有声音,那么我只是忽略新声音.

If it is and new sounds has lower priority than all currently playing sounds I'm just ignoring new sound.

如果是,并且新声音不是最低优先,我将停止最低声音并播放新声音.

If it is and new sounds isn't lowest with priority I'm stopping lowest one and playing new one.

这样,我会将当前正在播放的声音数量保持固定.

That way I'm keeping number of currently playing sounds to fixed amount.

最大的意外是,当时没有滞后的最大样本数是2个!播放3种或更多声音时,哪种声音系统会滞后?!?这里真的有问题.

And greatest surprise is that maximum number of samples that are not making a lag is 2 at the time ?!?! What kind of sounds system lags when playing 3 or more sounds?!? Something is really wrong here.

无论如何,即使一次最多采样2个声音,听起来也没问题.丢失的声音并不明显.

Anyway even with max of 2 samples at the time sound sounds ok.... Missing sounds are not noticeable.

这篇关于声音使Android上的应用滞后-任何解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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