views:

13

answers:

0

Hi, my goal is to make a GUI for this little app, that I copied from the JMF guide I downloaded from Oracle website.

The code capture 10s of audio from the mic and saves it in a file called foo.wav.

But it won't work when I try to make a GUI to it and I don't know what I'm doing wrong. So please, tell me what's wrong.

NewClass:

package exemploaudio;

import java.awt.Component;
import javax.media.Processor;
import javax.swing.JFrame;


public class NewClass extends JFrame{

    public NewClass(Processor p){
        Component visualComponent = p.getVisualComponent();
        add(visualComponent);

        Component controlPanel = p.getControlPanelComponent();
        add(controlPanel);
    }

}

Main Class:

  package exemploaudio;

import java.awt.Component;
import javax.swing.JApplet;
import java.io.IOException;
import java.net.URL;
import java.util.Vector;
import javax.media.*;
import javax.media.control.StreamWriterControl;
import javax.media.format.AudioFormat;
import javax.media.protocol.DataSource;
import javax.media.protocol.FileTypeDescriptor;
import jmapps.util.StateHelper;

public class Main {

    public static void main(String[] args) {
        CaptureDeviceInfo di = null;
        Processor p = null;
        StateHelper sh = null;
        DataSink filewriter = null;

        Vector deviceList = CaptureDeviceManager.getDeviceList(new AudioFormat("Linear", 44100, 16, 2));
        if (deviceList.size() > 0) {
            di = (CaptureDeviceInfo) deviceList.firstElement();
        } else {
            System.exit(-1);
        }

        try {
            p = Manager.createProcessor(di.getLocator());
            sh = new StateHelper(p);
        } catch (IOException e) {
            System.err.println("IOException");
        } catch (NoPlayerException e) {
            System.err.println("NoPlayerException");
        }

        if (!sh.configure(10000)) {
            System.exit(-1);
        }

        p.setContentDescriptor(new FileTypeDescriptor(FileTypeDescriptor.WAVE));

        if (!sh.realize(10000)) {
            System.exit(-1);
        }

        DataSource source = p.getDataOutput();
        try {
            MediaLocator dest = new MediaLocator(new URL("file://foo.wav"));
            filewriter = Manager.createDataSink(source, dest);
            filewriter.open();
        } catch (NoDataSinkException e) {
            System.exit(-1);
        } catch (IOException e) {
            System.exit(-2);
        } catch (SecurityException e) {
            System.exit(-3);
        }

        StreamWriterControl swc = (StreamWriterControl) p.getControl("javax.media.control.StreamWriterControl");

        if (swc != null) {
            swc.setStreamSizeLimit(5000000);
        }

        try {
            filewriter.start();
        } catch (IOException e) {
            System.exit(-1);
        }

        sh.playToEndOfMedia(5000);
        sh.close();
        filewriter.close();


        //This is the part that I made.
        NewClass frame = new NewClass(p);
        frame.setSize(375, 100);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

    }
}