eclipse - How to stop a music Clip in Java? -


i working classmates on game, have programmed quite music until now. music.java class posted below.

we have problem stopping our sound, need way stop clip playing can start clip, way can shift through music during our game (like when start game, song should different main menu). can me problem?

even if can destroy object make new one, if that's possibility, willing that, not have clue how this. rather have possibility stop current clip file, , replace new one.

package sound;  import java.io.file; import java.io.ioexception;  import javax.sound.sampled.audioformat; import javax.sound.sampled.audioinputstream; import javax.sound.sampled.audiosystem; import javax.sound.sampled.clip; import javax.sound.sampled.dataline; import javax.sound.sampled.floatcontrol; import javax.sound.sampled.lineevent; import javax.sound.sampled.linelistener; import javax.sound.sampled.lineunavailableexception; import javax.sound.sampled.unsupportedaudiofileexception;   public class music implements linelistener, runnable {  private file soundfile; private thread thread; private static music player; private music audio; private clip clip; private boolean stoppen = false;   /** * private because of singleton */ public music() { }  /** * play siren sound */ public void playsiren(string musicfilename) {     audio = getplayer();      audio.playsirenfile(musicfilename); }  /** * play siren file */ private void playsirenfile(string musicfilename) {     this.soundfile = new file("music/"+musicfilename+".wav");     thread = new thread(this);     thread.setname("soundplayer");     thread.start(); }  /** * invoked when thread kicks off */ public void run() {     try     {         audioinputstream stream =     audiosystem.getaudioinputstream(this.soundfile);         audioformat format = stream.getformat();  /** * can't yet open device alaw/ulaw playback, convert * alaw/ulaw pcm */             if ((format.getencoding() == audioformat.encoding.ulaw) ||    (format.getencoding() == audioformat.encoding.alaw))             {             audioformat tmp = new audioformat(                 audioformat.encoding.pcm_signed,             format.getsamplerate(),             format.getsamplesizeinbits() * 2, format.getchannels(),             format.getframesize() * 2, format.getframerate(), true);             stream = audiosystem.getaudioinputstream(tmp, stream);             format = tmp;         }         dataline.info info = new dataline.info(clip.class, stream         .getformat(), ((int) stream.getframelength() * format         .getframesize()));          this.clip = (clip) audiosystem.getline(info);         this.clip.addlinelistener(this);         this.clip.open(stream);         this.clip.start();         try         {             thread.sleep(99);         }         catch (exception e)         {         }         while (clip.isactive() && thread != null)         {             try             {                 thread.sleep(99);             }             catch (exception e)             {                 break;             }         }         clip.loop(999999999);         clip.drain();       }     catch (unsupportedaudiofileexception e)     {     // todo auto-generated catch block     e.printstacktrace();     }     catch (ioexception e)     {     // todo auto-generated catch block     e.printstacktrace();     }     catch (lineunavailableexception e)     {     // todo auto-generated catch block     e.printstacktrace();     } }  private static music getplayer() {     if (player == null)     {         player = new music();     }     return player; }  public void update(lineevent event) { }  public void stopclip() {     //todo need here }  public void startclip() {     //todo need here } public void volume(float volume) {      //todo need here     /*     floatcontrol gaincontrol = (floatcontrol) clip.getcontrol(floatcontrol.type.master_gain);     gaincontrol.setvalue(-50.0f); // reduce volume in decibels     clip.start();         */     } } 

stuff might need know:

in guicontroller, make new music object, through method playsiren give songname, , automatically runs.

look dataline.stop() method. clip implements dataline.


e.g. clipcontrol.java

import java.awt.event.*; import javax.swing.*; import javax.sound.sampled.*; import java.net.url;  class clipcontrol {      public static void main(string[] args) throws exception {         url url = new url("http://pscode.org/media/leftright.wav");         audioinputstream ais = audiosystem.getaudioinputstream(url);         final clip clip = audiosystem.getclip();         clip.open( ais );         runnable r = new runnable() {             public void run() {                 final jtogglebutton startstop = new jtogglebutton("stop");                 startstop.addactionlistener( new actionlistener() {                     public void actionperformed(actionevent ae) {                         if (startstop.isselected()) {                             clip.stop();                             startstop.settext("start");                         } else {                             clip.loop(-1);                             clip.start();                             startstop.settext("stop");                         }                     }                 } );                 clip.loop(-1);                 joptionpane.showmessagedialog(null, startstop);             }         };         swingutilities.invokelater(r);     } } 

Comments