Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Java code to ColdFusion

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>
like image 300
Simone Henry Avatar asked Dec 20 '22 23:12

Simone Henry


1 Answers

I'm not going to write your code for you, Simone, but there's a coupla general tips.

File file = new File("filename.mp3");

Well as you probably know, CFML is loosely-type, so you can dispense with the typing on the LHS, and then you need to use the createObject() function to create Java objects, which you already have a handle on. CF can't import Java libraries, so you'll need to give a fully-qualified path to the File class. You also need to explicitly call the constructor:

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

(as @Leigh points out below, file is a kinda reserved word in CFML, so best not to use it as a variable name! So I'm using mp3File here)

From there... you should be able to do the work for the other three statements easy enough. Basic method calls and assignments can pretty much be ported straight from the Java source, just lose the static-typing bits as per above, and the type-casting (long) etc.

If you cannot sort everything out from here, update your question with your experimentation, and we can improve this answer (or someone can post a different one). But you need to give us your specific problems, not just a general "write my code please". People won't do that, and you shouldn't be asking people to here (it's against the rules, and people are very big on rules on StackOverflow).

like image 186
Adam Cameron Avatar answered Jan 05 '23 02:01

Adam Cameron