简体   繁体   中英

Cannot change boolean value in method

I've got a little problem. I'm playing mp3 using Java sound sampled and I want to stop playing when I click the button. So I came up with something like this:

package sk.umb.osadnici.Client.Core.getterImages;

import java.io.File;
import java.io.IOException;

import java.io.InputStream;
import java.net.URL;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine.Info;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.FloatControl;

import javazoom.jl.player.advanced.AdvancedPlayer;

import static javax.sound.sampled.AudioSystem.getAudioInputStream;
import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;

public class GetterForBGMusic {
    private SourceDataLine line;
    private URL url;
    public URL bgUrl, bgUUUrl;
    private boolean canPlay = true;


    public void runMusic() {
        final GetterForBGMusic player = new GetterForBGMusic();
        player.play();
    }

    public void play() {
        URL inTTT = getClass().getResource("../sounds/bgMusic.mp3");

        try (AudioInputStream in = getAudioInputStream(inTTT)) {
            AudioFormat outFormat = getOutFormat(in.getFormat());
            Info info = new Info(SourceDataLine.class, outFormat);
            try (SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info)) {
                if (line != null) {
                    System.out.println(canPlay);
                    line.open(outFormat);
                    line.start();
                    stream(getAudioInputStream(outFormat, in), line);
                    line.drain();
                    line.stop();
                }
            }
        } catch (UnsupportedAudioFileException
                | LineUnavailableException
                | IOException e) {
            throw new IllegalStateException(e);
        }
    }

    private AudioFormat getOutFormat(AudioFormat inFormat) {
        final int ch = inFormat.getChannels();
        final float rate = inFormat.getSampleRate();
        return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
    }

    private void stream(AudioInputStream in, SourceDataLine line)
            throws IOException {
        while (true) {
            System.out.println(this.getCanPlay());
        }
    }

    public void setCanPlay(boolean play) {
        this.canPlay = play;
    }

    public boolean getCanPlay() {
        return canPlay;
    }

    private void booleanValue() {
        while (true)
            System.out.println(canPlay);
    }
}

Im using this code, if i call booleanValue method in constructor, everything is fine. but if call this method inside stream there is no change after value change.

Or can someone tell me how to stop this: http://odoepner.wordpress.com/2013/07/19/play-mp3-or-ogg-using-javax-sound-sampled-mp3spi-vorbisspi/

Your program is single-threaded, which means that it executes the sequence of "commands" you programmed from top to bottom.

For example, in this example

setCanPlay(true);
play(); //your for loop
setCanPlay(false);

the setCanPlay(false) instruction will only execute once the for loop has finished executing.

What you need is to have the for loop running in the background, and to be able to modify canPlay while the for loop is running. That's called multi-threading and you should lookup the classes Runnable, Task and Service in the java api doc to learn how to implement it.

You would end up with something like this:

setCanPlay(true);
play(); //your for loop, launched in another thread.
setCanPlay(false); // Executed while the for loop is running

That would start and end the playing instantly.

Multithreading is the only way to stop an executing program (from the outside).

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