Converting wav's to mp3's

I’m trying to make a tool to convert wav’s to mp3’s then send out a notification when it’s done. (The wav’s will always be 44100Hz 16-bit Stereo files)

My first attempt was to link to JMF then attempt to do this:


	public void wavToMP3(String waveFileName, String mp3FileName) throws Exception
	{
		File file = new File(waveFileName);
		AudioInputStream in = AudioSystem.getAudioInputStream(file);
		AudioInputStream out;
		AudioFormat.Encoding encodedFormat = new AudioFormat.Encoding(javax.media.format.AudioFormat.MPEGLAYER3);
		out = AudioSystem.getAudioInputStream(encodedFormat, in);
		File outfile = new File(mp3FileName);
		AudioSystem.write(out, new AudioFileFormat.Type("MP3","mp3"), outfile);
	}


but that results in this error:

Exception in thread “main” java.lang.IllegalArgumentException: Unsupported conversion: mpeglayer3 from PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian

I can’t seem to find what I need to change to fix this, any suggestions?

The JDK can’t read MP3s. You can use the JavaZoom project. It isn’t written very well, but works for most MP3s.

And just on the off-chance - if you can get away with using Ogg Vorbis format instead of MP3, you can use the JOrbis library.

Cas :slight_smile:

Indeed. OGG, FLAC, and APE have decent Java decoding libs.

the final format needs to be mp3 to be playable in flash(this is on the server) and as far as I know it doesn’t support ogg, flac or ape

it also seems javazoom’s domain is down

I found a link to JLayer and it only seems to be able to go from mp3->wav but has no way of going back again, is there a library to do this that doesn’t require an EasyJNI license?

Don’t think so. Stupid, eh?

I guess this will have to do:


	public void wavToMP3(String waveFileName, String mp3FileName) throws Exception
	{
		Runtime runtime = Runtime.getRuntime();
		Process proc = runtime.exec(LAME_PATH+" "+waveFileName+" "+mp3FileName);
	}