无法播放Java中的声音

问题描述:

class One { 

    public static void main(String[] arg) { 
     new One().play(); 
    } 

    public void play() { 
     try { 
      AudioClip clip = Applet.newAudioClip(new URL(
        "file:\\C:\\Documents and Settings\\admin\\Desktop\\javabottle-open.wav")); 
      clip.play(); 
      URL ur = new URL("file:\\C:\\Documents and Settings\\admin\\Desktop\\flute+hrn+mrmba.aif"); 
      clip = Applet.newAudioClip(ur); 
      clip.play(); 
      Thread.sleep(10000); 
     } catch(Exception e) { 
      System.out.println(e.getMessage()); 
     } 
    } 
} 

我没有收到任何异常消息&音频也没有播放。无法播放Java中的声音

+0

可能重复http://*.com/questions/2416935/how-to-play-wav-files- with-java) – McDowell 2013-02-19 09:11:05

+1

如果你在路径名中使用/,你会让你的生活更轻松。 – Ingo 2013-02-19 09:12:17

+0

@Ingo OP,如果您全部使用[File.separator](http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File),您将使您的生活更加轻松。 html#separator)的路径名称。 – ppeterka 2013-02-19 09:16:17

这对我的作品

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(); 

    } 
} 
的【如何播放.wav用java文件(
+0

也为mi工作。上帝保佑。 – sunya 2013-02-19 09:25:49

+0

@sunya不客气。如果你喜欢我的回答,只需接受并投票。谢谢 – 2013-02-19 09:27:53