I've struggled a lot with Java but could not combine a working example of Java .wav to .mp3 converter. This converter will be used in a Java applet so it should depend only on libraries written in pure Java with no underlying C code calls.
Can anyone provide a fully working example?
Thank you
Read your wave file @ http://java.sun.com/javase/technologies/desktop/media/jmf/ and encode to mp3 @ http://openinnowhere.sourceforge.net/lameonj/
As pointed out, lameonj is not a pure java solution. For that the options don't seem so many, but see the other SO question: MP3 Encoding in Java
If speed is not important for you, take any c implementation of MP3 (e. g. lame) and try to compile it with NestedVM to Java bytecode. It will be slow (like an emulator in an emulator), but it should work.
And it should be way less work than trying to port a MP3 library to pure Java.
I use Jump3r to convert wav to mp3 on my project because the html5 player of IE11 can't play wav files.
Jump3r is the simpliest solution found to run inside a tomcat servlet. I wasn't able to integrate others solutions like jave certainly due to the security manager... Jump3r is a pure java program.
Jump3r is available on the maven repository (https://mvnrepository.com/artifact/de.sciss/jump3r/1.0.4) and the sources are available on github (https://github.com/Sciss/jump3r)
To convert a file, you should call the main method (in the following code, I use an inlined version of the main method to catch/throw an IOException if necessary)
private void convertWavFileToMp3File(File source, File target) throws IOException {
String[] mp3Args = { "--preset","standard",
"-q","0",
"-m","s",
source.getAbsolutePath(),
target.getAbsolutePath()
};
(new Main()).run(mp3Args);
}
Another potentially relevant project is jffmpeg. This apparently aimed to an JMF support for a wide range of formats using both native and Java codecs. Judging from the 'formats' page, they made significant progress on the pure Java side. Unfortunately, the project has gone quiet.
This doesn't directly help the OP in the short term. But if he or others are keen to have pure Java codecs in the long term, consider getting involved.
Just check out the following source code. http://jsidplay2.cvs.sourceforge.net/jsidplay2/jump3r It is still work in progress, but a working example of the encoder part of a pure java based lame library.
See this link on SourceForge http://sourceforge.net/projects/jump3r/files/
Its JAR's only (no source code), but it does work on both PC and Android, but no necessarily as described in the authors posting http://pure-java-mp3-encoder.blogspot.com.au/
I got it to work by just using the jump3r-1.0.3.jar file, as a library, and instantiated mp3.Main then used called mp3.run() e.g. part of my Android code
String[] mp3Args = {"--preset","standard",
"-q","0",
"-m","s",
Environment.getExternalStorageDirectory().getPath()+"/myfile.wav",
Environment.getExternalStorageDirectory().getPath()+"/myfile.mp3"};
Main m = new mp3.Main();
try
{
m.run(mp3Args);
}
catch(Exception e)
{
System.out.println("ERROR processing MP3 " + e);// Some bug in Android seems to cause error BufferedOutputSteam is Closed. But it still seems to work OK.
}
I suspect it would be possible to directly call the lame encoder passing buffers of data etc, but as the exact API for this Java version is not documented, it would require some research
来源:https://stackoverflow.com/questions/4997923/is-there-any-pure-java-way-to-convert-wav-to-mp3