2013-02-09 218 views
1

这里夹线就是我using.I已经使用裁剪类发挥clip.Program已经没有任何错误的编译和运行正常,但我不能听到声音的代码。的java:不工作

import java.io.File; 

import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.Clip; 
import javax.sound.sampled.DataLine; 
import javax.sound.sampled.LineEvent; 
import javax.sound.sampled.LineListener; 


public class ClipTest { 

public static void main(String[] args) throws Exception { 


File soundFile = new File("./1.wav"); 
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile); 


DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat()); 
Clip clip = (Clip) AudioSystem.getLine(info); 
clip.open(sound); 


clip.addLineListener(new LineListener() { 
    public void update(LineEvent event) { 
    if (event.getType() == LineEvent.Type.STOP) { 
     event.getLine().close(); 
     System.exit(0); 
    } 
    } 
}); 


clip.start(); 
} 
}  
+0

请有看看我的帖子。它在我的机器 – 2013-02-09 08:49:57

回答

0

我只是尝试你的代码。我觉得你的错误是,你的文件IST空或不正确加载

我刚刚更换

File soundFile = new File("./1.wav"); 
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile); 

InputStream inRequest = this.getClass().getResourceAsStream("1.wav"); 
AudioInputStream sound = AudioSystem.getAudioInputStream(inRequest); 

这是新阶级

public class ClipTest { 

    public void run() throws UnsupportedAudioFileException, IOException, LineUnavailableException { 
     InputStream inRequest = this.getClass().getResourceAsStream("batR.wav"); 
     AudioInputStream sound = AudioSystem.getAudioInputStream(inRequest); 

     DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat()); 
     Clip clip = (Clip) AudioSystem.getLine(info); 
     clip.open(sound); 

     clip.addLineListener(new LineListener() { 

      public void update(LineEvent event) { 
       if(event.getType() == LineEvent.Type.STOP) { 
        event.getLine().close(); 
        System.exit(0); 
       } 
      } 
     }); 

     clip.start(); 

    } 

    public static void main(String[] args) throws Exception { 
     ClipTest clipTest = new ClipTest(); 
     clipTest.run(); 

    } 
} 
+0

没有好友上工作,你的代码是不是也工作....但最后我想通了,在我的代码的问题...和你的代码有同样的问题....我张贴的解决版本。 ...通过您的帮助方式日Thnx – user1678213 2013-02-09 09:19:52

+0

的代码后我上面的电脑 – 2013-02-09 09:21:09

+0

上工作的100%,如果你的代码是工作的机器,那么你可能会使用JDK 1.4.2或更早的版本上。 – user1678213 2013-02-09 09:23:55