views:

253

answers:

0

I am trying to write a small app to play videos using JMF 2.1.1e in Netbeans 6.8, I have cross-posted it at Sun Java Forums and Netbeans Users forum (link can't be added since I am a new user). The code is as follows,

package demo;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.Player;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class DemoApp extends SingleFrameApplication {

    static URL mediaURL;
    static Player mediaPlayer;

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        show(new WindowFrame(this));
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of DemoApp
     */
    public static DemoApp getApplication() {
        return Application.getInstance(DemoApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        try {
            mediaURL = (new File("<filename>")).toURL();
        } catch (MalformedURLException ex) {
            Logger.getLogger(DemoApp.class.getName()).log(Level.SEVERE, null, ex);
        }
        launch(DemoApp.class, args);
    }
}

The second file is,

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WindowFrame.java
 *
 * Created on Jan 30, 2010, 2:09:52 AM
 */
package demo;

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.border.Border;

/**
 *
 * @author flummoxed
 */
public class WindowFrame extends javax.swing.JFrame {

    /** Creates new form WindowFrame */
    public WindowFrame() {
        initComponents();
    }

    WindowFrame(DemoApp aThis) {
        try {
            initComponents();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
//[Code SNIPPED here]
  // </editor-fold>

    private void trafficDensitySliderStateChanged(javax.swing.event.ChangeEvent evt) {                                                 
        // TODO add your handling code here:
        //trafficDensitySlider.enable(false);
        trafficDensitySlider.setEnabled(false);
        String Value;
        if (trafficDensitySlider.getValue() == 1) {
            Value = "Low";
        } else if (trafficDensitySlider.getValue() == 2) {
            Value = "Medium";
        } else {
            Value = "High";
        }
        trafficDensityDisplay.setText(Value);

        try {

            Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true));
            demo.DemoApp.mediaPlayer = Manager.createRealizedPlayer(demo.DemoApp.mediaURL);

            Component video = demo.DemoApp.mediaPlayer.getVisualComponent();
            Component controls = demo.DemoApp.mediaPlayer.getControlPanelComponent();


            if (video != null) {
                videoPanel.add(video, BorderLayout.CENTER); // add video component
                videoPanel.add(controls, BorderLayout.SOUTH); // add control component
                videoPanel.setSize(600, 600);
                videoPanel.setVisible(true);

                demo.DemoApp.mediaPlayer.start();
                videoPanel.validate();
                videoPanel.repaint();

            }

        } catch (IOException ex) {
            Logger.getLogger(WindowFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoPlayerException ex) {
            Logger.getLogger(WindowFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CannotRealizeException ex) {
            Logger.getLogger(WindowFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }



    private void simSpeedSliderStateChanged(javax.swing.event.ChangeEvent evt) {
        // TODO add your handling code here:
        String value;
        int val = simSpeedSlider.getValue();
        HashMap<String, String> simspeeds = new HashMap<String, String>();
        simspeeds.put("0", "1/16x");
        simspeeds.put("1", "1/8x");
        simspeeds.put("2", "1/4x");
        simspeeds.put("3", "1/2x");
        simspeeds.put("4", "1x");
        simspeeds.put("5", "2x");
        simspeeds.put("6", "3x");
        simspeeds.put("7", "4x");
        simspeeds.put("8", "5x");

        simSpeedDisp.setText(simspeeds.get("" + val));

        demo.DemoApp.mediaPlayer.setRate(Float.parseFloat((simspeeds.get("" + val).trim())));
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new WindowFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JPanel VariableDisp;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea msgarea;
    private javax.swing.JTextField simSpeedDisp;
    private javax.swing.JSlider simSpeedSlider;
    private javax.swing.JTextField trafficDensityDisplay;
    private javax.swing.JSlider trafficDensitySlider;
    private javax.swing.JPanel videoPanel;
    // End of variables declaration
} 

The video works with JMF, I have played it using JMFStudio and have also run it using some sample programs available online. The filepath has been blanked out here, but that has been verified too. Upon execution, I can see the controller for the video and can also see that it is playing (the start method has been called). Only the visual component isn't being drawn/redrawn/painted/repainted correctly.

Any suggestions/help will be appreciated, thanks!

PS: Kindly ignore the fact that I have used a static variable when I could have avoided it. This was a quick hash to test JMF and SWT, so that I can go onto actually write the application.