2011-08-26 57 views
1

我是新來的java和嘗試添加聲音到我的應用程序。我遇到的問題是音樂開始播放,但當音樂方法第二次被調用時,音樂不會停止。我究竟做錯了什麼?學習java。音頻控制問題

import sun.audio.*; 
import java.io.*; 

public class sound { 
    private static boolean playing = false; 

    @SuppressWarnings("restriction") 
    public static void music() {   
     try { 
      // open the sound file as a Java input stream 
      String soundFile = "backgroundmusic.wav"; 
      InputStream in = new FileInputStream(soundFile); 

      // create an audiostream from the inputstream 
      AudioStream audioStream = new AudioStream(in); 

      // play the audio clip with the audioplayer class 
      if(playing == false){ 
       AudioPlayer.player.start(audioStream); 
       playing = true; 
      }else{ 
       AudioPlayer.player.stop(audioStream); 
       playing = false; 
      } 
     } catch(IOException error) {} 

     System.out.println(playing);     
    } 
} 

回答

3

每次調用music()時間,一個新的輸入流構造,所以你與方法調用之間完全不同的流實例的工作。

+0

現在啊,現在你說它很明顯。謝謝您的幫助。 – Kozmk12