⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m4appsampleswing.java

📁 M4AppSampleSwing.javafdfdf fgfgfdsgfgd
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************\
**                                                                            **
** Licensed Materials - Property of IBM                                       **
** IBM Toolkit for MPEG-4                                                     **
** Copyright IBM Corp. 1998-2005  All Rights Reserved                         **
**                                                                            **
\******************************************************************************/
package test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import player.PlayerAdapter;
import player.PlayerControl;
import player.PlayerControlPanel;
import player.PlayerControlPanelFactory;
import player.PlayerFactory;

/*******************************************************************************
 * IBM Toolkit for MPEG-4 Sample Player application for Swing. This code has
 * been adapted from the M4AppSample which was written using the AWT for the UI.
 *
 */
public class M4AppSampleSwing extends JFrame
{
  private final static String kPlayerName = "IBM Toolkit for MPEG-4 Sample Player";

  static
  {
    // Set the Swing UI to have the look and feel of the system its running on. We do this here in
    // a static initialization block so its done ahead of the menu and menu items being created
    // when the class is instantiated below.
    //
    try
    {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception ignored) {/* ignored - will use default look and feel should this fail */}
  }

  // The menus for the player; indented to show highlight the actual layout
  //
  private JMenu                fileMenu          = new JMenu("File");
  private JMenuItem            openMenuItem        = new JMenuItem("Open...");
  private JMenuItem            closeMenuItem       = new JMenuItem("Close");
  private JMenuItem            exitMenuItem        = new JMenuItem("Exit");

  private JMenu                viewMenu          = new JMenu("View");
  private JCheckBoxMenuItem    autosizeMenuItem    = new JCheckBoxMenuItem("Size window to new content");
  private JCheckBoxMenuItem    scaleMenuItem       = new JCheckBoxMenuItem("Scale content to window");
  private JCheckBoxMenuItem    speedScaleMenuItem  = new JCheckBoxMenuItem("Scaling; speed (vs. quality)");
  private JMenu                sizeMenu            = new JMenu("Window size");
  private JMenuItem            size50MenuItem        = new JMenuItem("50%");
  private JMenuItem            size100MenuItem       = new JMenuItem("100%");
  private JMenuItem            size200MenuItem       = new JMenuItem("200%");
  private JMenuItem            sizeHalfMenuItem      = new JMenuItem("x \u00BD");
  private JMenuItem            sizeTwiceMenuItem     = new JMenuItem("x 2");
  private JCheckBoxMenuItem    ctlBarMenuItem      = new JCheckBoxMenuItem("Control Bar");

  private JMenu                playMenu          = new JMenu("Play");
  private JMenuItem            playPauseMenuItem   = new JMenuItem("Play/Pause");
  private JMenuItem            stopMenuItem        = new JMenuItem("Stop");
  private JMenu                atDurActMenu        = new JMenu("At duration");
  private JRadioButtonMenuItem endContMenuItem       = new JRadioButtonMenuItem("Continue");
  private JRadioButtonMenuItem endStopMenuItem       = new JRadioButtonMenuItem("Stop");
  private JRadioButtonMenuItem endRptMenuItem        = new JRadioButtonMenuItem("Restart");
  private JCheckBoxMenuItem    muteMenuItem        = new JCheckBoxMenuItem("Mute");

  private PlayerControl      m4Player;         // This is the control interface for the player
  private PlayerControlPanel m4PlayerPanel;    // This is for the control panel bar

  private boolean playerAdded;                 // True when player renderer component is added to our frame

  /*****************************************************************************
   * The constructor creates the player, sets up listeners, sets up
   * behavior using the player control, adds a player control panel
   * and shows the application
   */
  public M4AppSampleSwing()
  {
    // Create the MPEG-4 player and add our listener class that has been derived from the PlayerAdapter
    //
    /*
     * // This commented code is the orginal code for this sample used with IBM Toolkit For MPEG-4
     * // versions up to 1.2.6. In version 1.2.6 a new lightweight rendering player was introduced and
     * // that is now created below. With that we can put lightwight swing components over the player
     * // and we don't need anymore the line of code to set JPopupMenu so menus come above the player.
     * // The heavweight version can still be used, hence the lines of code have been kept here to show
     * // how its done for that case.
     *
     * m4Player = PlayerFactory.createMPEG4Player();
     *
     * // Make sure we use have menus that will come up over the heavyweight player component
     * //
     * javax.swing.JPopupMenu.setDefaultLightWeightPopupEnabled(false);
     *
     */
    m4Player = PlayerFactory.createLightweightMPEG4Player();  // From 1.2.6 we can now create a lightweight player

    m4Player.addListener(new M4AppPlayerAdapter());

    // This is to allow the frame to close when we click the close button
    //
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {exit();}
    });

    // This is for when we are showing the title in the main frame to update as it resizes
    //
    addComponentListener(new ComponentAdapter() {
      public void componentResized(ComponentEvent e) {if (!playerAdded) repaint();}
    });

    // Put in our custom pane which will show our text 'logo'
    //
    setContentPane(new MyJPanel());

    // We use a border layout with the player's renderer component in the center and the control panel
    // to the south
    //
    getContentPane().setLayout(new BorderLayout());

    // Create the menus bar and setup the player
    //
    createMenuBar();
    //
    m4Player.setAutoSize(false);                     // We don't want the forced autosize, we do any resize ourself
    m4Player.setScaling(scaleMenuItem.getState());   // Scaling is set as per the menu state
    m4Player.setSpeedScaling(speedScaleMenuItem.getState());
    setEndAction(PlayerControl.END_ACTION_CONTINUE); // We set a default end behavior to carry on playing
    endContMenuItem.setSelected(true);
    atDurActMenu.setEnabled(false);                  // wait until we have content to enable this (see M4AppPlayerAdapter)
    m4Player.setMute(muteMenuItem.getState());       // Mute is as per the menu state

    // Create a control panel and set the menus to match
    //
    m4PlayerPanel = PlayerControlPanelFactory.createPlayerControlPanel("Basic", m4Player);
    if (m4PlayerPanel != null)
    {
      m4PlayerPanel.setBackground(Color.lightGray);
      showControlBar(true);
      ctlBarMenuItem.setEnabled(true);
      ctlBarMenuItem.setState(true);
    }

    // Lastly put the sample into the window title and show the player at a reasonable starting size
    //
    setTitle(kPlayerName);
    setSize(360, 280);
    show();
  }

  /*****************************************************************************
   * Creates the menus bar, sets up listeners for the menus and initializes
   * menus according to an initial setup (Note: some menu settings could be
   * saved as persistent preferences and reloaded, but that is beyond the scope
   * of this sample player)
   */
  private void createMenuBar()
  {
    JMenuBar  mb = new JMenuBar();

    mb.add(fileMenu);
    //
    openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, java.awt.Event.CTRL_MASK));
    openMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {open();}
    });
    fileMenu.add(openMenuItem);
    //
    closeMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {close();}
    });
    fileMenu.add(closeMenuItem);
    //
    fileMenu.addSeparator();
    exitMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {exit();}
    });
    fileMenu.add(exitMenuItem);

    mb.add(viewMenu);
    //
    viewMenu.add(autosizeMenuItem);
    //
    scaleMenuItem.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {m4Player.setScaling(scaleMenuItem.getState());}
    });
    viewMenu.add(scaleMenuItem);
    //
    speedScaleMenuItem.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {m4Player.setSpeedScaling(speedScaleMenuItem.getState());}
    });
    viewMenu.add(speedScaleMenuItem);
    //
    viewMenu.add(sizeMenu);
    //
    sizeMenu.add(size50MenuItem);
    size50MenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, java.awt.Event.CTRL_MASK));
    size50MenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {setSizePercent(50);}
    });
    //
    sizeMenu.add(size100MenuItem);
    size100MenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, java.awt.Event.CTRL_MASK));
    size100MenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {setSizePercent(100);}
    });
    //
    sizeMenu.add(size200MenuItem);
    size200MenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, java.awt.Event.CTRL_MASK));
    size200MenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {setSizePercent(200);}
    });
    //
    sizeMenu.addSeparator();
    sizeMenu.add(sizeHalfMenuItem);
    sizeHalfMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, java.awt.Event.CTRL_MASK));
    sizeHalfMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {setSizeActual(50);}
    });
    //
    sizeMenu.add(sizeTwiceMenuItem);
    sizeTwiceMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2, java.awt.Event.CTRL_MASK));
    sizeTwiceMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {setSizeActual(200);}
    });
    //
    viewMenu.addSeparator();
    viewMenu.add(ctlBarMenuItem);
    ctlBarMenuItem.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {showControlBar(ctlBarMenuItem.getState());}
    });

    mb.add(playMenu);
    //
    playMenu.add(playPauseMenuItem);
    playPauseMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, java.awt.Event.CTRL_MASK));
    playPauseMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {playPauseAction();}
    });
    //
    playMenu.add(stopMenuItem);
    stopMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, java.awt.Event.CTRL_MASK));
    stopMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {stopAction();}
    });
    //
    playMenu.addSeparator();
    playMenu.add(atDurActMenu);
    //
    atDurActMenu.add(endContMenuItem);
    endContMenuItem.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {setEndAction(PlayerControl.END_ACTION_CONTINUE);}
    });
    //
    atDurActMenu.add(endStopMenuItem);
    endStopMenuItem.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {setEndAction(PlayerControl.END_ACTION_STOP);}
    });
    //
    atDurActMenu.add(endRptMenuItem);
    endRptMenuItem.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {setEndAction(PlayerControl.END_ACTION_REPEAT);}
    });
    ButtonGroup bg = new ButtonGroup(); // The end action menus are to act as a radio button group
    bg.add(endContMenuItem);
    bg.add(endStopMenuItem);
    bg.add(endRptMenuItem);
    //
    playMenu.addSeparator();
    playMenu.add(muteMenuItem);
    muteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, java.awt.Event.CTRL_MASK));
    muteMenuItem.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {m4Player.setMute(muteMenuItem.getState());}
    });

    closeMenuItem.setEnabled(false);
    autosizeMenuItem.setState(true);
    sizeMenu.setEnabled(false);
    ctlBarMenuItem.setEnabled(false);
    playPauseMenuItem.setEnabled(false);
    stopMenuItem.setEnabled(false);

    setJMenuBar(mb);
  }

  /*****************************************************************************
   * Main entry point for the sample application.  We can pass a url from the
   * command line here; a local file or remote server e.g.  rtsp:// type url.
   * With this sample application passing a remote url here is the only way to
   * access remote content as this sample player has only a file open dialog.
   * (An OpenURL dialog could be added but again is beyond the scope of this
   * sample).
   *
   * @param args A string as a url point to media content
   */
  public static void main(String[] args)
  {
    // Create a new instance of our application
    M4AppSampleSwing app = new M4AppSampleSwing();
    if (args.length > 0)
    {
      app.openPlayUrl(args[0]);  // invoke content directly from the command line
    }
  }

  /*****************************************************************************
   * This opens and plays the content referenced by the url. The
   * player renderer component is added if it has not already been added.
   * (i.e. we can Open a new content without closing the prior one).
   *
   * @param url    String referencing the content e.g. myfile.mp4,
   *               rtsp://myserver.com/myfile.mp4
   */
  private void openPlayUrl(String url)
  {
    // Stop & close any playback that may be running still
    //
    m4Player.stopUrl();

    try
    {
      // We title our window as per the content and added the player renderer
      // component if needed. Now we have the player added we can enable the size
      // menu to allow the content display size to be set.
      //
      setTitle(url);
      if (!playerAdded)
      {
        getContentPane().add("Center", m4Player.getRendererComponent());
        playerAdded = true;
        sizeMenu.setEnabled(true);
        validate();
      }

      m4Player.open(url);
      m4Player.start();
    }
    catch (Exception e)
    {
      m4Player.stopUrl();                  // Stop MPEG-4 player
      setTitle(e.toString());              // and set error in title bar
    }
  }

  /*****************************************************************************
   * Opens a local file from the file dialog and plays it
   */
  private void open()
  {
    JFileChooser openFileChooser = new JFileChooser();
    int retVal = openFileChooser.showOpenDialog(this);
    if (retVal == JFileChooser.APPROVE_OPTION)
    {
      openPlayUrl(openFileChooser.getSelectedFile().getAbsolutePath());
    }
  }

  /*****************************************************************************
   * Stop playing and close any content, remove the player renderer
   * component. By closing the content we close any connection to it, e.g.. over

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -