Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Not able to play wav file using audio or embed tag in chrome  RSS feed

 
ripu sudan
Greenhorn
Posts: 20
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I have a website where use can listen wav files on server.
I am using following approach.
1. There is a servlet that write wav file from server to response object.
2. Passing reference of this servlet in <audio> tag.



servlet wav.java
============================
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


ServletOutputStream stream = null;
BufferedInputStream buf = null;
try {
stream = response.getOutputStream();
File wav = new File("E:\\RBTS\\rbt.wav" );
//set response headers
response.setContentType("audio/x-wav");
response.setContentLength((int) wav.length());

FileInputStream input = new FileInputStream(wav);
buf = new BufferedInputStream(input);
int readBytes = 0;
//read from the file; write to the ServletOutputStream
while ((readBytes = buf.read()) != -1)
stream.write(readBytes);
System.out.println("readBytes "+readBytes);
} catch (Exception ioe) {
ioe.printStackTrace();
throw new ServletException(ioe.getMessage());
} finally {
if (stream != null)
stream.close();
if (buf != null)
buf.close();
}

}



index.jsp
=======================
<audio controls>
<source src="/crbt/servlet/wav" type="audio/mpeg">
Your browser does not support the audio element.
</audio>




Using this approach i am able to play file in IE and MOZILA but not in crome.

I don't know weather this question is valid here or not cos normal wav file can not be played in crome.

So if anyone have alternative then please help me.

As mp3 file can be played in <audio> tag inside crome i came up with following solution...in case of crome servlet return mp3 file instead of wav file....but could not find how on the fly convert file instead of creating converted file on hard disk.

and i also post for this as subject Easy way to convert wav file to mp3 file on the fly http://www.coderanch.com/t/623583/JSP/java/Easy-convert-wav-file-mp here, but response provided is confusing to me.


Please help me i am snatching my hairs from two days :banghead:









 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, I can play wav files in Chrome without issue.

A question: since you have the files on the server, why the servlet to stream them? Why not just make the wav files part of the web app and reference them directly (the same way that one would reference an image file)?

Also, if it turns out you need an mp3 format, I would not do any translation on-the-fly. I'd convert the file once, and then serve the appropriate file.
 
ripu sudan
Greenhorn
Posts: 20
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear Bibeault,

Firstly thanks for your reply :)
Reason of using servlet is :: As there are thousand of Wav files and those are not only part of my web application ,other application also use them like IVR,so i can't make them part of my web application.
And if i use path to actual wav file as



It will not work Reason you knows [ cos when it served to client file will not be at client side :P]



Below code works for ie,mozila only not in crome
==============================================
<embed src="/crbt/servlet/wav" width="100%" height="60" >
<noembed><img src="yourimage.gif" ></noembed>
</embed>
==============================================
However if i put that wav file in my webapps directory(that is not required) it works.
as

<embed src="audio_dir/filetoplay.wav" width="100%" height="60" >
<noembed><img src="yourimage.gif" ></noembed>
</embed> //works for crome


so i am searching for alternative so that i can use that approach in case of crome only.

Waiting for your reply. :D

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ripu sudan wrote:so i can't make them part of my web application.

Why not? Simply copy the files during the build process.

With regards to Chrome (not "crome"), why aren't you using the <audio> tag for all moderne browsers?
 
ripu sudan
Greenhorn
Posts: 20
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear Bibeault,

We do not have to copy our files to our webapps directory.
I am not using <audio> tag because it is not working on even ie and mozila.

I am thinking that is there any third party audio control so that it can work on each browser.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The audio tag works fine in modern versions of all desktop and most mobile browsers. You can generally check for support of various features here.

If the audio tag isn't working for you, then you are using it incorrectly.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!