如何在Javasound中删除LOOP_CONTINUOUSLY? [内部代码] [英] How to remove LOOP_CONTINUOUSLY in Javasound? [code inside]

查看:103
本文介绍了如何在Javasound中删除LOOP_CONTINUOUSLY? [内部代码]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在该区域的某处看到了此代码.我想一次播放.wav文件,但不能连续播放.我怎样才能做到这一点?我尝试删除LOOP_CONTINUOUSLY行,但是它不起作用.

I have saw this code somewhere in this area. I want to play the .wav file once but not continuously. How can I do that? I try removing the LOOP_CONTINUOUSLY line but it does not work.

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!");
                                }
                            });

如果可能的话.我也可以播放mp3文件.因为当我尝试将其替换为mp3时,我的代码崩溃了.另外,我想从我的计算机上获取文件.不在互联网上.谁能帮我吗?

And if possible. Can I play mp3 files as well. Because when I try to replace it as mp3, my code crashes. Also, I want to get the file from my computer. Not on the internet. Can anyone help me?

推荐答案

  • 使用clip.start()代替clip.loop(Clip.LOOP_CONTINUOUSLY)
  • 使用FilegetResourceAsStream()代替URL,并以相同的方式将其喂入getAudioInputStream
  • 有关mp3文件,请参见此问题
    • Use clip.start() instead of clip.loop(Clip.LOOP_CONTINUOUSLY)
    • Use File or getResourceAsStream() instead of URL and feed it to getAudioInputStream the same way
    • See this question for mp3 files
    • 这篇关于如何在Javasound中删除LOOP_CONTINUOUSLY? [内部代码]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆