如何在 Web 浏览器嵌入中从 URL 播放 .wav 文件 - Java
我想在 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" />
- 如果我在 FileOutputStream 中写入,那么
- 如果我替换从 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" />
- If I write in FileOutputStream then it plays well
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您设置了正确的响应类型。 IE 在这方面非常挑剔。
[编辑] 您的复制循环已损坏。试试这个代码:
您的代码的问题是:如果在一次调用
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:
The problem with your code is: If the data isn't fetched in a single call to
is.read()
, it's not appended tofileBytes
but instead the first bytes are overwritten.Also, the output stream which you get from the response is already buffered.