简体   繁体   中英

clip.stop(); not stopping audio from playing

I'm unable to get clip.stop() to stop the current sound unless it immediately follows

 clip.loop(Clip.LOOP_CONTINUOUSLY);

Here's a bit of the code that I'm currently using followed by code that does not produce a sound.

try {
    Clip clip = AudioSystem.getClip();
    if(clock == 2){
        clip.open(AudioSystem.getAudioInputStream(new File("res//battle.wav")));
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        System.out.println(clock);
    }
    if(clock >=300){
        clip.stop();
        xaxis+=100;
        yaxis+=100;
        System.out.println("we tried to stop");
    }

try {
    Clip clip = AudioSystem.getClip();
    if(clock == 2){
    clip.open(AudioSystem.getAudioInputStream(new File("res//battle.wav")));
    clip.loop(Clip.LOOP_CONTINUOUSLY);
    clip.stop();
    System.out.println(clock);
}

currently clock = 2; only occurs once, otherwise it would have the audio overlapping and taking up all the memory loading the music. Any help is appreciated. Thank you.

read this carefully:

"Stops the line. A stopped line should cease I/O activity. If the line is open and running, however, it should retain the resources required to resume activity. A stopped line should retain any audio data in its buffer instead of discarding it, so that upon resumption the I/O can continue where it left off, if possible. (This doesn't guarantee that there will never be discontinuities beyond the current buffer, of course; if the stopped condition continues for too long, input or output samples might be dropped.) If desired, the retained data can be discarded by invoking the flush method. When audio capture or playback stops, a STOP event is generated."

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