如何在 Web 浏览器嵌入中从 URL 播放 .wav 文件 - Java

发布于 2024-09-16 05:35:13 字数 936 浏览 4 评论 0原文

我想在 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 的文件。以及为什么它在本地硬盘上播放得很好。

请帮忙。

I want to play a .wav sound file in embed default media player in IE. Sound file is on some HTTP location. I am unable to sound it in that player.

Following is the code.

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

Here is embed code of 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. If I write in FileOutputStream then it plays well
  2. If I replace my code of getting file from URL to my local hard disk. then it also works fine.

I don't know why I am unable to play file from HTTP. And why it plays well from local hard disk.

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

子栖 2024-09-23 05:35:13

确保您设置了正确的响应类型。 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 而是附加到 fileBytes 中第一个字节被覆盖。

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

Make sure you set the correct response type. IE is very picky in that regard.

[EDIT] Your copy loop is broken. Try this code:

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

The problem with your code is: If the data isn't fetched in a single call to is.read(), it's not appended to fileBytes but instead the first bytes are overwritten.

Also, the output stream which you get from the response is already buffered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文