I can get a sound to play but not stop

Ive been playing around with a sequencer and i can get my sounds to start but when i try to stop them it does nothing, here is my coding attempt on it, ive made sure to test and see if the int is being called correctly and it seems to be.

String music[] ={"jingle1","jingle2"};

Sequencer sequencer =null;

Sequence song =null;

try{

sequencer = MidiSystem.getSequencer();

sequencer.open();

if (i2 == 99){

sequencer.stop();

}else{

URL newsUrl =new URL("http://dnd.no-ip.org/music/"+music[i2]+".mid");

song = MidiSystem.getSequence(

newsUrl.openStream());

sequencer.setSequence(song);

sequencer.start();

}

}

catch (MidiUnavailableException e){

}

catch (InvalidMidiDataException e){

}

catch (IOException e){

}

[1620 byte] By [silabsofta] at [2007-10-3 4:27:54]
# 1
How are you testing playing and stopping? What's the process?
_maxmkleea at 2007-7-14 22:30:48 > top of Java-index,Security,Cryptography...
# 2
I don't have any experience here, but maybe this will help: http://java.sys-con.com/read/37803.htm http://java.sun.com/docs/books/tutorial/sound/playing.html http://www.onjava.com/pub/a/onjava/excerpt/jenut3_ch17/index.html
ajmasxa at 2007-7-14 22:30:48 > top of Java-index,Security,Cryptography...
# 3

Hello,

"MidiSystem.getSequencer()" always return a new intance of a Sequencer.

you must call sequencer.stop() in the same instance of sequencer who is playing.

but in your code you are making a new instance before call stop() method. and for this new instance there is not sound running.

so you need have something like this in your class:

private Sequencer sequencer;

private Sequencer getSequencer() throws MidiUnavailableException {

if (this.sequencer == null) {

this.sequencer = MidiSystem.getSequencer();

}

if (!this.sequencer.isOpen()) {

this.sequencer.open();

}

return this.sequencer;

}

and then in your code replace "MidiSystem.getSequencer()" to "getSequencer()"

akdmiaa at 2007-7-14 22:30:48 > top of Java-index,Security,Cryptography...