2011-08-26 58 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