Class 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 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.

  • Constructor Details

  • Method Details

    • remove

      public void remove()
      Destructor.
      Overrides:
      remove in class WCompositeWidget
      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

      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:
    • setTitle

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

      See Also:
    • addSource

      public void addSource(MediaEncoding 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(MediaEncoding 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:
    • setButton

      public void setButton(MediaPlayerButtonId 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(MediaPlayerButtonId id)
      Returns a control button.

      See Also:
    • setProgressBar

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

      The progress bar for the MediaPlayerProgressBarId.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(MediaPlayerProgressBarId id)
      Returns a control progress bar.

      See Also:
    • setText

      public void setText(MediaPlayerTextId 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(MediaPlayerTextId id)
      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

      public MediaReadyState 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

      public JSignal1<Double> 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 JSignal1<Double> volumeChanged()
      Event that indicates that the volume has changed.
    • refresh

      public void refresh()
      Description copied from class: WWidget
      Refresh 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 reimplemented refresh() to attach to it an effect).

      Overrides:
      refresh in class WCompositeWidget
    • setFormData

      protected void setFormData(WObject.FormData formData)
      Overrides:
      setFormData in class WObject
    • render

      protected void render(EnumSet<RenderFlag> flags)
      Description copied from class: WWidget
      Renders 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:
      render in class WCompositeWidget