Class WMediaPlayer
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 JWt.
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
Nested classes/interfaces inherited from class eu.webtoolkit.jwt.WObject
WObject.FormData -
Constructor Summary
ConstructorsConstructorDescriptionWMediaPlayer(MediaType mediaType) Creates a new media player.WMediaPlayer(MediaType mediaType, WContainerWidget parentContainer) Creates a new media player. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddSource(MediaEncoding encoding, WLink link) Adds a source.voidClears all sources.ended()Event that indicates that the video or audio has ended.Returns a control button.Returns the user-interface controls widget.doubleReturns the current playback time.doubleReturns the duration.doubleReturns the current playback rate.Returns a control progress bar.Returns the current player state.getSource(MediaEncoding encoding) Returns a source.Returns a text place-holder widget.intReturns the video height.intReturns the video width.doubleReturns the volume.booleanReturns whether the media is currently playing.voidmute(boolean mute) Mutes or unmutes the playback volume.voidpause()Pauses the player.voidplay()Start or resume playing.Event that indicates that playback paused.Event that indicates that playback started.voidrefresh()Refresh the widget.voidremove()Destructor.protected voidrender(EnumSet<RenderFlag> flags) Renders the widget.voidseek(double time) Seeks to a time.voidBinds a control button.voidsetControlsWidget(WWidget controlsWidget) Sets the user-interface controls widget.protected voidsetFormData(WObject.FormData formData) voidsetPlaybackRate(double rate) Sets the playback rate.voidBinds a control progress bar.voidsetText(MediaPlayerTextId id, WText w) Sets a text place-holder widget.voidsetTitle(CharSequence title) Sets the media title.voidsetVideoSize(int width, int height) Sets the video size.voidsetVolume(double volume) Sets the volume.voidstop()Stops the player.Event that indicates a time update.Event that indicates that the volume has changed.Methods inherited from class eu.webtoolkit.jwt.WCompositeWidget
addStyleClass, boxBorder, boxPadding, callJavaScriptMember, doJavaScript, enableAjax, find, findById, getAttributeValue, getBaseZIndex, getChildren, getClearSides, getDecorationStyle, getFloatSide, getHeight, getId, getImplementation, getJavaScriptMember, getLineHeight, getMargin, getMaximumHeight, getMaximumWidth, getMinimumHeight, getMinimumWidth, getObjectName, getOffset, getPositionScheme, getScrollVisibilityMargin, getStyleClass, getTabIndex, getTakeImplementation, getToolTip, getVerticalAlignment, getVerticalAlignmentLength, getWidth, hasFocus, hasStyleClass, isCanReceiveFocus, isDisabled, isEnabled, isHidden, isHiddenKeepsGeometry, isInline, isLoaded, isPopup, isScrollVisibilityEnabled, isScrollVisible, isSetFirstFocus, isThemeStyleEnabled, isVisible, load, propagateSetEnabled, propagateSetVisible, removeStyleClass, removeWidget, resize, scrollVisibilityChanged, setAttributeValue, setCanReceiveFocus, setClearSides, setDecorationStyle, setDeferredToolTip, setDisabled, setFloatSide, setFocus, setHidden, setHiddenKeepsGeometry, setId, setImplementation, setInline, setJavaScriptMember, setLineHeight, setMargin, setMaximumSize, setMinimumSize, setObjectName, setOffsets, setParentWidget, setPopup, setPositionScheme, setScrollVisibilityEnabled, setScrollVisibilityMargin, setSelectable, setStyleClass, setTabIndex, setThemeStyleEnabled, setToolTip, setVerticalAlignmentMethods inherited from class eu.webtoolkit.jwt.WWidget
acceptDrops, acceptDrops, addCssRule, addCssRule, addJSignal, addStyleClass, animateHide, animateShow, applyThemeStyles, createJavaScript, disable, dropEvent, enable, getDropTouch, getJsRef, getParent, hide, htmlText, isExposed, isGlobalWidget, isLayoutSizeAware, isRendered, layoutSizeChanged, needsRerender, positionAt, positionAt, positionAt, positionAt, removeFromParent, removeStyleClass, render, resize, scheduleRender, scheduleRender, scheduleRender, scheduleThemeStyleApply, setClearSides, setDeferredToolTip, setFocus, setHeight, setHidden, setLayoutSizeAware, setMargin, setMargin, setMargin, setMargin, setMargin, setOffsets, setOffsets, setOffsets, setOffsets, setOffsets, setToolTip, setVerticalAlignment, setWidth, show, stopAcceptDrops, toggleStyleClass, toggleStyleClass, tr
-
Constructor Details
-
WMediaPlayer
Creates a new media player.The player is instantiated with default controls.
-
WMediaPlayer
Creates a new media player.
-
-
Method Details
-
remove
public void remove()Destructor.- Overrides:
removein classWCompositeWidget- See Also:
-
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:
-
getVideoHeight
public int getVideoHeight()Returns the video height.- See Also:
-
setControlsWidget
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()andsetProgressBar()calls.Setting a
nullwidget 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
Returns the user-interface controls widget. -
setTitle
Sets the media title.- See Also:
-
addSource
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
linkif you want to indicate the media types you will use (later) without already loading data. -
getSource
Returns a source.Returns the media source for the given
encoding, which must have previously been added usingaddSource(). -
clearSources
public void clearSources()Clears all sources. -
setButton
Binds a control button.A control button is typically implemented as a
WAnchoror aWPushButton(although anyWInteractWidgetcan 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
Returns a control button. -
setProgressBar
Binds a control progress bar.The progress bar for the
MediaPlayerProgressBarId.Timeindication should be contained in aWContainerWidgetwhich 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
Returns a control progress bar. -
setText
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
Returns a text place-holder widget.- See Also:
-
pause
public void pause()Pauses the player.- See Also:
-
play
public void play()Start or resume playing.The player starts or resumes playing at the current time.
- See Also:
-
stop
public void stop()Stops the player.- See Also:
-
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
-
getVolume
public double getVolume()Returns the volume.- See Also:
-
mute
public void mute(boolean mute) Mutes or unmutes the playback volume.- See Also:
-
isPlaying
public boolean isPlaying()Returns whether the media is currently playing.- See Also:
-
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:
-
getCurrentTime
public double getCurrentTime()Returns the current playback time.Returns the current playback time, expressed in seconds.
- See Also:
-
getPlaybackRate
public double getPlaybackRate()Returns the current playback rate.- See Also:
-
timeUpdated
Event that indicates a time update.The event indicates that the
getCurrentTime()has changed. -
playbackStarted
Event that indicates that playback started.The event is fired when playback has started (or is being continued).
-
playbackPaused
Event that indicates that playback paused.The event is fired when playback has been paused.
-
ended
Event that indicates that the video or audio has ended. -
volumeChanged
Event that indicates that the volume has changed. -
refresh
public void refresh()Description copied from class:WWidgetRefresh the widget.The refresh method is invoked when the locale is changed using
WApplication#setLocale()or when the user hit the refresh button.The widget must actualize its contents in response.
Note: This does not rerender the widget! Calling
refresh()usually does not have any effect (unless you've reimplementedrefresh()to attach to it an effect).- Overrides:
refreshin classWCompositeWidget
-
setFormData
- Overrides:
setFormDatain classWObject
-
render
Description copied from class:WWidgetRenders the widget.This function renders the widget (or an update for the widget), after this has been scheduled using
scheduleRender().The default implementation will render the widget by serializing changes to JavaScript and HTML. You may want to reimplement this widget if you have been postponing some of the layout / rendering implementation until the latest moment possible. In that case you should make sure you call the base implementation however.
- Overrides:
renderin classWCompositeWidget
-