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?
/** @ 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.