如何下载和播放在线剪辑?

时间:2019-07-23 18:27:34

标签: java file url audio

我想在我的保管箱中播放wav声音。使用URL加载时,声音播放〜0.1秒并停止播放。没有错误显示。

我直接尝试了一个文件,它起作用了。我切换到URL,然后失败。我认为在剪辑开始播放之前,文件未完全/足够地加载。

这是我的最小代码:

URL u = new URL("https://www.dropbox.com/s/uqm81qa10825da8?dl=1");
        Clip clip = loadSound(u);

        clip.start();

loadSound函数为:

        try{
            BufferedInputStream in = null;


            if(URLManagerUtils.isLocalFile(soundFile))
                in = new BufferedInputStream(new FileInputStream(soundFile.getFile()));
            else 
                in = new BufferedInputStream(URLManagerUtils.loadFromFile(soundFile));

            AudioInputStream audioStream = AudioSystem.getAudioInputStream(in);
            DataLine.Info info = new DataLine.Info(Clip.class, audioStream.getFormat());

            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.addLineListener(new LineListener() {

                @Override
                public void update(LineEvent event) {
                    if(event.getType().equals(LineEvent.Type.STOP))
                    {
                    }
                }
            });
            clip.open(audioStream);
            return clip;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            throw new Error("Error trying to read sound file:"+soundFile+"\n"+sw.toString());
        }
    }

我希望声音能够完全播放。现在只播放约0.1秒,然后停止。 我想要并且没有错误消息。

您能帮我吗?

0 个答案:

没有答案