简体   繁体   中英

Java Clip (Clip.Stop not working)

I make a video games on eclipse and i will make a menu song and stop the song but clip.stop(); not work i don't know why please help me. The menu song Clip.play();and Clip.loop(Clip.LOOP_CONTINUOUSLY); work but i can stop the song only if i close my game.

package Audio;

import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

import sun.audio.AudioPlayer;


public class MenuAudio {

public static boolean stopped = false;

public static void Start(){
System.out.println(stopped);
try{
File f = new File("C:\\Games\\Test\\Assets\\sound\\menu\\menuLoop.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(f);
clip.open(ais);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
AudioPlayer.player.stop(ais);
if(stopped == true) // work
{
    clip.stop(); //not work
    clip.close(); //not work
    stopped = false; //work
    System.out.println(stopped);//work
} 

}catch(Exception exception){System.out.println("WAV FILES NOT FOUND!! ");}

}

} 

boolean stopped return to true in another class whit this code

    int test = 0;
 //code useless
    ........
 //code useless
    MenuAudio.Start(); // play the sound
 //code useless
    ........
 //code useless

if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)){  //work
            test = 0;
        }

        if(test == 0 && Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){ //work
            MenuAudio.stopped = true;
            MenuAudio.Start();
            test = 1;
        }

this code is work the console return that : true false if i press the key Left after Right. I don't know why Clip.stop(); not work.

(Sorry for the bad english)

There are a number of confusing things in your code. For one, I think sun.audio.AudioPlayer is deprecated. I don't see it listed in the Java 7 API. I'm pretty sure it has no role to play in controlling a javax.audio.Clip in the code you have written.

There is more info on sun.audio.AudioPlayer at this past StackOverflow question link.

Another source of confusion is your naming convention. If the first letter is a capital letter, we expect this to be a Class, not a variable or method. Fixing the indentations would also make it a little easier to read.

I don't understand why you are setting a variable to false and then calling a routine to both load and start and stop the playback in quick succession.

Clip's were designed to be opened separately from when they are played. As written, your code has to load the entire clip into RAM before the start command will execute. If you load the Clip beforehand, perhaps in a constructor for MenuAudio, then you could consider a plan such as passing the clip variable to any method responsible for starting or stopping it.

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