Solved

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

Posted on 2002-03-30
4
1,671 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groovy example issue 10 90
stackato and cloud 4 74
MySqlDump not dumping triggers 1 19
How  Can  I  resolve   this  Error "java.sql.SQLException: Invalid column index" 4 25
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now