eu.webtoolkit.jwt
Class WMediaPlayer

java.lang.Object
  extended by eu.webtoolkit.jwt.WObject
      extended by eu.webtoolkit.jwt.WWidget
          extended by eu.webtoolkit.jwt.WCompositeWidget
              extended by eu.webtoolkit.jwt.WMediaPlayer

public class WMediaPlayer
extends WCompositeWidget

A media player.

This widget implements a media player, suitable to play video or audio, and with a customizable user-interface.

To support cross-browser playing of video or audio content, you may need to provide the contents appropriately encoded. For audio, at least an MP3 or MP4 audio (M4A) encoding should be supplied, while for video the M4V encoding should be provided. Additional encodings are beneficial since they increase the chance that native HTML <video> or <audio> elements can be used (which may be hardware accelerated), instead of the flash player. See HTML5 browser media support.

You need to specify the encoding types you are going to use when instantiating the media player, since based on the chosen encodings, a particular suitable implementation will be used. Thus, you need to call addSource() immediately, but you may pass empty URLs if you do not yet want to load media.

The player provides a user-interface to control the playback which may be freely customized, and which is independent of the underlying media technology (HTML video or Flash player). The controls user-interface may be implemented as a JWt widget, where the controls (buttons, progress bars, and text widgets) are bound directly to the video player component (client-side).

This widget relies on a third-party JavaScript component jPlayer, which is distributed together with Wt.

The default user-interface can be themed using jPlayer themes. The theme is global (it applies to all media player instances), and is configured by loading a CSS stylesheet.

The following code creates a video using the default controls:

 ...
 
 

Alternatively, a custom widget may be set which implements the controls, using setControlsWidget(). In this case, you should add to this widget the buttons, text place holders, and progress bars and bind them to the media player using the setButton(), setText() and setProgressBar() methods. The controls widget is integrated in the media player, and this has as unique benefit (for a video player) that they may also be shown when the video player is maximized.

Finally, you may want to control the media player only through widgets external to the media player. This may be configured by setting null as controlsWidget. In this case however, full screen mode should not be used since there is no way to restore the original size.


Nested Class Summary
static class WMediaPlayer.BarControlId
          An enumeration for a progressbar function.
static class WMediaPlayer.ButtonControlId
          An enumeration for a button function.
static class WMediaPlayer.Encoding
          An enumeration for a media encoding.
static class WMediaPlayer.MediaType
          An enumeration for a media type.
static class WMediaPlayer.ReadyState
          An enumeration for the player state.
static class WMediaPlayer.TextId
          An enumeration for a text.
 
Constructor Summary
WMediaPlayer(WMediaPlayer.MediaType mediaType)
          Creates a new media player.
WMediaPlayer(WMediaPlayer.MediaType mediaType, WContainerWidget parent)
          Creates a new media player.
 
Method Summary
 void addSource(WMediaPlayer.Encoding encoding, WLink link)
          Adds a source.
 void clearSources()
          Clears all sources.
 JSignal ended()
          Event that indicates that the video or audio has ended.
 WInteractWidget getButton(WMediaPlayer.ButtonControlId id)
          Returns a control button.
 WWidget getControlsWidget()
          Returns the user-interface controls widget.
 double getCurrentTime()
          Returns the current playback time.
 double getDuration()
          Returns the duration.
 double getPlaybackRate()
          Returns the current playback rate.
 WProgressBar getProgressBar(WMediaPlayer.BarControlId id)
          Returns a control progress bar.
 WMediaPlayer.ReadyState getReadyState()
          Returns the current player state.
 WLink getSource(WMediaPlayer.Encoding encoding)
          Returns a source.
 WText getText(WMediaPlayer.TextId id)
          Returns a text place-holder widget.
 int getVideoHeight()
          Returns the video height.
 int getVideoWidth()
          Returns the video width.
 boolean isPlaying()
          Returns whether the media is currently playing.
 void mute(boolean mute)
          Mutes or unmutes the playback volume.
 void pause()
          Pauses the player.
 void play()
          Start or resume playing.
 JSignal playbackPaused()
          Event that indicates that playback paused.
 JSignal playbackStarted()
          Event that indicates that playback started.
 void remove()
          Destructor.
 void seek(double time)
          Seeks to a time.
 void setButton(WMediaPlayer.ButtonControlId id, WInteractWidget w)
          Binds a control button.
 void setControlsWidget(WWidget controlsWidget)
          Sets the user-interface controls widget.
 void setPlaybackRate(double rate)
          Sets the playback rate.
 void setProgressBar(WMediaPlayer.BarControlId id, WProgressBar w)
          Binds a control progress bar.
 void setText(WMediaPlayer.TextId id, WText w)
          Sets a text place-holder widget.
 void setTitle(java.lang.CharSequence title)
          Sets the media title.
 void setVideoSize(int width, int height)
          Sets the video size.
 void setVolume(double volume)
          Sets the volume.
 void stop()
          Stops the player.
 JSignal timeUpdated()
          Event that indicates a time update.
 JSignal volumeChanged()
          Event that indicates that the volume has changed.
 
Methods inherited from class eu.webtoolkit.jwt.WCompositeWidget
addStyleClass, boxBorder, boxPadding, callJavaScriptMember, doJavaScript, enableAjax, find, getAttributeValue, getClearSides, getDecorationStyle, getFloatSide, getHeight, getId, getImplementation, getJavaScriptMember, getLineHeight, getMargin, getMaximumHeight, getMaximumWidth, getMinimumHeight, getMinimumWidth, getOffset, getPositionScheme, getStyleClass, getTabIndex, getToolTip, getVerticalAlignment, getVerticalAlignmentLength, getWidth, isDisabled, isEnabled, isHidden, isHiddenKeepsGeometry, isInline, isLoaded, isPopup, isVisible, load, propagateSetEnabled, refresh, removeStyleClass, resize, setAttributeValue, setClearSides, setDecorationStyle, setDisabled, setFloatSide, setHidden, setHiddenKeepsGeometry, setId, setImplementation, setInline, setJavaScriptMember, setLineHeight, setMargin, setMaximumSize, setMinimumSize, setOffsets, setPopup, setPositionScheme, setSelectable, setStyleClass, setTabIndex, setToolTip, setVerticalAlignment
 
Methods inherited from class eu.webtoolkit.jwt.WWidget
acceptDrops, acceptDrops, addCssRule, addCssRule, addStyleClass, animateHide, animateShow, containsExposed, disable, dropEvent, enable, getDrop, getJsRef, getParent, hide, htmlText, isLayoutSizeAware, isRendered, layoutSizeChanged, positionAt, positionAt, removeStyleClass, resize, setClearSides, setHeight, setHidden, setLayoutSizeAware, setMargin, setMargin, setMargin, setMargin, setMargin, setOffsets, setOffsets, setOffsets, setOffsets, setOffsets, setToolTip, setVerticalAlignment, setWidth, show, stopAcceptDrops, toggleStyleClass, toggleStyleClass, tr
 
Methods inherited from class eu.webtoolkit.jwt.WObject
addChild, getObjectName, setObjectName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WMediaPlayer

public WMediaPlayer(WMediaPlayer.MediaType mediaType,
                    WContainerWidget parent)
Creates a new media player.

The player is instantiated with default controls.

See Also:
setControlsWidget(WWidget controlsWidget)

WMediaPlayer

public WMediaPlayer(WMediaPlayer.MediaType mediaType)
Creates a new media player.

Calls this(mediaType, (WContainerWidget)null)

Method Detail

remove

public void remove()
Destructor.

Overrides:
remove in class WCompositeWidget
See Also:
WContainerWidget.removeWidget(WWidget widget)

setVideoSize

public void setVideoSize(int width,
                         int height)
Sets the video size.

This sets the size for the video. The actual size of the media player may be slightly larger, if the controlWidget take additional space (i.e. is not overlayed on top of the video).

CSS Themes for the default jPlayer controls support two formats (480 x 270 and 640 x 360).

The default video size is 480 x 270.


getVideoWidth

public int getVideoWidth()
Returns the video width.

See Also:
setVideoSize(int width, int height)

getVideoHeight

public int getVideoHeight()
Returns the video height.

See Also:
setVideoSize(int width, int height)

setControlsWidget

public void setControlsWidget(WWidget controlsWidget)
Sets the user-interface controls widget.

This sets a widget that contains the controls (buttons, text widgets, etc...) to allow the user to control the player.

Widgets that implement the buttons, bars, and text holders should be bound to the player using setButton(), setText() and setProgressBar() calls.

Setting a null widget will result in a player without controls. For an audio player this has the effect of being entirely invisible.

The default controls widget is a widget that can be styled using a jPlayer CSS theme.


getControlsWidget

public WWidget getControlsWidget()
Returns the user-interface controls widget.

See Also:
setControlsWidget(WWidget controlsWidget)

setTitle

public void setTitle(java.lang.CharSequence title)
Sets the media title.

See Also:
WMediaPlayer.TextId.Title

addSource

public void addSource(WMediaPlayer.Encoding encoding,
                      WLink link)
Adds a source.

Adds a media source. The source may be specified as a URL or as a dynamic resource.

You may pass a null link if you want to indicate the media types you will use (later) without already loading data.


getSource

public WLink getSource(WMediaPlayer.Encoding encoding)
Returns a source.

Returns the media source for the given encoding, which must have previously been added using addSource().


clearSources

public void clearSources()
Clears all sources.

See Also:
addSource(WMediaPlayer.Encoding encoding, WLink link)

setButton

public void setButton(WMediaPlayer.ButtonControlId id,
                      WInteractWidget w)
Binds a control button.

A control button is typically implemented as a WAnchor or a WPushButton (although any WInteractWidget can work).

You should use this method in conjunction with setControlsWidget() to bind buttons in a custom control interface to media player functions.

The default control widget implements all buttons using a WAnchor.


getButton

public WInteractWidget getButton(WMediaPlayer.ButtonControlId id)
Returns a control button.

See Also:
setButton(WMediaPlayer.ButtonControlId id, WInteractWidget w)

setProgressBar

public void setProgressBar(WMediaPlayer.BarControlId id,
                           WProgressBar w)
Binds a control progress bar.

The progress bar for the WMediaPlayer.BarControlId.Time indication should be contained in a WContainerWidget which bounds the width of the progress bar, rather than setting a width on the progress bar. This is because the progress bar may, in some cases, also be used to indicate which part of the media can be seeked, and for this its width is being manipulated.

You should use this method in conjunction with setControlsWidget() to bind progress bars in a custom control interface to media player functions.


getProgressBar

public WProgressBar getProgressBar(WMediaPlayer.BarControlId id)
Returns a control progress bar.

See Also:
setProgressBar(WMediaPlayer.BarControlId id, WProgressBar w)

setText

public void setText(WMediaPlayer.TextId id,
                    WText w)
Sets a text place-holder widget.

This binds the widget that displays text such as current time and total duration of the loaded media.

You should use this method in conjunction with setControlsWidget() to bind progress bars in a custom control interface to media player functions.


getText

public WText getText(WMediaPlayer.TextId id)
Returns a text place-holder widget.

See Also:
setText(WMediaPlayer.TextId id, WText w)

pause

public void pause()
Pauses the player.

See Also:
play()

play

public void play()
Start or resume playing.

The player starts or resumes playing at the current time.

See Also:
seek(double time)

stop

public void stop()
Stops the player.

See Also:
play()

seek

public void seek(double time)
Seeks to a time.

If possible, the player sets the current time to the indicated time (expressed in seconds).

Note: It may be the case that this only works after the player has already loaded the media.


setPlaybackRate

public void setPlaybackRate(double rate)
Sets the playback rate.

This modifies the playback rate, expressed as a ratio of the normal (natural) playback rate.

The default value is 1.0

Note: Not all browsers support this function.


setVolume

public void setVolume(double volume)
Sets the volume.

This modifies the volume, which must be a number between 0 and 1.0.

The default value is 0.8


mute

public void mute(boolean mute)
Mutes or unmutes the playback volume.

See Also:
setVolume(double volume)

isPlaying

public boolean isPlaying()
Returns whether the media is currently playing.

See Also:
play()

getReadyState

public WMediaPlayer.ReadyState getReadyState()
Returns the current player state.

The state reflects in how far the media player has loaded the media, and has determined its characteristics.


getDuration

public double getDuration()
Returns the duration.

The duration may be reported as 0 if the player has not yet loaded the media to determine the duration. Otherwise the duration is the duration of the loaded media, expressed in seconds.

See Also:
getReadyState(), getCurrentTime()

getCurrentTime

public double getCurrentTime()
Returns the current playback time.

Returns the current playback time, expressed in seconds.

See Also:
seek(double time)

getPlaybackRate

public double getPlaybackRate()
Returns the current playback rate.

See Also:
setPlaybackRate(double rate)

timeUpdated

public JSignal timeUpdated()
Event that indicates a time update.

The event indicates that the getCurrentTime() has changed.


playbackStarted

public JSignal playbackStarted()
Event that indicates that playback started.

The event is fired when playback has started (or is being continued).


playbackPaused

public JSignal playbackPaused()
Event that indicates that playback paused.

The event is fired when playback has been paused.


ended

public JSignal ended()
Event that indicates that the video or audio has ended.


volumeChanged

public JSignal volumeChanged()
Event that indicates that the volume has changed.