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

📄 mediaplayerframe.java

📁 用java 播放音频
💻 JAVA
字号:
import javax.media.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.io.*;

/**
 * An instance of the MediaPlayerFrame may be used to display any media
 * recognized * by JMF.  This is intended to be a very simple GUI example,
 * displaying all possible controls for the given media type.
 */
public class MediaPlayerFrame extends JFrame
{

  private static final String FRAME_TITLE = "developerWorks JMF Tutorial " +
    "Media Player";
  private static final String CONTROL_PANEL_TITLE = "Control Panel";

  // location and size variables for the frame.
  private static final int LOC_X = 100;
  private static final int LOC_Y = 100;
  private static final int HEIGHT = 500;
  private static final int WIDTH = 500;

  /**
   * The current player.
   */
  private Player player = null;

  /**
   * The tabbed pane for displaying controls.
   */
  private JTabbedPane tabPane = null;

  /**
   * Create an instance of the media frame.  No data will be displayed in the
   * frame until a player is set.
   */
  public MediaPlayerFrame()
  {
    super(FRAME_TITLE);
    setLocation(LOC_X, LOC_Y);
    setSize(WIDTH, HEIGHT);
    //JTabbedPane aa= new JTabbedPane();
    tabPane = new JTabbedPane();
    getContentPane().add(tabPane);
    // getContentPane().add(aa);
    /* adds a window listener so that the player may be cleaned up before
    the frame actually closes.
     */
    addWindowListener(new WindowAdapter()
    {
      /**
       * Invoked when the frame is being closed.
       */
      public void windowClosing(WindowEvent e)
      {
        closeCurrentPlayer(); System.exit(0);
      }
    }
    );
  }

  private JPanel createMainPanel()
  {
    JPanel mainPanel = new JPanel();
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();

    mainPanel.setLayout(gbl);

    boolean visualComponentExists = false;

    // if the visual component exists, add it to the newly created panel.
    if (player.getVisualComponent() != null)
    {
      visualComponentExists = true;
      gbc.gridx = 0;
      gbc.gridy = 0;
      gbc.weightx = 1;
      gbc.weighty = 1;
      gbc.fill = GridBagConstraints.BOTH;
      mainPanel.add(player.getVisualComponent(), gbc);
    }

    // if the gain control component exists, add it to the new panel.
    if ((player.getGainControl() != null) && (player.getGainControl()
      .getControlComponent() != null))
    {
      gbc.gridx = 1;
      gbc.gridy = 0;
      gbc.weightx = 0;
      gbc.weighty = 1;
      gbc.gridheight = 2;
      gbc.fill = GridBagConstraints.VERTICAL;
      mainPanel.add(player.getGainControl().getControlComponent(), gbc);
    }

    // Add the control panel component if it exists (it should exists in
    // all cases.)
    if (player.getControlPanelComponent() != null)
    {
      gbc.gridx = 0;
      gbc.gridy = 1;
      gbc.weightx = 1;
      gbc.gridheight = 1;

      if (visualComponentExists)
      {
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weighty = 0;
      }
      else
      {
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weighty = 1;
      }

      mainPanel.add(player.getControlPanelComponent(), gbc);
    }

    return mainPanel;
  }


  public void setMediaLocator(MediaLocator locator)throws IOException,
    NoPlayerException, CannotRealizeException
  {

    // create a new player with the new locator.  This will effectively
    // stop and discard any current player.
    setPlayer(Manager.createRealizedPlayer(locator));
  }


  public void setPlayer(Player newPlayer)
  {
    // close the current player
    closeCurrentPlayer();

    player = newPlayer;

    // refresh the tabbed pane.
    tabPane.removeAll();

    if (player == null)
      return ;

    // add the new main panel
    tabPane.add(CONTROL_PANEL_TITLE, createMainPanel());

    Control[]controls = player.getControls();
    for (int i = 0; i < controls.length; i++)
    {
      if (controls[i].getControlComponent() != null)
      {
        tabPane.add(controls[i].getControlComponent());
      }
    }
  }

  /**
   * Stops and closes the current player if one exists.
   */
  private void closeCurrentPlayer()
  {
    if (player != null)
    {
      player.stop();
      player.close();
    }
  }

  /**
   * Prints a usage message to System.out for how to use this class
   * through the command line.
   */
  public static void printUsage()
  {
    System.out.println("Usage: java MediaPlayerFrame mediaLocator");
  }

  /**
   * Allows the user to run the class through the command line.
   * Only one argument is allowed, which is the media locator.
   */
  public static void main(String[]args)
  {
    try
    {
      if (args.length == 1)
      {
        MediaPlayerFrame mpf = new MediaPlayerFrame();
        mpf.setMediaLocator(new MediaLocator(new File(args[0]).toURL()));
        mpf.show();
      }
      else
      {
        printUsage();
      }
    }
    catch (Throwable t)
    {
      t.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

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