Skip to main content
Participating Frequently
October 2, 2013
Question

Please help me convert this Java code to ColdFusion

  • October 2, 2013
  • 1 reply
  • 856 views

Hello everyone,

I'm not skilled at Java at all so I could really use your help.  I'm trying to read the duration and bit rate from an mp3 file. I'm using a java library called "mp3spi" from http://www.javazoom.net/mp3spi/documents.html.

So var I've been able to determine that these objects exist:

<cfset AudioFormat = createObject("java", "org.tritonus.share.sampled.TAudioFormat")>

                              <cfset AudioFileFormat = createObject("java", "org.tritonus.share.sampled.file.TAudioFileFormat")>

                              <cfset AudioFileReader = createObject("java", "javax.sound.sampled.spi.AudioFileReader")>

I'm having trouble with the following code and converting it to ColdFusion:

File file = new File("filename.mp3");
AudioFileFormat baseFileFormat = new MpegAudioFileReader().getAudioFileFormat(file);
Map properties = baseFileFormat.properties();
Long duration = (Long) properties.get("duration");

I've tried several ways of setting the above variables, but I keep getting an error that either MpegAudioFileReader or getAudioFileFormat doesn't exist. However when I dump the variables I used to create the Java objects, they do exist.

Here is what I have:

<cfscript>
    mp3file
= FileOpen(ExpandPath("./") & originalfile, "readBinary");
    baseFileFormat
= AudioFileReader.getAudioFileFormat(mp3file);
    properties
= baseFileFormat.properties();
    duration
= properties.get("duration");
</cfscript>

I can't figure out how to use File, AudioFileFormat, Map, and Long respectively in my cfscript.

Thank you.

-- Simone

Message was edited by: shenry  I'm not asking for someone to write my code for me. I'm saying I don't understand this Java code and I'm getting errors with my own code and can't figure out where I've gone wrong.

This topic has been closed for replies.

1 reply

BKBK
Community Expert
Community Expert
December 1, 2013

<!--- A search of the web shows that class of MpegAudioFileReader is javazoom.spi.mpeg.sampled.file.MpegAudioFileReader--->

<cfset mpegAudioFileReader = createObject("java", "javazoom.spi.mpeg.sampled.file.MpegAudioFileReader").init()>

/* ColdFusion's fileOpen() does not return a Java File object! ColdFusion equivalent of: File file = new File("filename.mp3") */

mp3file =  createObject("java", "java.io.File").init("filename.mp3");

/*baseFileFormat is of type javax.sound.sampled.AudioFileFormat*/

baseFileFormat = mpegAudioFileReader.getAudioFileFormat(mp3file)