简体   繁体   中英

Libgdx Sound - Sample not ready

I am building a card game and I've got 21 ogg short sound files (one.ogg, two.ogg, etc..) which are loaded in my Assets class. During the gameplay, most of the time it plays the sound with no issue but sometime it just state "Sample xx not ready" in the logcat. I have no idea why since I thought I have already loaded all the sounds from my Assets class.

My Assets class is as follow:

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();
    }
}

This is my SoundManager class

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();
    }
}

So this is how I load and play a sound:

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

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

Is there anything I have miss?

Loading assets in LibGDX are non-blocking, so you better check if an asset is loaded before calling the get method. So, before trying to call the get method, you need to call the isLoaded(String fileName) method to avoid the sample not ready error. Here is how it should look like:

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

You an also call

Assets.manager.finishLoading();

to let the manager load all the assets at once.


For errors caused by the LRUCache's removal of items, you should not dispose (or dispose and reload as described above) the assets. There is no point disposing the items removed from the LRUCache for your case. (For how it does this, check the class specification here , it marks the least used elements in the cache as collectable by the garbage collector, so this is basically why your elements are "randomly" -but not all the time it exceeds its size- removed from the cache.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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