【问题标题】:How to play .wav file from URL in web browser embed - Java如何在 Web 浏览器嵌入中从 URL 播放 .wav 文件 - Java
【发布时间】:2011-04-02 12:33:30
【问题描述】:

我想在 IE 中嵌入默认媒体播放器中播放 .wav 声音文件。声音文件位于某个 HTTP 位置。我无法在那个播放器中发出声音。

以下是代码。

    URL url = new URL("http://www.concidel.com/upload/myfile.wav");
        URLConnection urlc = url.openConnection();
        InputStream is = (InputStream)urlc.getInputStream();
         fileBytes = new byte[is.available()];
         while (is.read(fileBytes,0,fileBytes.length)!=-1){}
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
         out.write(fileBytes);

这里是 HTML 的嵌入代码。

<embed src="CallStatesTreeAction.do?ivrCallId=${requestScope.vo.callId}&agentId=${requestScope.vo.agentId}" type="application/x-mplayer2" autostart="0" playcount="1" style="width: 40%; height: 45" />
  1. 如果我在 FileOutputStream 中写入,那么它会播放得很好
  2. 如果我替换从 URL 获取文件到本地硬盘的代码。那么它也可以正常工作。

我不知道为什么我无法从 HTTP 播放文件。以及为什么它在本地硬盘上播放效果很好。

请帮忙。

【问题讨论】:

    标签: java audio embed wav


    【解决方案1】:

    确保您设置了正确的响应类型。 IE 在这方面非常挑剔。

    [编辑] 你的复制循环坏了。试试这个代码:

    URL url = new URL("http://www.concidel.com/upload/myfile.wav");
    URLConnection urlc = url.openConnection();
    InputStream is = (InputStream)urlc.getInputStream();
    fileBytes = new byte[is.available()];
    int len;
    while ( (len = is.read(fileBytes,0,fileBytes.length)) !=-1){
        response.getOutputStream.write(fileBytes, 0, len);
    }
    

    您的代码的问题是:如果在对is.read() 的一次调用中未获取数据,则它不会附加到fileBytes,而是会覆盖第一个字节。

    此外,您从响应中获得的输出流已经被缓冲了。

    【讨论】:

    • 检查我的编辑。可以肯定的是,将数据也保存到一个文件中(不仅是响应)并尝试播放该文件(以测试它没有损坏)。
    猜你喜欢
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    相关资源
    最近更新 更多