File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Streaming Audo File, need to Stream only 30 seconds Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Streaming Audo File, need to Stream only 30 seconds" Watch "Streaming Audo File, need to Stream only 30 seconds" New topic
Author

Streaming Audo File, need to Stream only 30 seconds

s mahen perera
Ranch Hand

Joined: Mar 08, 2005
Posts: 101
Hi all,

Not sure whether this is the correct Forum to post this Question. Anyway, this is my problem.

I am streaming a Audio File from a URL and setting the HttpServletResponce with the streamed data as shown in the code below. My problem is, now I have to stream only the first 30 seconds of the audio file, and not the entire one..



How can i ammend the code to stream only the first 30 seconds of the audio file..

Thanks for any insights,,
Kenny Johnson
Ranch Hand

Joined: Jan 01, 2007
Posts: 37
You would need to know how many bytes = 1 second of playtime for the audio file. Im not really sure how you would find this out. You would have to read about the format which the audio file is stored as. I know when I play mp3s some play at 120K a second, and others play at 180K a second. In my case with the mp3s, I would guess that mp3s have some kind of header info that tells the player how many Kb to read per second. If you are streaming an mp3, you would need to read up on how a mp3 stores that info in the header and then extract it.
Then if you were able to find this information out, just keep a count of the number of bytes read so far, and stream in the ( num bytes in a second ) * 30.

By the way, this is just all speculation and I could be dead wrong.

Although I am curious as to how this would be done now that I think about it more... Please post up how you solve this problem if/when you do.
[ January 12, 2007: Message edited by: Kenny Johnson ]
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2510
    
  10

s mahen,

You can use the Java Media Framework API (JMF) to cut the first 30 seconds out of your media file, and stream those.

This API has methods to retrieve parts of media files (includes mp3), and to write media data to a stream.

This example comes very close to what you want to do:

Given an input media file, the object is to cut pieces from the file and generate an output file from that.


More info on Sun's website:
http://java.sun.com/products/java-media/jmf/

Regards, Jan
[ January 13, 2007: Message edited by: Jan Cumps ]

OCUP UML fundamental and ITIL foundation
youtube channel
s mahen perera
Ranch Hand

Joined: Mar 08, 2005
Posts: 101
thanks a lot for both comments... will try these out and let you guys know..
Jeevan Philip
Ranch Hand

Joined: Nov 17, 2006
Posts: 41
Just setup a timer in your streaming loop for 30 seconds and close the stream at the end. The technique of finding bytes used for 1 sec will not work coz it varies with every file/encoding. And I am not sure if you really need to manipulate the file using JMF. In that case its better to have all files as 30 sec bits instead which may not be viable.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2510
    
  10

Jeevan,
I have some remarks on your suggestion:

Just setup a timer in your streaming loop for 30 seconds and close the stream at the end.


Cutting a stream will not be liked by the browser plug-in playing the tune. It will most likely show an 'error while streaming' message, because there is no valid end to the stream.

In that case its better to have all files as 30 sec bits


This would require you to cut 30 seconds out of every audio file you host. The JMF solution works on the full audio file. And it gives a valid break after the 30 seconds.

Regards, Jan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Streaming Audo File, need to Stream only 30 seconds