Finding Duration of an MP3 file in Java - java

OK so I have tried using ID3 tags to get the duration and I also tried using JMF media player.getDuration().
player.getDuration().getSeconds()
The file is VBR. Are there any light weight libraries or something inside JMF that could be used to solve this problem.
Thanks.

I use JAudioTagger to achieve this. The below code will get you the duration of an MP3 track.
int duration = 0;
try {
AudioFile audioFile = AudioFileIO.read(new File("file.mp3"));
duration = audioFile.getAudioHeader().getTrackLength();
} catch (Exception e) {
e.printStackTrace();
}
You can alternatively cast audioFile.getAudioHeader() to MP3AudioHeader and use the method getPreciseTrackLength() to get a more precise duration. However, this (i believe) only applies to MP3 files and no other formats (such as WAV files).

I using this lib and this code :
File f = new File("path/mp3");
MediaLocator ml = null;
Player p = null;
try {
ml = new MediaLocator(f.toURL());
p = Manager.createPlayer(ml);
p.start();
while (true) {
Thread.sleep(1000);
System.out.println("Media Time :: "+p.getMediaTime().getSeconds());
System.out.println("Duration :: "+p.getDuration().getSeconds());
if(p.getMediaTime().getSeconds() == p.getDuration().getSeconds())
break;
}
p.stop();
p.deallocate();
p.close();
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Good luck.

Related

JFugue converts midi file incorrectly

(using jfugue 5.0.9) I wanted to convert .mid to .txt (staccato), and later to .mid again, to confirm conversions worked. Both .mid (original and converted) should be equal ideally, but the converted (midi -> staccato -> midi) file has weird delayed notes, and one enlargened note duration. JFugue probably struggles because the midi is a human, hyper-sensible recording. Is there any way to fix this?
Heres the 3 files https://drive.google.com/drive/folders/1DepX0lCqNaIRCoHRfGwBRsO1xRFCbCpl?usp=sharing
And here are the 2 methods used:
public static Pattern convMidToStac(String fileName, boolean makeAFile) {
Pattern p = new Pattern();
// Convert midi file to a JFugue Staccato pattern.
try {
p = MidiFileManager.loadPatternFromMidi(new File("D:/eclipse-workspace/MidiReader/" + fileName + ".mid"));
if (makeAFile) {
makeFile(fileName, p.toString());
}
return p;
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
return null;
}
}
public static void convStacToMid(String fileName) {
Pattern p = new Pattern();
try {
p = MidiFileManager.loadPatternFromMidi(new File("D:/eclipse-workspace/MidiReader/" + fileName + ".mid"));
File filePath = new File("D:/eclipse-workspace/MidiReader/" + fileName + "MIDI.mid");
MidiFileManager.savePatternToMidi(p, filePath);
} catch (Exception e) {
e.printStackTrace();
}
}

Zxing - UPC +5 Supplement Barcode Detection

I have been attempting to use Zxing 2.3.0 to read images of UPC barcodes with a +5 supplement in java however i cannot read the supplement portion of the barcode. The code successfully reads the first portion only. After searching multiple websites i cannot find any further indications of how to read the supplement other than my current method. Any help would greatly be appreciated.
public static void main(String[] args) {
decodeUPC5();
}
public static void decodeUPC5(){
InputStream barCodeInputStream = null;
try {
barCodeInputStream = new FileInputStream("C:/Users/apoclyps/git/zxing-barcoder/Zxing-Test/img/upc5.png");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedImage barCodeBufferedImage = null;
try {
barCodeBufferedImage = ImageIO.read(barCodeInputStream);
} catch (IOException e) {
e.printStackTrace();
}
LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
// Attempting to read UPC + 5 Supplement
GenericMultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(new MultiFormatReader());
try {
multiReader.decodeMultiple(bitmap);
} catch (NotFoundException e1) {
e1.printStackTrace();
}
Result[] result = null;
try {
result = multiReader.decodeMultiple(bitmap);
} catch (NotFoundException e) {
e.printStackTrace();
}
System.out.println("Results length "+result.length);
for(Result r : result ){
System.out.println("Barcode text is " + r.toString());
}
}
Barcode image!
Output
Results length 1
Barcode text is 9780735200449
Keep in mind that the content of the barcode is 9780735200449 and not 9780735200449 51299. It will always (correctly) return the 9780735200449 as the contents of the barcode.
The +5 extension is returned as ResultMetadata, under key ResultMetadatatype.UPC_EAN_EXTENSION.
Note that it will still return the UPC barcode even if it doesn't see a +5 extension, obviously. So it's possible you would see it return without a +5 extension on this image. However it works for me with the app and so would imagine it easily detects the +5. (If you scan with the app, look at the left for "Metadata $12.99")

Lower cased properties in Java

Do anyone know if it is possible to have lower case letters in a property file in Java?
Currently when I have Example:
My_Conf=1234567
I get, when checking the property
My_Conf=null
in the code.
The code is suppose to be ran in Jboss servlet engine.
Best regards
Yes, it is definitely possible to have lower case characters in a .properties file. The problem might be that your code doesn't load the file.
For more information, have a look here: http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load%28java.io.Reader%29
Try this for debugging purpose:
String filename = "example.properties";
Properties properties = new Properties();
BufferedInputStream stream = null;
try {
stream = new BufferedInputStream(new FileInputStream(filename));
try {
properties.load(stream);
String prop = properties.getProperty("My_Conf");
System.out.println(prop);
} catch (IOException e) {
System.out.println("IOException");
}finally{
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found");
}

Taking Snapshots from a Webcam using JMF

I want to take a snapshot from a webcam through java. I followed this question and arrived at the this example. But there is a null pointer exception coming from the below line -
Buffer buf = frameGrabber.grabFrame();
Image img = (new BufferToImage((VideoFormat) buf.getFormat())
.createImage(buf));
buffImg = new BufferedImage(img.getWidth(this), img.getHeight(this),
BufferedImage.TYPE_INT_RGB);
Through the debugger I observed that the buffer doesn't actually contain data. So I went the creation of frameGrabber.
frameGrabber = (FrameGrabbingControl) player
.getControl("javax.media.control.FrameGrabbingControl");
Is there a problem with this code. Because JMFStudio works fine in my machine but the code cannot access it. Thank you.
I found the solution. The JMF needs time for initialization. In the example we have to switch a line. Put the
new Timer(3000, this).start();
below the try catch.
The whole block looks like below.
try {
player = Manager.createRealizedPlayer(cdi.getLocator());
player.start();
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (CannotRealizeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
new Timer(3000, this).start();
// Grab a frame from the capture device
frameGrabber = (FrameGrabbingControl) player
.getControl("javax.media.control.FrameGrabbingControl");

No capture devices found in FMJ

Previously I was working with JMF, but JMF need to be installed, but I don't want to add this overhead. That's why I want be moved to FMJ. And FMJ is opensource. :)
There is some sample example given with FMJ source. And there is a FMJStudio, from where I can run and transmit RTP audio captured from microphone.
But when I want to Transmit RTP, using the source below, it couldn't find any capture device.
The complete source can be found on: fmj-20070928-0938_2.zip in FMJ
And the class name of this source class is SimpleVoiceTransmiter.
//final String urlStr = URLUtils.createUrlStr(new File("samplemedia/gulp2.wav"));//"file://samplemedia/gulp2.wav";
Format format;
format = new AudioFormat(AudioFormat.ULAW_RTP, 8000, 8, 1);
//format = new AudioFormat(AudioFormat.ULAW_RTP, 8000.0, 8, 1, AudioFormat.LITTLE_ENDIAN, AudioFormat.SIGNED);
//format = new AudioFormat(BonusAudioFormatEncodings.ALAW_RTP, 8000, 8, 1);
//format = new AudioFormat(BonusAudioFormatEncodings.SPEEX_RTP, 8000, 8, 1, -1, AudioFormat.SIGNED);
//format = new AudioFormat(BonusAudioFormatEncodings.ILBC_RTP, 8000.0, 16, 1, AudioFormat.LITTLE_ENDIAN, AudioFormat.SIGNED);
CaptureDeviceInfo di = null;
//Set to true if you want to transmit audio from capture device, like microphone.
if (true)
{
// First find a capture device that will capture linear audio
// data at 8bit 8Khz
AudioFormat captureFormat = new AudioFormat(AudioFormat.LINEAR, 8000, 8, 1);
Vector devices = CaptureDeviceManager.getDeviceList(captureFormat);
if (devices.size() > 0)
{
di = (CaptureDeviceInfo) devices.elementAt(0);
} else
{
System.err.println("No capture devices");
// exit if we could not find the relevant capturedevice.
System.exit(-1);
}
}
// Create a processor for this capturedevice & exit if we
// cannot create it
Processor processor = null;
try
{
//processor = Manager.createProcessor(new MediaLocator(urlStr));
processor = Manager.createProcessor(di.getLocator());
} catch (IOException e)
{
e.printStackTrace();
System.exit(-1);
} catch (NoProcessorException e)
{
e.printStackTrace();
System.exit(-1);
}
// configure the processor
processor.configure();
while (processor.getState() != Processor.Configured)
{
try
{
Thread.sleep(10);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
processor.setContentDescriptor(new ContentDescriptor(ContentDescriptor.RAW_RTP));
TrackControl track[] = processor.getTrackControls();
boolean encodingOk = false;
// Go through the tracks and try to program one of them to
// output g.711 data.
for (int i = 0; i < track.length; i++)
{
if (!encodingOk && track[i] instanceof FormatControl)
{
if (((FormatControl) track[i]).setFormat(format) == null)
{
track[i].setEnabled(false);
} else
{
encodingOk = true;
}
} else
{
// we could not set this track to g.711, so disable it
track[i].setEnabled(false);
}
}
// At this point, we have determined where we can send out
// g.711 data or not.
// realize the processor
if (encodingOk)
{
if (!new net.sf.fmj.ejmf.toolkit.util.StateWaiter(processor).blockingRealize())
{
System.err.println("Failed to realize");
return;
}
// get the output datasource of the processor and exit
// if we fail
DataSource ds = null;
try
{
ds = processor.getDataOutput();
} catch (NotRealizedError e)
{
e.printStackTrace();
System.exit(-1);
}
// hand this datasource to manager for creating an RTP
// datasink our RTP datasink will multicast the audio
try
{
String url = "rtp://192.168.1.99:49150/audio/1";
MediaLocator m = new MediaLocator(url);
DataSink d = Manager.createDataSink(ds, m);
d.open();
d.start();
System.out.println("Starting processor");
processor.start();
Thread.sleep(30000);
} catch (Exception e)
{
e.printStackTrace();
System.exit(-1);
}
}
When I run this source, The output is: No capture devices
What may be the problem? :-(
Edit: I uninstalled the JMF from my system.
Ok, after two and half days, stuck in the middle of nowhere, I pointed out the problem myself.
The problem was, when I uninstalled JMF it wasn't removed from the CLASSPATH user variable. There was somethinng like:
"C:\PROGRA~1\JMF21~1.1E\lib\sound.jar;C:\PROGRA~1\JMF21~1.1E\lib\jmf.jar;C:\PROGRA~1\JMF21~1.1E\lib;"
and when I removed them, and restarted my computer. Then bingo. The code run without any problem. :)

Categories

Resources