Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2002-03-30
4
Medium Priority
?
1,698 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
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 …
This video teaches viewers about errors in exception handling.
Suggested Courses

721 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