nicolasgramlich / AndEngine

Free Android 2D OpenGL Game Engine
http://www.andengine.org
Apache License 2.0
3.18k stars 1.35k forks source link

MusicFactory: AssetFileDescriptor closed and method to load music async #205

Open flomin opened 11 years ago

flomin commented 11 years ago

This patch add close() method to the file descriptors after the setDataSource call: according to the Android documentation of MediaPlayer.setDataSource: "It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns."

This patch also add a new method to load the Music asynchronously like this:

MusicFactory.createMusicFromAssetAsync(aMusicManager, aContext, aFilename, new IMusicLoadedListener() {
    @Override
    public boolean onError(MediaPlayer pPlayer, int pWhat, int pExtra) {
        Debug.e(LOG_TAG, "createMusicFromAssetAsync() KO: MediaPlayer.onErrorListener(...) : " + pWhat + ": " + pExtra);
        return true;
    }

    @Override
    public void onMusicLoaded(final Music pMusic) {
        if (pMusic != null) {
            pMusic.play();
        }
    }
});