Audio Streaming from disk in java Servlets

To stream audio file I have implemented following code. But i am getting Exception:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file at javax.sound.sampled.AudioSystem.getAudioInputStream(

Can Any one help me please......

try { // From file System.out.println("hhhhhhhhhhhhhhhh"); AudioInputStream stream = AudioSystem.getAudioInputStream(new File("C:\\track1.mp3")); System.out.println("stream created"); AudioFormat format = stream.getFormat(); if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) { System.out.println("in if"); format = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), format.getSampleSizeInBits()*2, format.getChannels(), format.getFrameSize()*2, format.getFrameRate(), true); // big endian stream = AudioSystem.getAudioInputStream(format, stream); } // Create line SourceDataLine.Info info = new DataLine.Info( SourceDataLine.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize())); SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);; line.start(); // Continuously read and play chunks of audio int numRead = 0; byte[] buf = new byte[line.getBufferSize()]; while ((numRead =, 0, buf.length)) >= 0) { int offset = 0; while (offset < numRead) { offset += line.write(buf, offset, numRead-offset); } } line.drain(); line.stop(); }


That you're doing this job in a servlet class gives me the impression that your intent is to play the mp3 file whenever someone visits your website and that the visitor should hear this mp3 file.

If true, I'm sorry to say, but you're approaching this entirely wrong. Java servlet code runs in webserver machine and not in webbrowser machine. Whenever someone visits your website, this way the mp3 file would only be played at the webserver machine. This is usually a physically completely different machine which runs at the other side of the network connection and the visitor ain't ever going to hear the music.

You want to send the mp3 file raw (unmodified byte by byte) from webserver to the webbrowser without massaging it by some Java Audio API and instruct the webbrowser to play this file. The easist way is to just drop the mp3 file in public webcontent (there where your HTML/JSP files also are) and use HTML <embed> tag to embed it in your HTML/JSP file. The below example assumes the MP3 file to be in the same folder as the HTML/JSP file:

<embed src="file.mp3" autostart="true"></embed>

That's all and this is supported in practically every browser and it will show a player as well.

If the MP3 file is by business requirement stored outside public webcontent, then you may indeed need a servlet for this, but the servlet should do absolutely nothing more than getting an InputStream of it in some way and write it unmodified to the OutputStream of the HttpServletResponse the usual Java IO way. You only need to set the HTTP Content-Type header to audio/mpeg beforehand and if possible also the HTTP Content-Length header. Then point the src to the servlet's URL instead.

<embed src="mp3servlet" autostart="true"></embed>

Default java AudioInputStream does not support mp3 files. You have to plug in MP3SPI to let it decode mp3.

ALso, what do you mean by streaming? This code will play the audio file, not stream it as in internet radio streaming.

Category:java Time:2011-08-05 Views:3

Related post

  • Google Docs PDF Streaming is faster than java servlet Stream? 2009-08-13

    I am trying to serve PDF (useing tomcat server + Servlet normal streaming). The steaming is stable there is no bug and it handling parallel serving. But not as fast as Google DOC PDF Streaming. My Question is how did they do that fast ? What server t

  • How to send audio stream via UDP in java? 2010-01-18

    I have a problem, i have set MediaLocator to microphone input, and then created Player. I need to grab that sound from the microphone, encode it to some lower quality stream, and send it as a datagram packet via UDP. Here's the code, i found most of

  • Java: how to intercept video and audio streams? 2011-09-16

    is there any Java library out there that can help intercept video and audio streams being sent through the browser? So for example, a video starts loading through a flash player using some Flash server, the java application would listen for video/aud

  • Streaming large files in a java servlet 2008-09-11

    I am building a java server that needs to scale. One of the servlets will be serving images stored in Amazon S3. Recently under load, I ran out of memory in my VM and it was after I added the code to serve the images so I'm pretty sure that streaming

  • How to get video and audio streams from web cameras with Java? 2010-03-26

    How can I get video and audio streams from web cameras with Java (in a cross-platform way)? For example, we have a computer with 3-4 USB web cameras; we want to get their streams and make them visible in the user interface. How can we perform such a

  • Streaming multiple FLV files over a Java servlet as a single file 2011-04-07

    I am trying to implement a Java servlet that runs on Tomcat, capable of streaming multiple FLV files to client browsers having JWPlayer. The catch is I have to stream multiple files one at a time and sometimes start streaming from the middle of the f

  • intercept the audio stream that is playing on a pc Java 2011-04-30

    Possible Duplicate: internal audio recording program Here is the scenario: You are listening a song or an audio file with windows media player or vlc or from youtube whatever... i want that my program intercept the audio stream for analyze it. I trie

  • Creating an audio streamer in Java - where to start? 2011-05-12

    What I am looking to do is create a simple Java audio streaming server. To keep things simple it will just be static MP3 files but I'm sure the theory transfers. Clients can be local or over Internet and can be any device or computer that can be prog

  • Java-Servlet: Streaming of a Quicktime-video causes a ClientAbortException 2011-08-26

    I am trying to program a small webproxy based on a java-servlet. This proxy has only one task: HTML shall be passed to the client which sent the request (GET requests with destination port 80 are rerouted to the proxy by firewall rules). An embedded

  • Problem reading Java Servlet input stream in Google App Engine 2011-09-13

    I'm building something using Google App Engine and I'm trying to create a java Servlet that reads the raw POST data and does something with it. According to the servlet documentation, this should be possible using either the getReader() method or the

  • How do I mix multiple audio streams into a single recorded stream with Java? 2012-01-13

    Using Java Sound, Tritonus and JavaFX, how can I intercept data sent to the speakers and record it as a file? For example, I want to mix JavaFX media player audio streams (and/or Java Sound audio streams) into a single "wav" file in the same way a re

  • Java: How to implement listener to provide realtime audio stream position? 2012-04-22

    I'm working on an application to synchronize with realtime audio playback. I would like to define a listener with a specific timestep, and use the listener to get updates every time the audio being played has advanced by a timestep. Implementing this

  • tutorial on using flash or java servlet to upload microphone data from browser to server? 2009-05-07

    There was a question on how to get data from a microphone on a client to a server via an HTTP browser, and the answer was "use flash or maybe a java servlet applet". Fine, but how? Is there any sample code or tutorial out there? Can it be done with a

  • upload file with java servlet to app engine 2009-11-25

    I would like to be able to have the user upload short audio samples to my app-engine application and store them in the provided datastore. I'm using the Java servlet version. My problem is using a multi-part form to upload a file. Using the normal re

  • Take screenshot of audio stream 2011-01-23

    Alright, what I need is a command-line application that allows you to take a screenshot of a file's audio stream. For example it should be run like this: app.exe "C:/artist-title.mp3" "C:/mp3Stream.jpg" app.exe "C:/artist-title.wav" "C:/wavStream.jpg

  • Is it possible to downsample an audio stream at runtime with Flash or FMS? 2009-02-10

    I'm no expert in audio, so if any of you folks are, I'd appreciate your insights on this. My client has a handful of MP3 podcasts stored at a relatively high bit rate, and I'd like to be able to serve those files to her users at "different" bit rates

  • How to stream mp3 using pure Java 2009-03-18

    Is it possible to stream mp3s using pure Java? If so, what are the best resources to implement this. If not, are any other music formats streamable using Java only? --------------Solutions------------- As Mario says, JMF - Java Media Framework is a g

  • Passing Information from Java (Servlet) to CGI method? 2009-05-28

    I have this java servlet that grabs information from a form, I need to then take that information and pass it to a specific method in an .exe program and get the value that method returns. Is there a good way to go about doing this? --------------Sol

  • What is a lightweight, fast, java servlet container? 2009-10-04

    I have been writing a pure java web server specifically customized for a website I'm making. I've grown tired of reinventing the wheel though, and am now investigating moving over to java servlets. Basically I just want to run server side java code w

Copyright (C), All Rights Reserved.

processed in 2.017 (s). 13 q(s)