📄 jmfmediaplayer.java
字号:
/* * File: JMFMediaPlayer.java * Project: MPI Linguistic Application * Date: 02 May 2007 * * Copyright (C) 2001-2007 Max Planck Institute for Psycholinguistics * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package mpi.eudico.client.annotator.player;import mpi.eudico.client.annotator.ElanLayoutManager;import mpi.eudico.client.annotator.ElanLocale;import mpi.eudico.client.annotator.gui.FormattedMessageDlg;import mpi.eudico.client.mediacontrol.ControllerEvent;import mpi.eudico.client.mediacontrol.ControllerListener;import mpi.eudico.client.mediacontrol.ControllerManager;import mpi.eudico.client.mediacontrol.PeriodicUpdateController;import mpi.eudico.client.mediacontrol.TimeEvent;import mpi.eudico.server.corpora.clomimpl.abstr.MediaDescriptor;import mpi.util.TimeFormatter;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.IOException;import java.net.URL;import javax.media.Control;import javax.media.EndOfMediaEvent;import javax.media.GainControl;import javax.media.Manager;import javax.media.MediaLocator;import javax.media.Player;import javax.media.StopAtTimeEvent;import javax.media.Time;//import javax.media.control.*;import javax.swing.JMenuItem;import javax.swing.JPopupMenu;import javax.swing.SwingUtilities;/** * The JMF implementation of an elan media player *///public class JMFMediaPlayer extends ControllerManager implements ElanMediaPlayer, javax.media.ControllerListener {public class JMFMediaPlayer extends ControllerManager implements ElanMediaPlayer, ControllerListener, javax.media.ControllerListener, ActionListener { /** Holds value of property DOCUMENT ME! */ protected Player player; // the JMF player /** Holds value of property DOCUMENT ME! */ protected float shouldBeRate; // used to remember the player rate since JMF sometimes forgets it /** Holds value of property DOCUMENT ME! */ protected long intervalStopTime; /** Holds value of property DOCUMENT ME! */ protected long frozenMediaTime; /** Holds value of property DOCUMENT ME! */ protected long offset; /** Holds value of property DOCUMENT ME! */ protected long milliSecondsPerSample; /** Holds value of property DOCUMENT ME! */ protected boolean playingInterval; /** Holds value of property DOCUMENT ME! */ protected PeriodicUpdateController periodicController; /** Holds value of property DOCUMENT ME! */ protected float systemVolumeLevel; /** Holds value of property DOCUMENT ME! */ protected JPopupMenu popup; /** Holds value of property DOCUMENT ME! */ protected MediaDescriptor mediaDescriptor; /** Holds value of property DOCUMENT ME! */ protected ElanLayoutManager layoutManager; /** Holds value of property DOCUMENT ME! */ protected boolean detached; /** Holds value of property DOCUMENT ME! */ protected JMenuItem durationItem; /** Holds value of property DOCUMENT ME! */ protected JMenuItem detachItem; private JMenuItem infoItem; /** Holds value of property DOCUMENT ME! */ protected NoMouseComponent noMouseComponent; //protected FramePositioningControl fpc; // private FramePositioningControl fpc; /** * Create a JMFMediaPlayer for a media URL * * @param mediaDescriptor DOCUMENT ME! * * @throws NoPlayerException DOCUMENT ME! */ public JMFMediaPlayer(MediaDescriptor mediaDescriptor) throws NoPlayerException { this.mediaDescriptor = mediaDescriptor; // WebStart related initialization, see at the end of this file for details JMFClassLoader.initJMFJNI(); // light weight in JMF is unusable because some mpegs can not be rendered in this mode // Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true)); // Manager.setHint(Manager.PLUGIN_PLAYER, new Boolean(true)); String URLString = mediaDescriptor.mediaURL; //URLString = "file:///" + URLString.substring(i); //URLString = "file://Sun03/Corpora/media-archive/lac_data/senft/StagedEvents/Media/SE-1.mpg"; //URLString = "rtsp://nt06.mpi.nl/onno/echo2a_6.mp4"; //URLString = "rtsp://nt06.mpi.nl/SyncTest_hinted.mov"; //URLString = "rtsp://nt06.mpi.nl:80/De_Eng.mpg"; player = null; try { System.out.println("mediaURL = " + URLString); if (URLString.startsWith("rtsp")) { // do something for streaming System.out.println("stream"); MediaLocator ml = new MediaLocator(URLString); player = Manager.createPlayer(ml); } else { URL mediaURL = new URL(URLString); player = Manager.createPlayer(mediaURL); // player = (Player) Manager.createProcessor(mediaURL); } } catch (javax.media.NoPlayerException e) { System.out.println( "javax.media.NoPlayerException while creating JMF player"); e.printStackTrace(); throw new NoPlayerException("javax.media.NoPlayerException"); } catch (IOException e) { System.out.println("IO exception while creating JMF player"); e.printStackTrace(); throw new NoPlayerException( "IO exception, problem to connect to data source"); } catch (Exception e) { System.out.println("Unknown exception while creating JMF player"); e.printStackTrace(); throw new NoPlayerException( "Exception, problem to connect to data source"); } // initialize local parameters offset = mediaDescriptor.timeOrigin; shouldBeRate = 1.0f; frozenMediaTime = -1; milliSecondsPerSample = 40; // make sure the player is prefetched so the client code is not bothered with it // for .mpg a start stop sequence is needed to make setMediaTime possible before // the client code called player.start(). This is needed to be able to look for // a certain scene before the movie was started. It looks like JMF does not read // all the necessary info to do this when it prefetches a player on mpg data. // mute the player during start stop long timeout = System.currentTimeMillis() + 5000; player.realize(); while (getState() != javax.media.Controller.Realized) { try { Thread.sleep(50); if (System.currentTimeMillis() > timeout) { player.close(); throw new NoPlayerException( "Could not realize the jmf player"); } } catch (InterruptedException e) { e.printStackTrace(); } } // printControls(player); /* fpc = (FramePositioningControl)player.getControl("javax.media.control.FramePositioningControl"); if (fpc == null) { System.err.println("The player does not support FramePositioningControl."); } else { System.err.println("OK"); } */ GainControl gain = player.getGainControl(); float level = 1.0f; if (gain != null) { level = gain.getLevel(); gain.setLevel(0); } systemVolumeLevel = level; start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } stop(); setMediaTime(0); if (gain != null) { gain.setLevel(level); } // aspectRatio = (float) (getVisualComponent().getPreferredSize().getWidth() / // getVisualComponent().getPreferredSize().getHeight()); // this takes care of detecting the EndOfMediaEvent player.addControllerListener(this); if (player.getVisualComponent() != null) { player.getVisualComponent().addMouseListener(new MouseHandler()); } // fpc = (FramePositioningControl) player.getControl("javax.media.control.FramePositioningControl"); popup = new JPopupMenu(); durationItem = new JMenuItem(ElanLocale.getString("Player.duration") + ": " + TimeFormatter.toString(getMediaDuration())); durationItem.setEnabled(false); infoItem = new JMenuItem(ElanLocale.getString("Player.Info")); infoItem.addActionListener(this); popup.addSeparator(); popup.add(infoItem); popup.add(durationItem); // popup.add("duration : " + ViewerTools.toString(getMediaDuration())).setFont(Constants.SMALLFONT); } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ public MediaDescriptor getMediaDescriptor() { return mediaDescriptor; } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ public String getFrameworkDescription() { return "Java Media Framework " + Manager.getVersion(); } /** * Restore the system volume level, JMF destroys it */ public void finalize() { if (player.getState() >= Player.Realized) { GainControl gain = player.getGainControl(); if (gain != null) { gain.setLevel(systemVolumeLevel); } } } /** * JMF controllerUpdate * * @param event DOCUMENT ME! */ public void controllerUpdate(javax.media.ControllerEvent event) { if (event instanceof EndOfMediaEvent) { // stops all the controllers and sets the media time stop(); } else if (event instanceof StopAtTimeEvent) { System.out.println("stopped by jmf itself"); frozenMediaTime = intervalStopTime; stop(); } } /** * Elan controllerUpdate Used to stop at the stop time in cooperation with * the playInterval method * * @param event DOCUMENT ME! */ public synchronized void controllerUpdate(ControllerEvent event) { if (event instanceof TimeEvent) { if (periodicController != null) { if (getMediaTime() >= intervalStopTime) { // freeze mediaTime frozenMediaTime = intervalStopTime; // remember the volume float volume = getVolume(); // mute the player, makes the acoustic behaviour more deterministic // because stop() does not stop instantaneously setVolume(0); // stop the player stop(); //restore the volume setVolume(volume); } } } } /** * play between two times. This method uses the contollerUpdate method to * detect if the stop time is passed. The setStopTime method of JMF can * not be used because it gives unstable behaviour * * @param startTime DOCUMENT ME! * @param stopTime DOCUMENT ME! */ public synchronized void playInterval(long startTime, long stopTime) { if ((player == null) || playingInterval || (stopTime <= startTime)) { return; } periodicController = new PeriodicUpdateController(25); periodicController.addControllerListener(this); addController(periodicController); intervalStopTime = stopTime + offset; setMediaTime(startTime); playingInterval = true; //player.setStopTime(new Time(1000000 * stopTime)); start(); } /** * Empty implementation for ElanMediaPlayer Interface * Only usefull for player that corectly supports setting stop time */ public void setStopTime(long stopTime) { } /** * Disable all code for interval playing */ private void stopPlayingInterval() { if (periodicController != null) { periodicController.removeControllerListener(this); removeController(periodicController); periodicController = null; } playingInterval = false; } /** * Gets the display Component for this Player. * * @return DOCUMENT ME! */ public java.awt.Component getVisualComponent() { if (player == null) { return null; } // if (noMouseComponent == null) { // noMouseComponent = new NoMouseComponent(); // noMouseComponent.add(player.getVisualComponent()); // } // return noMouseComponent; return player.getVisualComponent(); } /** * The only way to get to the dimension of the video image seems to be through the preferred * size ofthe visual component.(?) * * @see mpi.eudico.client.annotator.player.ElanMediaPlayer#getSourceHeight() */ public int getSourceHeight() { if (getVisualComponent() != null) { return (int) getVisualComponent().getPreferredSize().getHeight(); } return 0; } /** * The only way to get to the dimension of the video image seems to be through the preferred * size ofthe visual component.(?) * * @see mpi.eudico.client.annotator.player.ElanMediaPlayer#getSourceWidth() */ public int getSourceWidth() { if (getVisualComponent() != null) { return (int) getVisualComponent().getPreferredSize().getWidth(); } return 0; } /** * Gets the control Component for this Player. Necessary for CorexViewer. * A.K. * * @return DOCUMENT ME! */ public java.awt.Component getControlPanelComponent() { if (player == null) { return null; } return player.getControlPanelComponent();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -