Solved

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

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

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
Comment Utility
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
Comment Utility
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
Comment Utility
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
HashMap Vs TreeMap 12 47
what is a "java.lang.System Property"   ? 20 34
eclipse java  build path 6 17
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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

8 Experts available now in Live!

Get 1:1 Help Now