Select a media file from tjelocal file system and gain statistics : JMF « 2D Graphics GUI « Java






Select a media file from tjelocal file system and gain statistics

  

/**********************************************************************
* GUIMediaStatistics - a GUI allowing the user to select a media
* file from tjelocal file system and gain statistics on that media
* (audio or video) concerning its tracks and their format/composition.
*
* The class is built atop the MediaStatistics class and arguably
* provides a more user friendly mechanism for determining the
*format of media.
*
* @author Spike Barlow
***********************************************************************/

import java.awt.*;
import java.awt.event.*;
import javax.media.*;
import java.util.*;
public class GUIMediaStatistics extends Frame implements ActionListener {

  /** Object to determine the format ofthe media. Performs all the real work. */
  protected MediaStatistics     stats;

  /** User wants to search for a file. */
  protected Button              browseButton;

  /** User wants to know the format of the current file. */
  protected Button              getStatsButton;

  /** Current state of processing. */
  protected int                 state;

  /** Where the format informationis displayed. */
  protected TextArea            results;

  /** Area i which the user can type the name of a file. */
  protected TextField           mediaField;

  /** Pause in milliseconds between checks to see if format is known. */
  public static final int       PAUSE = 50;

  /** Maximum wait before giving up on determining theformat. */
  public static final int       MAX_WAIT_PERIOD = 10000;

  /** Directory that the user last searched for files. */
  protected String              lastDirectory;





/**********************************************************************
* Construct the GUIMediaStatistics object, constructing the various
* components and laying them out on the screen.
**********************************************************************/
public GUIMediaStatistics() {
  super("GUIMediaStatistics");
  setLayout(new BorderLayout());

  addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
      System.exit(0); } });

  Panel filePanel = new Panel();

  filePanel.add(new Label("File:"));
  mediaField = new TextField(40);
  filePanel.add(mediaField);

  browseButton =new Button("browse...");
  filePanel.add(browseButton);
  browseButton.addActionListener(this);

  getStatsButton = new Button("get stats...");
  filePanel.add(getStatsButton);
  getStatsButton.addActionListener(this);

  add(filePanel,"North");

  results = new TextArea(80,10);
  results.setEditable(false);
  add(results,"Center");
}


/************************************************************************
* Respond to user button presses - either browsing for a file or
* gathering format info on the current file.
************************************************************************/
public void actionPerformed(ActionEvent e) {

    /////////////////////////////////
    // Browse - use FileDialog class.
    /////////////////////////////////
    if (e.getSource() == browseButton) {
      FileDialog choice = new FileDialog(this, "Media File Choice",
          FileDialog.LOAD);
      if (lastDirectory != null)
        choice.setDirectory(lastDirectory);
      choice.show();
      String selection = choice.getFile();
      if (selection != null) {
        lastDirectory = choice.getDirectory();
        mediaField.setText("file://" + choice.getDirectory()
            + selection);
      }
    }
    ////////////////////////////////////////////////////////
    // Get statistics - create a MediaStatistics object and
    // monitor its progress.
    ///////////////////////////////////////////////////////
    else {
      stats = new MediaStatistics(mediaField.getText());
      monitorAndReport();
    }
  }

  /************************************************************************
   * Monitor the progress of the MediaStatistics object as it attempts to
   * determineformatinformation for the current file. It periodically
   * polls the object, obtaining a report from it and printing it to the
   * screen. The polling stops as soon as the format is known, it is
   * found that it isimpossible to determine a format, or a certain time
   * period is exceeded.
   **************************************************************************/
  private void monitorAndReport() {
    int elapsed = 0;

    state = stats.getState();
    while (elapsed < MAX_WAIT_PERIOD
        && (state != MediaStatistics.FAILED && state != MediaStatistics.KNOWN)) {
      results.setText(stats.getReport());
      try {
        Thread.sleep(PAUSE);
      } catch (Exception e) {
      }
      elapsed += PAUSE;
      state = stats.getState();
    }
    if (state == MediaStatistics.FAILED || state == MediaStatistics.KNOWN)
      results.setText(stats.getReport());
    else
      results.setText("Timing out after maximum period of "
          + MAX_WAIT_PERIOD + "ms");
  }

  /***************************************************************************
   * Main method - construct a GUIMediaStatistics frame and show it on the
   * screen.
   **************************************************************************/
  public static void main(String[] args) {

    GUIMediaStatistics gui = new GUIMediaStatistics();
    gui.pack();
    gui.setSize(600, 400);
    gui.show();
  }
}

           
         
    
  








Related examples in the same category

1.Query the installed version of the JMFQuery the installed version of the JMF
2.Renderer for RGB images using AWT Image using Java Media API
3.A motion detection algoritm for use with the Java Media Framework API (JMF).
4.javax.media
5.Media player
6.Sample program to demonstrate FramePositioningControl.