de.muntjak.qtjplayer
Class QTJPlayer

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by de.muntjak.qtjplayer.QTJPlayer
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Externalizable, java.io.Serializable, javax.accessibility.Accessible

public class QTJPlayer
extends javax.swing.JPanel
implements java.io.Externalizable

QTJPlayer is a visual component to display and control QuickTime media (referred to as movies, though media can also be images, sounds or text files). It can be used as a simple media player or it can be integrated into a graphical UI. By default, the player provides controls to let the user define a play list, start, pause and stop movies, set volume, etc. You can hide the controls and control media from application code (or create controls of your own).

QTJPlayer consists of four components:


You can either specify a single media using setMovie(File) (this will load the media, then calling playMovie starts playing the media) or you can specify a play list. Play list related methods are:
A movie (or any other media) is displayed in a fixed-size display area (see setMoviePanelSize(Dimension)). If the movie is larger than the display area, it will be scaled to fit inside, if it is smaller than the display area, it will be centered.

Media related methods are: Note: Currently QTJPlayer does not support loading media from an (http) URL, this might be added in the future. Loading media from a jar URL is problematic because it must be decompressed into memory (or to disk) before it is ready to be played.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Field Summary
static int DOUBLE_SIZE
          Movie size constant: display media at double size
static int HALF_SIZE
          Movie size constant: display media at half size
static int NORMAL_SIZE
          Movie size constant: display media at normal size
 
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
QTJPlayer()
          Constructs the UI after checking for QuickTime for Java 6.1 or later being installed.
 
Method Summary
 void addMovie(java.io.File mediaFile)
          Appends the specified media to the end of the play list.
 void addMovie(int index, java.io.File mediaFile)
          Inserts the specified media in the play list at the specified index.
 void addMovieListener(MovieListener listener)
          Adds a MovieListener.
 void clearPlayList()
          Clears the play list after stopping the currently playing movie.
 int getMovieDuration()
          Returns the currently selected media's duration.
 java.awt.Dimension getMovieOriginalSize()
          Returns the currently selected media's original size or null if no media is currently selected.
 java.awt.Dimension getMoviePanelSize()
          Returns the size of the movie display area.
 int getMoviesSize()
          Returns all movies' current display size, one of QTJPlayer.HALF_SIZE, QTJPlayer.NORMAL_SIZE or QTJPlayer.DOUBLE_SIZE.
 int getMovieTime()
          Returns the selected media's current time.
 float getMovieVolume()
          Returns the current movie volume represented as a float value between 0.0 and 1.0.
 java.io.File[] getPlayList()
          Returns a copy of the play list as an array of File objects.
 int getPlayListSize()
          Returns the play list's current size.
 int getSelectedMovie()
          Returns the currently selected movie's index or -1 if the play list is empty.
 int getUnitsPerSecond()
          Returns the currently selected media's time scale.
 boolean isLoopMovie()
          Returns the current movie's loop state.
 boolean isLoopPlayList()
          Returns the play list's loop state.
 boolean isProcessKeyEvents()
          Returns true if key events are processed, false otherwise.
 boolean isShowMenuBar()
          Returns the showMenuBar property.
 boolean isShowMovieControllers()
          Returns whether movie controllers are currently displayed.
 boolean isShowPlayList()
          Returns whether the play list is currently displayed.
static void main(java.lang.String[] args)
          You can start QTJPlayer as a simple QickTime media player by executing
> java -jar qtjplayer.jar
from the command line.
 void pauseMovie()
          Pauses the currently playing media.
 void playMovie()
          Starts playing the media currently selected in the play list, respectively the first media of the play list if no play list entry is currently selected.
 void readExternal(java.io.ObjectInput in)
          Restores all public properties.
 void removeMovie(int index)
          Removes the specified entry from the play list.
 void removeMovieListener(MovieListener listener)
          Removes a MovieListener.
 void setBackground(java.awt.Color bg)
          Sets the background color of the menu bar, the movie panel and the play list panel.
 void setLoopMovie(boolean b)
          Enables or disables looping the currently playing / selected movie.
 void setLoopPlayList(boolean b)
          Sets the play list's loop state.
 void setMovie(java.io.File mediaFile)
          Loads the specified media so it is ready for playing.
 void setMoviePanelSize(java.awt.Dimension size)
          Sets the size of the movie display area.
 void setMoviesSize(int size)
          Sets the display size of all movies.
 void setMovieTime(int time)
          Sets the the currently selected media's new time.
 void setMovieVolume(float volume)
          Sets the movie volume to the specified value.
 void setPlayList(java.io.File[] mediaFiles)
          Sets a new play list from the given array.
 void setProcessKeyEvents(boolean b)
          Sets if this component should process key events if it has the keyboard focus.
 void setSelectedMovie(int index)
          Selects a movie specified by its index in the play list.
 void setShowMenuBar(boolean showMenuBar)
          Hides or shows the menubar dependent on the given flag.
 void setShowMovieControllers(boolean showControllers)
          Hides or shows the movie controllers.
 void setShowPlayList(boolean showPlayList)
          Hides or shows the play list dependent on the given flag.
 void showMovieInfo()
          Displays information about the currently selected movie (such as its frame rate and duration) using a non-modal dialog.
 void stopMovie()
          Stops the currently playing media.
 void writeExternal(java.io.ObjectOutput out)
          Writes all public properties.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DOUBLE_SIZE

public static final int DOUBLE_SIZE
Movie size constant: display media at double size

See Also:
Constant Field Values

HALF_SIZE

public static final int HALF_SIZE
Movie size constant: display media at half size

See Also:
Constant Field Values

NORMAL_SIZE

public static final int NORMAL_SIZE
Movie size constant: display media at normal size

See Also:
Constant Field Values
Constructor Detail

QTJPlayer

public QTJPlayer()
Constructs the UI after checking for QuickTime for Java 6.1 or later being installed.

Method Detail

addMovie

public void addMovie(java.io.File mediaFile)
Appends the specified media to the end of the play list. If some media is currently playing, it will be stopped and the specified media will start playing.

Parameters:
mediaFile - a media file

addMovie

public void addMovie(int index,
                     java.io.File mediaFile)
Inserts the specified media in the play list at the specified index. If some media is currently playing, it will be stopped and the specified media will start to play. If index is outside the play list range, it will be corrected so the specified media will either be appended to the play list or will be inserted at the first position.

Parameters:
index - an index into the play list
mediaFile - a media file

addMovieListener

public void addMovieListener(MovieListener listener)
Adds a MovieListener. The listener will receive events if a movie gets loaded, started, paused or stopped.

Parameters:
listener - the MovieListener to add

clearPlayList

public void clearPlayList()
Clears the play list after stopping the currently playing movie.


getMovieDuration

public int getMovieDuration()
Returns the currently selected media's duration. You can calculate the media's duration (in seconds) with getMovieDuration() / getUnitsPerSecond().

Returns:
the currently selected media's duration

getMovieOriginalSize

public java.awt.Dimension getMovieOriginalSize()
Returns the currently selected media's original size or null if no media is currently selected. For pure audio or midi, the returned Dimension will be of zero width and height.

Returns:
the currently selected media's original size or null if no media is currently selected

getMoviePanelSize

public java.awt.Dimension getMoviePanelSize()
Returns the size of the movie display area.

Returns:
the size of the movie display area
See Also:
setMoviePanelSize(Dimension)

getMoviesSize

public int getMoviesSize()
Returns all movies' current display size, one of

Returns:
all movies' current display size

getMovieTime

public int getMovieTime()
Returns the selected media's current time. Time measurement is the same for getMovieTime, getMovieDuration and getUnitsPerSecond.

Returns:
the selected media's current time
See Also:
getMovieDuration(), setMovieTime(int)

getMovieVolume

public float getMovieVolume()
Returns the current movie volume represented as a float value between 0.0 and 1.0.

Returns:
the current movie volume represented as a float value between 0.0 and 1.0

getPlayList

public java.io.File[] getPlayList()
Returns a copy of the play list as an array of File objects.

Returns:
a copy of the play list as an array of File objects

getPlayListSize

public int getPlayListSize()
Returns the play list's current size.

Returns:
the play list's current size

getSelectedMovie

public int getSelectedMovie()
Returns the currently selected movie's index or -1 if the play list is empty.

Returns:
the currently selected movie's index or -1 if the play list is empty
See Also:
setSelectedMovie(int)

getUnitsPerSecond

public int getUnitsPerSecond()
Returns the currently selected media's time scale. You can calculate the media's duration (in seconds) with getMovieDuration() / getUnitsPerSecond().

Returns:
the currently selected media's time scale

isLoopMovie

public boolean isLoopMovie()
Returns the current movie's loop state.

Returns:
the current movie's loop state

isLoopPlayList

public boolean isLoopPlayList()
Returns the play list's loop state.

Returns:
the play list's loop state

isProcessKeyEvents

public boolean isProcessKeyEvents()
Returns true if key events are processed, false otherwise.

Returns:
true if key events are processed, false otherwise
See Also:
setProcessKeyEvents(boolean)

isShowMenuBar

public boolean isShowMenuBar()
Returns the showMenuBar property.

Returns:
the showMenuBar property

isShowMovieControllers

public boolean isShowMovieControllers()
Returns whether movie controllers are currently displayed.

Returns:
true if the movie controllers are currently displayed, false otherwise

isShowPlayList

public boolean isShowPlayList()
Returns whether the play list is currently displayed.

Returns:
true if the play list is currently displayed. false otherwise

main

public static void main(java.lang.String[] args)
You can start QTJPlayer as a simple QickTime media player by executing
> java -jar qtjplayer.jar
from the command line.

Parameters:
args - ignored

pauseMovie

public void pauseMovie()
Pauses the currently playing media. Does nothing if no media is currently playing.

User gestures: By double-clicking inside the movie display area or by pressing Enter, media can be started and paused.


playMovie

public void playMovie()
Starts playing the media currently selected in the play list, respectively the first media of the play list if no play list entry is currently selected. If the play list is empty or a media is already playing, this method does nothing.

User gestures: By double-clicking inside the movie display area or by pressing Enter, media can be started and paused.


readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Restores all public properties.

Specified by:
readExternal in interface java.io.Externalizable
Throws:
java.io.IOException
java.lang.ClassNotFoundException

removeMovie

public void removeMovie(int index)
Removes the specified entry from the play list. Performs no action if argument is outside the play list range.

Parameters:
index - the play list index of the media to remove

removeMovieListener

public void removeMovieListener(MovieListener listener)
Removes a MovieListener.

Parameters:
listener - the MovieListener to remove

setBackground

public void setBackground(java.awt.Color bg)
Sets the background color of the menu bar, the movie panel and the play list panel.

Overrides:
setBackground in class javax.swing.JComponent

setLoopMovie

public void setLoopMovie(boolean b)
Enables or disables looping the currently playing / selected movie. The default value of this property is false.

Parameters:
b - the new value

setLoopPlayList

public void setLoopPlayList(boolean b)
Sets the play list's loop state. If true, the first entry of the play list will start playing as soon as the last entry has ended. The default value of this property is false.

Parameters:
b - the new loop state

setMovie

public void setMovie(java.io.File mediaFile)
Loads the specified media so it is ready for playing. This is simply a shortcut for specifying a play list of size 1. If argument is null, the play list is cleared. If some movie is currently playing, it is stopped and the specified movie starts playing.

Parameters:
mediaFile - a media file

setMoviePanelSize

public void setMoviePanelSize(java.awt.Dimension size)
Sets the size of the movie display area. Movies which are larger than the display area will be scaled to fit inside the display area. Movies which are smaller then the display area will be centered. The default size is Dimension(320, 240).

Parameters:
size - the new movie display area size

setMoviesSize

public void setMoviesSize(int size)
Sets the display size of all movies. The currently displayed movie will be scaled immediately.

Note: Because this also affects QTJPlayer's preferred size, the component might shrink or grow, dependent on the kind of layout it was added to.

Parameters:
size - one of
  • QTJPlayer.HALF_SIZE,
  • QTJPlayer.NORMAL_SIZE or
  • QTJPlayer.DOUBLE_SIZE
If argument is not one of those, QTJPlayer.NORMAL_SIZE will be selected

setMovieTime

public void setMovieTime(int time)
Sets the the currently selected media's new time. A value <= 0 positions the media at its beginning, a value >= getDuration() positions the media at the end.

Parameters:
time - the new time value
See Also:
getMovieTime(), getMovieDuration()

setMovieVolume

public void setMovieVolume(float volume)
Sets the movie volume to the specified value. Specify a value between 0.0 and 1.0 where 0f mutes audio.

Parameters:
volume - the new value

setPlayList

public void setPlayList(java.io.File[] mediaFiles)
Sets a new play list from the given array. If argument is an empty array or is null, the play list is cleared. If some movie is currently playing, it is stopped. The first entry of the new play list will be loaded and is ready to be played.

Parameters:
mediaFiles - array of File objects, which may be empty

setProcessKeyEvents

public void setProcessKeyEvents(boolean b)
Sets if this component should process key events if it has the keyboard focus. If set to true, the following key events will be processed:
  • Enter: play / pause media
  • Cursor_Right: Fast forward
  • Cursor_Left: Fast backward

The following key events will be processed only, if no menu bar is displayed (these are exactly the menu item accelerators):
  • Ctrl+O: Open play list
  • Ctrl+S: Save play list
  • Ctrl+0: Display media at half size
  • Ctrl+1: Display media at normal size
  • Ctrl+2: Display media at double size
  • Ctrl+L: Switch media loop mode
  • Ctrl+I: Show media info
  • F1: Display help information

This property defaults to true.

Note: If Beans.isDesignTime() returns true, QTJPlayer will ignore key presses and setting this property will have no effect.

Parameters:
b - if true, key events will be processed, if false, key events will be ignored

setSelectedMovie

public void setSelectedMovie(int index)
Selects a movie specified by its index in the play list. Does nothing if the play list is currently empty or the argument references the currently selected movie. Else, if a movie is currently playing, it will be stopped and the specified movie will start playing. If argument is not a valid play list index, it will be corrected, so either the first or the last movie is selected.

Parameters:
index - an index into the play list

setShowMenuBar

public void setShowMenuBar(boolean showMenuBar)
Hides or shows the menubar dependent on the given flag. The default value of this property is true.

Parameters:
showMenuBar - the new value

setShowMovieControllers

public void setShowMovieControllers(boolean showControllers)
Hides or shows the movie controllers. The default value of this property is true.

Parameters:
showControllers - the new value

setShowPlayList

public void setShowPlayList(boolean showPlayList)
Hides or shows the play list dependent on the given flag. The default value of this property is true.

Parameters:
showPlayList - the new value

showMovieInfo

public void showMovieInfo()
Displays information about the currently selected movie (such as its frame rate and duration) using a non-modal dialog.


stopMovie

public void stopMovie()
Stops the currently playing media. Does nothing if no media is currently playing.


writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Writes all public properties.

Specified by:
writeExternal in interface java.io.Externalizable
Throws:
java.io.IOException