|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
Hi, I´m a newbie here and I´ve been trying to send audio from a servlet. Unfortunately, I have not been successful (not even close I think). In simple terms, what I´ve tried is the following:
1. Set the content type to audio/x-wav 2. create a file object that opens my *.wav file 3. create an audioinputstream and use the wav file 4. create a bufferedoutputstream = response.getOutputStream() 5. read from the audioniputstream and write to the bufferedoutputstream do these steps sound reasonable? I´d appreciate any help! this is my code... (it´s a little messy... sorry) import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import javax.sound.sampled.*; public class ServeAudio extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse resp) throws ServletException, java.io.IOException { int framesReadTotal = 0; File waveFile = new File( "abc.wav" ); resp.setContentType("audio/x-wav"); resp.setHeader("Content-Disposition", "filename=abc.wav") ; BufferedOutputStream myOutStream = new BufferedOutputStream(resp.getOutputStream() ); try{ AudioInputStream audioStream = AudioSystem.getAudioInputStream(waveFile); int bytesPerFrame = audioStream.getFormat().getFrameSize(); int numBytes = 1024 * bytesPerFrame; byte[] audioBytes = new byte[numBytes]; int numBytesRead = 0; int numFramesRead = 0; while ((numBytesRead = audioInputStream.read(audioBytes)) != -1) { numFramesRead = numBytesRead / bytesPerFrame; framesReadTotal += numFramesRead; myOutStream.write(audioBytes); } mOutStream.flush(); } catch (Exception ex){ } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { processRequest(request, response); } } |
|
#2
|
|||
|
|||
|
I have not done what you are asking, but I believe you may be overthinking what you need to do. You are specifying that you are sending out a wav file, yet you are opening an Audiostream to get it. From reading breifly about the AudioStream, it seems to be for if you plan on using the data in an actual audio situation.
It seems more likely to me that you should supply the file as is to the browser, who wil lthen open it in it's own audiostream since you are telling it it is an audio file. Make sense? In other words, try mearly opening the file as a File and send it out in raw bytes and see what that does. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Creating and sending/streaming audio from a servlet |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|