Search code examples
javaaudiojlayer

How to stop a sound using JLayer


I'm using JLayer to play mp3 files in my game, but It's impossible for me to stop the song. That's my code:

public void play(final String sonido) {

    reproduciendo.put(sonido, 1);
    Thread thread = new Thread() {
        public void run() {
            while(reproduciendo.get(sonido)!=null) {
            try {
            Player player = new Player(getSonido(sonido));
            player.play();
            if(player.isComplete()) reproduciendo.remove(sonido);
            } catch(Exception e) {e.printStackTrace();};
        }}
    };

    thread.start();

}


public void stop (String sonido ) {
    reproduciendo.remove(sonido);
}

When I remove the song of the hashmap "reproduciendo" with the stop method it should stop, but nothing is done, I've read this way here, but it doesn't work for me. Do you know another way to do it?


Solution

  • I don't know what library you are using, but maybe you could find a "stop" method in your player class.