-1

I have created a program where I can play a song, However I cannot get it to pause/stop and when I click the play button again the song loops over and over again. How can i get the song to pause or stop?

Here's my source code below to play a song:

public void sound(String nm) {   
    AudioStream BGM; //Backround Music

    try {
        InputStream test = new FileInputStream("./"+nm+".wav");
        BGM = new AudioStream(test);

        AudioPlayer.player.start(BGM);

    }
    catch (IOException error) {
            JOptionPane.showMessageDialog(null, error.getMessage());
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • What play button? There are no UI elements in your code. – chtenb Jan 12 '14 at 16:19
  • I would read the documentation of the class of `AudioPlayer.player`. We don't know what class it is, so we can't help. – JB Nizet Jan 12 '14 at 16:25
  • 1
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). From past experience, I guess `AudioPlayer` is in the `sun.com` packages? They are undocumented, cannot be relied upon to be in a non-Oracle JRE, or even the next version of the Oracle JRE. **Don't use them.** Instead use the Java Sound based [`Clip`](http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/Clip.html) to play short sounds. See the [info. page](http://stackoverflow.com/tags/javasound/info) for a short example. Check the Java Docs for `Clip` (linked earlier in comment) for how to stop one. – Andrew Thompson Jan 12 '14 at 16:47

2 Answers2

0

I guess you're using this example, which is using sun.audio.* namespace.

As you can see from the source code, if you want to stop, you can use AudioPlayer.player.stop(BGM); with the same reference as a parameter that you used for play.

eis
  • 51,991
  • 13
  • 150
  • 199
0

Your answere is maybe in the AudioPlayer class. take a look in it- perhaps there is also a pause() and stop () function.

LPH
  • 1,275
  • 9
  • 16