简体   繁体   English

如何在Javasound中删除LOOP_CONTINUOUSLY? [内部代码]

[英]How to remove LOOP_CONTINUOUSLY in Javasound? [code inside]

I have saw this code somewhere in this area. 我在该区域的某处看到了此代码。 I want to play the .wav file once but not continuously. 我想一次播放.wav文件,但不能连续播放。 How can I do that? 我怎样才能做到这一点? I try removing the LOOP_CONTINUOUSLY line but it does not work. 我尝试删除LOOP_CONTINUOUSLY行,但是它不起作用。

URL url = new URL("http://pscode.org/media/leftright.wav");
                            Clip clip = AudioSystem.getClip();
                            // getAudioInputStream() also accepts a File or InputStream
                            AudioInputStream ais = AudioSystem.
                                getAudioInputStream( url );
                            clip.open(ais);
                            clip.loop(Clip.LOOP_CONTINUOUSLY);
                            SwingUtilities.invokeLater(new Runnable() {
                                public void run() {
                                    JOptionPane.showMessageDialog(null, "Close to exit!");
                                }
                            });

And if possible. 如果可能的话。 Can I play mp3 files as well. 我也可以播放mp3文件。 Because when I try to replace it as mp3, my code crashes. 因为当我尝试将其替换为mp3时,我的代码崩溃了。 Also, I want to get the file from my computer. 另外,我想从计算机上获取文件。 Not on the internet. 不在互联网上。 Can anyone help me? 谁能帮我?

  • Use clip.start() instead of clip.loop(Clip.LOOP_CONTINUOUSLY) 使用clip.start()代替clip.loop(Clip.LOOP_CONTINUOUSLY)
  • Use File or getResourceAsStream() instead of URL and feed it to getAudioInputStream the same way 使用FilegetResourceAsStream()代替URL ,然后以相同方式将其提供给getAudioInputStream
  • See this question for mp3 files 看到此问题的mp3文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM