jPDFNotes’ Sound Annotation object produces raw audio through its getContent() method and an AudioFormat object via its getFormat() method. To convert raw audio to any of its supported audio format, you would have to use Java Sound classes for this to happen.

Below is the method to convert the raw audio to a WAV file.

To use it, do this (assuming “sound” is your Sound Annotation object).

If you want the output returned as an array of bytes (in case you want to convert the WAV to some other format like MP3), you can change the AudioSystem.write() method to use an OutputStream object as the last parameter instead of a File object.

Since my method returns a ByteArrayOutputStream object, you can call the method toByteArray() to have it return in byte[].

mp3While MP3 encoding support in Java Sound is nonexistent, you will need 3rd party libraries for doing so. This post will talk about the necessary requirements to convert, say, a WAV file to MP3 format under a Windows environment as the encoding files involved are DLL files. If you are in some other operating system, check the readme files included in the zip files.

A good library to use is Tritonus, which is an implementation of the Java Sound API. You need these two files: tritonus_mp3.jar and tritonus_share.jar which is a shared classes required by all other Tritonus plug-ins. You can add these two files in the JDK and/or JRE’s library extension folder or you can include them in the classpath of your project.

To make use of Tritonus’ libraries to encode to MP3 format, go to their site’s plugin page and download the file tritonus-mp3enc-2001-04-25.zip under mp3 Encoder. That zip file contains a sample java source code for encoding to MP3 format as well as instructions on where to place the included lametritonus.dll (since it is windows, it should be placed in c:\windows\system32).

The other requirement is equally important, called LAME. It is a high quality MPEG Audio Layer III (MP3) encoder licensed under the LGPL. The DLL for this is responsible for the encodings. Download the LAME package and transfer the DLL file lame_enc.dll to c:\windows\system32.

You can get it in http://www.jthz.com/~lame/.

That is it. You can then check if your conversion from WAV to MP3 works by compiling and running the Mp3Encoder.java source file. Keep in mind that those two DLL files are very important so they must both be present.

Related Posts Plugin for WordPress, Blogger...