PhoneGap: Attempt to call getDuration without a valid mediaplayer

I use phone-gap to handle the gap between HTML 5 and Android and I’ve been very pleased with the framework thus far. Today, however, I had a very annoying issue with the Media-object found in PhoneGap. I got the following error:

10-27 20:02:14.039: E/MediaPlayer(16984): error (-19, 0)
10-27 20:02:14.039: V/MediaPlayer(16984): callback application
10-27 20:02:14.039: V/MediaPlayer(16984): back from callback
10-27 20:02:14.039: V/MediaPlayer(16984): getDuration_l
10-27 20:02:14.039: E/MediaPlayer(16984): Attempt to call getDuration without a valid mediaplayer
10-27 20:02:14.039: V/MediaPlayer(16984): message received msg=100, ext1=-38, ext2=0
10-27 20:02:14.039: E/MediaPlayer(16984): error (-38, 0)

The Attempt to call getDuration without a valid mediaplayer part of the error seemed to be the most usable and I googled it. It turned out that android can only run a finite amount of sound instances. From the PhoneGap documentation:

Function media.release is a synchronous function that releases the underlying operating systems audio resources. This function is particularly important for Android as there are a finite amount of OpenCore instances for media playback. Developers should call the ‘release’ function when they no longer need the Media resource.

So, I added release() to my code, like this:

var change = new Media("change.wav");
change.setVolume = GlobalSettings.EffectVolume;;

And, instead I got the following error in my catalog:

10-27 20:10:26.049: D/AudioPlayer(21033): renaming /storage/emulated/0/tmprecording.3gp to change.wav
10-27 20:10:26.049: E/AudioPlayer(21033): FAILED renaming /storage/emulated/0/tmprecording.3gp to change.wav

The problem seemed to be that the file wasn’t finished playing (obviously) so it crashed. I then tested to place the release after the sound had finished playing:

var change = new Media("/android_asset/www/sound/effects/Change.wav", function()
change.setVolume = GlobalSettings.EffectVolume;;

This has worked well and has caused no crashes. The second, optional, parameter of the Media object takes a callback that will be called when the sound has finished playing.