简体   繁体   中英

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?

我不知道您使用的是哪个库,但是也许您可以在播放器类中找到一个“停止”方法。

see my answer to other similar question, just play one audio frame and check a flag if it should pause or stop playing

https://stackoverflow.com/a/16738486/2094045

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