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 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() {
  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);

  browseButton =new Button("browse...");

  getStatsButton = new Button("get stats...");


  results = new TextArea(80,10);

* 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",
      if (lastDirectory != null)
      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());

   * 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)) {
      try {
      } catch (Exception e) {
      elapsed += PAUSE;
      state = stats.getState();
    if (state == MediaStatistics.FAILED || state == MediaStatistics.KNOWN)
      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.setSize(600, 400);;


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).
5.Media player
6.Sample program to demonstrate FramePositioningControl.