Solved

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

Posted on 2002-03-30
4
1,686 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…
Suggested Courses

734 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