| Author |
Need help writing non-gui JMF program
|
Scott Wade
Greenhorn
Joined: Jun 02, 2005
Posts: 4
|
|
I need to write a program that will convert a WAV file from a ULAW format to a GSM WAV file format. I first tried this with the Java Sound API and the WAV file format of GSM was not supported so I stumbled across JMF. I can use the JMStudio and Export GUI programs that come with JMF to convert the wav file as needed. But now I'm trying to write a batch program (non-gui) to convert the file on the fly. First, is this possible. Second, does anyone have any code samples of a batch java program using JMF in batch. Thanks, Scott
|
 |
Scott Wade
Greenhorn
Joined: Jun 02, 2005
Posts: 4
|
|
I figured it out. Include the JMF.jar in your project and here is the code. import java.io.IOException; import javax.media.DataSink; import javax.media.Manager; import javax.media.MediaLocator; import javax.media.NoDataSourceException; import javax.media.Processor; import javax.media.control.TrackControl; import javax.media.format.AudioFormat; import javax.media.protocol.DataSource; import javax.media.protocol.FileTypeDescriptor; import jmapps.util.StateHelper; public class TestAudio5 { public static void main(String[] args) { try { Processor p = null; StateHelper sh = null; DataSource inSource = Manager.createDataSource( new MediaLocator("file:c:\\ASSURANCE_THREE_MINUTE.wav")); p = Manager.createProcessor(inSource); sh = new StateHelper(p); //Configure the processor if (!sh.configure(10000)) { System.out.println("can't configure"); System.exit(-1); } p.setContentDescriptor(new FileTypeDescriptor(FileTypeDescriptor.WAVE)); //AudioFormat(java.lang.String encoding, double sampleRate, int sampleSizeInBits, int channels) //AudioFormat( //java.lang.String encoding, //double sampleRate, //int sampleSizeInBits, //int channels, //int endian, //int signed, //int frameSizeInBits, //double frameRate, //java.lang.Class dataType) AudioFormat outputFormat = new javax.media.format.AudioFormat( AudioFormat.GSM_MS, 8000.0, 0, 1, 0, 1, 520, 1625.0, null); System.out.println("outputFormat: " + outputFormat.toString()); TrackControl tc[] = p.getTrackControls(); for ( int i = 0; i < tc.length; i++ ) { tc[i].setEnabled(true); System.out.println("before format: " + tc[i].getFormat().toString()); tc[i].setFormat(outputFormat); System.out.println("after format: " + tc[i].getFormat().toString()); } if (!sh.realize(10000)) { System.out.println("can't realize"); System.exit(-1); } // get the output of the processor DataSource source = p.getDataOutput(); // create a File protocol MediaLocator with the location // of the file to which bits are to be written MediaLocator dest = new MediaLocator("file://c:\\test.wav"); // create a datasink to do the file writing & open the // sink to make sure we can write to it. DataSink filewriter = null; filewriter = Manager.createDataSink(source, dest); filewriter.open(); // now start the filewriter and processor filewriter.start(); sh.playToEndOfMedia(5000); sh.close(); filewriter.close(); } catch (NoDataSourceException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("done!"); System.exit(-1); } }
|
 |
 |
|
|
subject: Need help writing non-gui JMF program
|
|
|