Example usage for com.badlogic.gdx.audio Music setOnCompletionListener

List of usage examples for com.badlogic.gdx.audio Music setOnCompletionListener

Introduction

In this page you can find the example usage for com.badlogic.gdx.audio Music setOnCompletionListener.

Prototype

public void setOnCompletionListener(OnCompletionListener listener);

Source Link

Document

Register a callback to be invoked when the end of a music stream has been reached during playback.

Usage

From source file:at.therefactory.jewelthief.JewelThief.java

License:Open Source License

/**
 * Either continues the music playback of a previously paused music file or proceeds to the next music file depending on the proceedToNext flag.
 * If there was no previous music file, a new music file is chosen randomly.
 * @param proceedToNext If set to true, a new music file is chosen randomly. If set to false, the previously paused file is being resumed.
 *///w w w. ja va2s.co m
public void playMusicFile(boolean proceedToNext) {

    // remember all available music files
    if (musicFiles == null) {
        FileHandle dirHandle = Gdx.files.internal("audio/music");
        FileHandle[] fileList = dirHandle.list();
        musicFiles = new String[fileList.length];
        for (int i = 0; i < musicFiles.length; i++) {
            musicFiles[i] = fileList[i].path();
            Gdx.app.log(getClass().getName(), "Found '" + fileList[i].path() + "'");
        }
    }

    if (musicFiles.length == 0) {
        Gdx.app.error(getClass().getName(), "Could not find any music files!");
    } else {
        // select a music file to play
        if (currentMusicFile == -1) {
            // if there is no previous music file, choose a new one randomly
            currentMusicFile = (short) Utils.randomWithin(0, musicFiles.length - 1);
            music = loadMusicAsset(musicFiles[currentMusicFile]);
        } else if (proceedToNext) {
            // switch to the next music file randomly
            int previousMusicFile = currentMusicFile;
            do {
                currentMusicFile = (short) Utils.randomWithin(0, musicFiles.length - 1);
            } while (previousMusicFile == currentMusicFile);
            assetManager.unload(musicFiles[previousMusicFile]); // free the resources of the previous music file
            if (music != null) {
                music.dispose();
                music = null;
            }
            music = loadMusicAsset(musicFiles[currentMusicFile]);
        } else {
            // resume previously paused music file
        }

        // play the selected music file
        music.play();
        music.setOnCompletionListener(new Music.OnCompletionListener() {
            @Override
            public void onCompletion(Music music) {
                playMusicFile(true);
            }
        });
    }
}

From source file:com.jupiter.europa.audio.LocalAudioService.java

License:Open Source License

public LocalAudioService() {
    this.musicSets = new HashMap<>();

    for (String type : AudioService.MUSIC_TYPES) {
        Path typeFolder = FileLocations.AUDIO_DIRECTORY.resolve(type);
        if (Files.exists(typeFolder) && Files.isDirectory(typeFolder)) {
            Set<Music> music = new HashSet<>();

            try (DirectoryStream<Path> paths = Files.newDirectoryStream(typeFolder)) {
                paths.iterator().forEachRemaining((Path musicFile) -> {
                    if (ArrayUtils.contains(MUSIC_EXTENSIONS, FileUtils.getExtension(musicFile))) {
                        Music instance = Gdx.audio.newMusic(new FileHandle(musicFile.toString()));
                        instance.setOnCompletionListener((Music music1) -> {
                            LocalAudioService.this.trackCompleted(music1);
                        });/*from  w  ww .  j ava 2s.  c om*/
                        music.add(instance);
                    }
                });
            } catch (IOException ex) {

            }

            this.musicSets.put(type, music.toArray(new Music[music.size()]));
        } else {
            this.musicSets.put(type, new Music[0]);
        }
    }
}