Libgdx Sound - 示例尚未就绪

时间:2014-11-10 13:31:53

标签: java libgdx

我正在建立一个纸牌游戏,我有21个ogg短声音文件(one.ogg,two.ogg等等),这些都是在我的Assets类中加载的。在游戏过程中,大多数时候它播放声音没有问题,但有时它只是在logcat中声明“Sample xx not ready”。我不知道为什么,因为我以为我已经加载了我的Assets类中的所有声音。

我的资产课程如下:

public class Assets {

    // This is just a class that reads the annotation and extends LibGDX Assets class...
    public static final AnnotationAssetManager manager = new AnnotationAssetManager();

    public static SoundManager soundManager;

    @Asset(Sound.class)
    public static final String ace = "sound/ace.ogg", two = "sound/two.ogg", three = "sound/three.ogg",
                           four = "sound/four.ogg", five = "sound/five.ogg",
                           six = "sound/six.ogg", seven = "sound/seven.ogg",
                           eight = "sound/eight.ogg", nine = "sound/nine.ogg", ten = "sound/ten.ogg",
                           eleven = "sound/eleven.ogg", twelve = "sound/twelve.ogg",
                           thirteen = "sound/thirteen.ogg", fourteen = "sound/fourteen.ogg",
                           fifteen = "sound/fifteen.ogg", sixteen = "sound/sixteen.ogg",
                           seventeen = "sound/seventeen.ogg", eighteen = "sound/eighteen.ogg", 
                           nineteen = "sound/nineteen.ogg", twenty = "sound/twenty.ogg",
                           twentyone = "sound/twentyone.ogg";
    }

    public Assets() {
        soundManager = new SoundManager();
        soundManager.setVolume( 1f );
        soundManager.setEnabled( true  );
    }

    public static void dispose() {
        Assets.dispose();
    }
}

这是我的SoundManager类

public class SoundManager implements CacheEntryRemovedListener<String,Sound>, Disposable {

    private final LRUCache<String,Sound> soundCache;

    public SoundManager() {
        soundCache = new LRUCache<String,Sound>(10);
        soundCache.setEntryRemovedListener( this );
    }

    /**
     * Plays the specified sound.
     */
    public void play( String fileName ) {
        Sound soundToPlay = soundCache.get( fileName );
        if( soundToPlay == null ) {
            soundToPlay = Assets.manager.get( fileName, Sound.class);
            soundCache.add( fileName, soundToPlay );
        }
        soundToPlay.play( volume );
    }


    /**
     * Disposes the sound manager.
     */
    public void dispose() {
        for( Sound sound : soundCache.retrieveAll() ) {
            sound.stop();
            sound.dispose();
        }
    }

    @Override
    public void notifyEntryRemoved(String key, Sound value) {
        value.dispose();
    }
}

这就是我加载和播放声音的方式:

new Assets();
while( !Assets.manager.update() ) {
    System.out.println("Assets: " + Assets.manager.getProgress()*100 + "%");
}

Assets.getSoundManager().play( Assets.twelve );

我有什么想念吗?

1 个答案:

答案 0 :(得分:2)

在LibGDX中加载资源是非阻塞的,因此在调用get方法之前,最好先检查资产是否已加载。因此,在尝试调用get方法之前,需要调用isLoaded(String fileName)方法以避免样本未就绪错误。这是它应该是什么样子:

if(Assets.manager.isLoaded(fileName)) { soundToPlay = Assets.manager.get( fileName, Sound.class);
}

你也打电话

Assets.manager.finishLoading();

让经理一次加载所有资产。


对于LRUCache删除项目导致的错误,您不应该处置(或处理和重新加载)资产。没有必要为您的案例处理从LRUCache中删除的项目。 (关于它是如何做到的,检查类规范here,它将缓存中最少使用的元素标记为可由垃圾收集器收集,因此这基本上是为什么你的元素是“随机”的 - 但不是所有的时间它超出了它的大小 - 从缓存中删除。)