Matt Gifford aka coldfumonkeh | Consultant Developer
View Github Profile

Using Java to create a CFML .wav file audio player component

Oct 10, 2019

Yesterday I had a thought (it does happen sometimes), and I wondered if it’s possible to make an audio player in CFML that manages audio playback through the browser.

I had a little investigation, and during my lunchbreak today I came up with CFWavPlayer.

I don’t know why you’d want to use this, but you might do.. because, you know, wav files are cool.

This was built just for fun as an experiment, and I love how CFML lets me create and try these things out with relative ease.

Create a new player

Instantiating a new instance of the CFWavPlayer object can be done through the WavBuilder factory.

All methods that interact directly with the audio playback and management can be chained.

This above code will create a new instance of the CFWavPlayer object, load in a file and play it to the end.

You can choose to run an action on the CFWavPlayer instance at a given duration using the wait() method. This is essentially a helper shortcut method to the CFML sleep() function.

So, in the above example, we load a file, start playing it, and five seconds into it we stop the audio.

We could use the wait() method to do other things:

Here we play the loaded wav file, wait five seconds, pause it, wait another 1.5 seconds, then resume playing from where we left off. I know.. the power to control the wav is immensely invigorating.

Jumping the playhead

You can use the jumpTo() method to move the virtual playhead to a given point in the track and then start playing from there:

This can be useful to cut out any annoying introductions to songs. I mean, who wants to listen to a million bars of intro when you can just jump straight to the awesome guitar riff somewhere in the middle? (assuming you know the exact microsecond position of that guitar riff).


You can loop an audio file endlessly, should you wish to:

Note that with the loop() method added to the chain you do not need to call the play() method.

To stop the loop, you would then just need to call the stop() method on the CFWavPlayer instance:

NOTE that if you add a loop() and forget to add a stop(), the only way to kill the sound is to restart your server.

In this looping example we can loop over the audio file which is roughly ten seconds long. After 2.5 loops we can stop it:

Restarting audio

You can also restart the audio from the beginning:

In the above example we play the audio for ten seconds then restart it from the very beginning, playing it through to the end. Just because we can.

Multiple sounds at once!

You can use CFWavPlayer to play multiple sounds at once through the browser. Because who doesn’t like wav-induced headaches?

Before we play the audio in player1 we can get the duration (in microseconds) from the underlying Java Clip instance. We can then convert that duration into milliseconds and pass it into the wait() method on player2, after which we’ll tell it to stop playing.

Running this, both sounds will start and end at the same time, the second looping until the first one finishes.

The parallel play doesn’t seem work in Lucee but I have tested it and it is working on Adobe ColdFusion 2018.

Debugging (kinda)

You can debug (in a way) by also dumping out the current status of the CFWavPlayer instance, as well as the current frame of the audio playhead:

So, that’s CFWavPlayer. It was built for fun in about 60 minutes as an experiment and testing myself to explore a little more about the underlying Java classes. Plus, it’s audio related, so for me that’s a LOT of fun.

You can find the project on my Github repository here

Latest Blog Posts

Jul 16, 2020
Github Actions with CommandBox and TestBox
Read More
Jul 9, 2020
Azure pipelines with CommandBox and TestBox
Read More
Dec 23, 2019
CFML content moderation detection component library
Read More