BitRateControl

How can I use javax.media.BitRateControl?I would like to connect it to a DataSource and find the bitrate..
[127 byte] By [turingcomplete] at [2007-9-26 3:43:51]
# 1

I tried the following, but I'm getting a bitrate of 0.

package net.turingcomplete.phosphor.shared;

import javax.media.*;

import javax.media.control.*;

import java.net.*;

public class MediaHelpers {

public static void main(String[] args) {

try {

URL url = new URL("file:///C:\\My Documents\\Sound\\mp3\\downloaded\\Allen Ginsberg - Howl.mp3");

Player player = Manager.createPlayer(url);

BitRateControl control = (BitRateControl) player.getControl("javax.media.control.BitRateControl");

Trace.display("rate: "+control.getBitRate());

} catch (Exception e) {

Trace.display(e, "Cannot find bitrate of file");

}

}

}///:~

Is bitRate for mp3's not supported or am I missing a step?

turingcomplete at 2007-6-29 12:23:00 > top of Java-index,Security,Cryptography...
# 2
I have a feeling that the bitrate can only be gotten when the media is playing. This is too bad. Is there some way of finding the length of time of the media, and then doing a simple divide of the size of the file?
turingcomplete at 2007-6-29 12:23:00 > top of Java-index,Security,Cryptography...
# 3

/** @ return the approx. number of bits per second or -1 if cannot find the bitRate

*/

public static int getBitRate(File file) {

try {

Player player = Manager.createRealizedPlayer(new URL("file:///"+file.getAbsolutePath()));

double length = player.getDuration().getSeconds();

if (length > 0)

return (int) ((file.length() * 8) / length);

else

return 0;

} catch (Exception e) {

return -1;

}

}

The problem is that you have to create a realized player.. not very good for doing big batch of files.

turingcomplete at 2007-6-29 12:23:00 > top of Java-index,Security,Cryptography...
# 4
Also, remember that bit rate is not constant in many MP3 files. I don't know whether or not the JMF supports reading Xing headers, but it shouldn't be too hard for local files given the documentation at http://www.xingtech.com/developer/mp3/...ken
kbad at 2007-6-29 12:23:00 > top of Java-index,Security,Cryptography...