Package game.audioPlayer

Source Code of game.audioPlayer.AudioPlayer

package game.audioPlayer;

import game.core.Collectable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.advanced.AdvancedPlayer;

/**
* Cette classe permet de jouer une musique, elle devra etre collectée par le collecteur afin d'etre stoppée automatiquement
* @author mastersnes
*/
public class AudioPlayer implements Collectable {

    private AdvancedPlayer player;
    private final String musiquePath;
    private Thread t;

    /**
     * Creer un nouveau lecteur avec la musique passée en parametre
     * @param musiquePath
     */
    public AudioPlayer(final String musiquePath) {
        try {
            player = new AdvancedPlayer(new FileInputStream(musiquePath));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(AudioPlayer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JavaLayerException ex) {
            Logger.getLogger(AudioPlayer.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.musiquePath = musiquePath;
    }

    /**
     * Demarre la lecture
     */
    public void start() {
        t = new Thread() {

            @Override
            public void run() {
                try {
                    player.play(0, Integer.MAX_VALUE);
                } catch (JavaLayerException ex) {
                    Logger.getLogger(AudioPlayer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        t.start();
    }

    @Override
    public void stop() {
        player.close();
    }
}
TOP

Related Classes of game.audioPlayer.AudioPlayer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.