简体   繁体   中英

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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