views:

19

answers:

1

Hi;

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.

A: 

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.

Aaron Digulla
I also tried it. But no luck.
Tahir Akram
Check my edits. To be sure, save the data also to a file (not only to the response) and try to play that file (to test that it was not corrupted).
Aaron Digulla