Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old July 5th, 2002, 04:30 PM
El_Gusano El_Gusano is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 0 El_Gusano User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation Creating and sending/streaming audio from a servlet

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);
}

}

Reply With Quote
  #2  
Old July 11th, 2002, 02:44 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Creating and sending/streaming audio from a servlet


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway