Solved

JMF: converter from WAV to MP3 isn't working !!!!

Posted on 2002-03-30
4
1,674 Views
Last Modified: 2012-08-13
I have the sample code below.  When I run it, it generates a new file, out.mp3, that is of the same size as that of the wave file!!!!! Even though MP3 format is much smaller than WAV.  Also, the generated file is unplayable.  Any hints???  

Thank you
0
Comment
Question by:bustany
  • 2
4 Comments
 

Author Comment

by:bustany
ID: 6907726

import java.net.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.control.*;
import javax.media.protocol.*;
import javax.swing.*;
import java.util.*;

public class WaveConverter extends JFrame
{    // processor for handling the conversion
    Processor p = null;
    // datasink for creating output URL for converted media data
    DataSink sink = null;
    String sourceURL, destURL;
    // main functions just constructs and passes params to app class
    // which does all the work
    public static void main(String args[])
    {
        WaveConverter app=new WaveConverter(args[0], args[1]);
    }
    // contructor does ain setup and registers processor listener
    public WaveConverter(String sourceURL, String destURL)
    {
        super("Visual Processor Converter Application");
        this.sourceURL=sourceURL;
        this.destURL=destURL;
        try
        {
            // create processor to perform format conversion
            // (we can add other processing later if wish to)
            p = Manager.createProcessor(new MediaLocator(sourceURL));
            // add a listener to the processor to handle state transitions
            // and call appropriate methods at suitable times
            // (see table in lecture notes on processor states)
            p.addControllerListener(new ProcessorAdapter());
            // must call configure separately otherwise
            // configuration states are bypassed
            p.configure();
            p.start();

        } catch ( Exception e )
        {
            // cannot convert to mp3
            System.out.println("Exception creating processor: " + e);
            System.exit(-1);
        }
    }
    // **********************************************
    // Adapter class to perform Processor event handling
    // **********************************************
    class ProcessorAdapter extends ControllerAdapter
    {
        // do Processor configuration (set it up to convert source to MP3)
        public void configureComplete(ConfigureCompleteEvent e)
        {
            // Need to set the output content type before processor is realized
            p.setContentDescriptor(new
                                   FileTypeDescriptor(FileTypeDescriptor.WAVE));
            // Get the track control objects
            TrackControl track[] = p.getTrackControls();
            // Go through the tracks and try to program one of them
            // to output mp3 data.
            for ( int i = 0; i < track.length; i++ )
            {
                try
                {
                    // set output format to MP3
                    System.out.println(i + ") Setting track " + track[i] + "to MP3");
                    track[i].setFormat(new AudioFormat(AudioFormat.MPEGLAYER3));
                } catch ( Exception ex )
                {
                    // cannot convert to mp3
                    System.out.println("Cannot convert " + sourceURL + " to mp3");
                    p.close();
                }
            }
        }
        // cannot write output to file until processor is realized
        public void realizeComplete(RealizeCompleteEvent event)
        {
            // call helper function to write output to file
            writeProcessorOutputToFile(p, destURL);
        }
        // close Processor when end of input media is encountered
        public void endOfMedia(EndOfMediaEvent e)
        {
            p.close();
        }
        // exit app when Processor is closed
        public void controllerClosed(ControllerClosedEvent e)
        {
            System.exit(0);
        }
    }

    // write output DataSource of Processor to a URL
    // pre-condition: Processor p must be realized
    public void writeProcessorOutputToFile(Processor p, String destURL)
    {
        DataSource source = p.getDataOutput();
        // create a datasink to do the file writing and open the sink to
        // make sure we can write to it
        try
        {
            sink = Manager.createDataSink(source, new MediaLocator(destURL));
            sink.open();
            sink.start();
        } catch ( Exception e )
        {
            System.out.println(e);
            System.exit(-1);
        }
    }
}

0
 

Author Comment

by:bustany
ID: 6907729
Here is the syntax for running it:

java WaveConverter file://D:/tmp/sound.wav file://D:/tmp/out.mp3
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7923921
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
PAQ'd and pts refunded
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 7978527
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java JRE greater than 1.6 5 63
ForLoop Example 3 49
MySQL  on Tomcat 8 46
login form jsp example 2 26
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question