简体   繁体   English

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

[英]How to play .wav file from URL in web browser embed - Java

I want to play a .wav sound file in embed default media player in IE. 我想在IE中嵌入默认媒体播放器中播放.wav声音文件。 Sound file is on some HTTP location. 声音文件位于某个HTTP位置。 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. 这是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 如果我用FileOutputStream编写,则播放效果很好
  2. If I replace my code of getting file from URL to my local hard disk. 如果我替换了从URL获取文件到本地硬盘的代码。 then it also works fine. 那么它也可以正常工作。

I don't know why I am unable to play file from HTTP. 我不知道为什么我无法从HTTP播放文件。 And why it plays well from local hard disk. 以及为什么它可以从本地硬盘正常播放。

Please help. 请帮忙。

Make sure you set the correct response type. 确保设置正确的响应类型。 IE is very picky in that regard. IE在这方面非常挑剔。

[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. 您的代码存在的问题是:如果没有在对fileBytes is.read()的单次调用中获取数据,则不会将其附加到fileBytes ,而是会覆盖第一个字节。

Also, the output stream which you get from the response is already buffered. 同样,您从响应中获得的输出流已经被缓冲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM