2011-05-02 82 views
1

我目前卡在我的項目中,目前我可以播放音樂,但我希望能夠交換歌曲,這樣我就可以在主菜單主題和遊戲主題之間做出區別。停止播放中的剪輯

我已經編寫了解應用程序,有沒有辦法阻止剪輯播放(從我的軟件的另一部分),例如,通過將布爾值從true更改爲false。我嘗試了很多,比如使布爾變量變化,這樣它會在運行中發生變化,但沒有任何幫助。我需要找到一種方法來阻止運行中的剪輯,有人可以幫我解決這個問題嗎?

我寫了待辦事項代碼應該來停止剪輯,如果可能的話,你可以寫下代碼停止我的剪輯在運行內播放?

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 stop; 

    /** 
    * Private because of the singleton 
    */ 
    public Music() 
    { 
    } 

    /** 
    * Play a siren sound 
    */ 
    public void playSiren(String musicFileName) 
    { 
     Music p = getPlayer(); 
     p.playSirenFile(musicFileName); 
    } 

    /** 
    * Play the siren file 
    */ 
    private void playSirenFile(String musicFileName) 
    { 
     this.soundFile = new File("Music/"+musicFileName+".wav"); 
     stop = true; 
     thread = new Thread(this); 
     thread.setName("SoundPlayer"); 
     thread.start(); 

    } 

    public Clip getClip() 
    { 
     return this.clip; 
     } 
    /** 
    * Invoked when the thread kicks off 
    */ 
    @SuppressWarnings("deprecation") 
    public void run() 
    { 
     try 
     { 
      AudioInputStream stream = AudioSystem.getAudioInputStream(this.soundFile); 
      AudioFormat format = stream.getFormat(); 
      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(99999999); 
     } 

     catch (UnsupportedAudioFileException e) 
     { 
     e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
     e.printStackTrace(); 
     } 
     catch (LineUnavailableException e) 
     { 
     e.printStackTrace(); 
     } 

    } 

    private static Music getPlayer() 
    { 
     if (player == null) 
     { 
      player = new Music(); 
     } 
     return player; 
    } 

    public void update(LineEvent event) 
    { 
    } 

    public void stopClip() 
    { 
     // TODO NEED HELP HERE 
    } 

    public void closeClip() 
    { 
     clip.close(); 
    } 

    public void startClip() 
    { 
     clip.start(); 
    } 
    public void volume(float volume) 
    { 
      //TODO NEED HELP HERE 

     /* 
     FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); 
     gainControl.setValue(-50.0f); // Reduce volume IN DECIBELS 
     clip.start(); 
     */ 
    } 
    } 
+0

爲了更好地爲您提供幫助,請在[相關主題]上發佈[SSCCE](http://pscode.org/sscce.html)(http://stackoverflow.com/questions/5833553/how-到停止-A-音樂卡子的Java/5834103#5834103)。請注意觀察員:*相關*,而不是*重複*。 – 2011-05-02 09:43:12

回答

1

你確定你訪問該程序與單版本,導致單是私人在你的類,如果你不這樣做,你就不會停止剪輯,但只能使新的

「getPlayer()」方法與sigletons中的「getInstance()」具有相同的功能。那麼你確定你使用getplayer()調用訪問正在運行的obkect嗎?因爲這個方法在你的類中是私有的,所以如果你不使用這個方法,你不會停止這個剪輯,而只是創建新的。