de.enough.polish.video
Class VideoContainer

java.lang.Object
  extended by de.enough.polish.ui.Item
      extended by de.enough.polish.ui.Container
          extended by de.enough.polish.video.VideoContainer
All Implemented Interfaces:
Animatable, UiElement, VideoCallback, Runnable, PlayerListener

public class VideoContainer
extends Container
implements Runnable, PlayerListener, VideoCallback

A Container to play a video in its content area. Usually a VideoContainer is filled with items and appended to a Screen or Container and a controlling instance is set as the callback for the VideoContainer. The VideoSource is set via setSource(). setRatio(), setRepeat() etc. are optional. To initialize the player, the dimensions etc. for the video, prepare() is called. When the Container is fully initialized (displayed on the screen) and the player and dimensions are set, the listener is notified via onVideoReady() and the video can be played.

Author:
Andre Schmidt

Nested Class Summary
static class VideoContainer.Ratio
          The ratio to use (i.e.
 class VideoContainer.Seek
           
static class VideoContainer.Test
          Static methods to test videoplayback
 
Field Summary
static int STATE_CLOSED
          The video is stopped
static int STATE_NOT_PREPARED
          The video is not yet prepared
static int STATE_PAUSED
          The video is paused
static int STATE_PLAYING
          The video is playing
static int STATE_READY
          The video is fully prepared
static int STATE_STOPPED
          The video is stopped
 
Fields inherited from class de.enough.polish.ui.Container
allowCycling, autoFocusEnabled, autoFocusIndex, containerView, enableScrolling, focusAllStyle, focusedIndex, focusedItem, focusedStyleFirst, focusedStyleLast, isExpandItems, itemsList, itemStyle, lastPointerPressTime, lastPointerPressY, lastPointerPressYOffset, plainStyle, SCROLL_DEFAULT, SCROLL_SMOOTH, scrollHeight, scrollSmooth, targetYOffset, yOffset
 
Fields inherited from class de.enough.polish.ui.Item
_androidView, _bbField, appearanceMode, availableHeight, availableWidth, availContentHeight, availContentWidth, background, backgroundHeight, backgroundWidth, backgroundYOffset, bgBorder, border, BUTTON, cacheItemImage, colSpan, commands, completeBackground, completeBackgroundPadding, completeBorder, contentHeight, contentWidth, contentX, contentXAdjustment, contentY, contentYAdjustment, cssHeight, cssSelector, cssWidth, defaultCommand, focusedStyle, HORIZONTAL, HYPERLINK, ignoreRepaintRequests, includeLabel, INTERACTIVE, internalHeight, internalWidth, internalX, internalY, isContentVisible, isFocused, isInlineLabel, isInvisible, isJustFocused, isLayoutCenter, isLayoutExpand, isLayoutRight, isPressed, isShown, isStyleInitialized, itemCommandListener, itemHeight, itemWidth, label, labelStyle, landscapeStyle, layout, LAYOUT_2, LAYOUT_BOTTOM, LAYOUT_CENTER, LAYOUT_DEFAULT, LAYOUT_EXPAND, LAYOUT_LEFT, LAYOUT_NEWLINE_AFTER, LAYOUT_NEWLINE_BEFORE, LAYOUT_RIGHT, LAYOUT_SHRINK, LAYOUT_TOP, LAYOUT_VCENTER, LAYOUT_VEXPAND, LAYOUT_VSHRINK, marginBottom, marginLeft, marginRight, marginTop, maximumHeight, maximumItemHeight, maximumItemWidth, maximumWidth, minimumHeight, minimumItemHeight, minimumItemWidth, minimumWidth, nativeItem, NO_POSITION_SET, opacity, opacityPaintNormally, opacityRgbData, paddingBottom, paddingHorizontal, paddingLeft, paddingRight, paddingTop, paddingVertical, parent, PLAIN, portraitStyle, preferredHeight, preferredWidth, preserveViewType, relativeX, relativeY, rowSpan, screen, setView, style, TRANSPARENT, useSingleRow, VERTICAL, view, xAdjustment, yAdjustment
 
Fields inherited from interface javax.microedition.media.PlayerListener
CLOSED, DEVICE_AVAILABLE, DEVICE_UNAVAILABLE, DURATION_UPDATED, END_OF_MEDIA, ERROR, STARTED, STOPPED, VOLUME_CHANGED
 
Constructor Summary
VideoContainer()
          Constructs a new VideoContainer instance
VideoContainer(boolean adjustSizeAutomatically)
          Constructs a new VideoContainer instance
VideoContainer(boolean adjustSizeAutomatically, Style style)
          Constructs a new VideoContainer
VideoContainer(Style style)
          Constructs a new VideoContainer instance with the given screen as parent
 
Method Summary
 void addCallback(VideoCallback callback)
          Adds a callback
 boolean animate()
          Animates this item.
 void animate(long currentTime, ClippingRegion repaintRegion)
          Animates this item.
 byte[] capture(String encoding)
          Captures a snapshot when a CaptureSource is used.
 void close()
          Closes this VideoContainer.
 Exception getLastException()
          Retrieves the last exception that occurred, if any
 long getLength()
          Returns the total length of the video
 VideoSource getSource()
          Returns the current VideoSource
 int getState()
          Returns the current state of the VideoContainer
 long getTime()
          Returns the current time of the playback
 int getVolume()
          Returns the volume
protected  void hideNotify()
          Called by the system to notify the item that it is now completely invisible, when it previously had been at least partially visible.
protected  void initContent(int firstLineWidth, int availWidth, int availHeight)
          Initialises this item.
protected  void initDisplay(VideoSource src)
          Initializes the display by using the content area of the Container.
protected  void initSource(VideoSource src)
          Initializes the source by calling open() on it
protected  void initVolume(VideoSource source)
          Initializes the volume
 boolean isFullscreen()
          Returns true if the video is played in fullscreen mode
 boolean isLandscape()
          Returns true if the video should be played in landscape mode
 boolean isMute()
           
 boolean isRepeat()
          Returns true if the video should be repeated
 void mute(boolean mute)
           
 void next()
           
 void onSnapshot(byte[] data, String encoding)
          Called when a capture is done
 void onVideoClose()
          Called when a video is destroyed
 void onVideoError(Exception e)
          Called when an error occures
 void onVideoPause()
          Called when the video is paused
 void onVideoPlay()
          Called when the video is played
 void onVideoReady()
          Called when a video is fully prepared
 void onVideoStop()
          Called when the video is stopped
 void paint(int x, int y, int leftBorder, int rightBorder, Graphics g)
          Paints this item on the screen.
 void pause()
          Pauses the video
 void play()
          Plays the video or shows the snapshot preview window.
 void playerUpdate(Player player, String event, Object data)
           
 void prepare()
          Starts the thread to prepare the video if the state is zero
 void removeCallback(VideoCallback callback)
          Removes a callback
 void run()
          Prepares the video by creating the player, initializing the display and controls and notifying the listeners when its done.
 void seek(long position)
          Set the player to the given position.
protected  void setDisplay(VideoControl control, int x, int y, int width, int height, VideoContainer.Ratio ratio)
          Set the display by using the given VideoControl instance and the dimensions given
 void setEnterFullscreenGameAction(int gameAction)
          Sets a shortcut
 void setFullscreen(boolean fullscreen)
          Enters or leaves the fullscreen mode.
 void setFullscreen(VideoSource src, boolean fullscreen)
          Enters or leaves the fullscreen mode.
 void setLandscape(boolean landscape)
          Sets the landscape mode
 void setMultipart(VideoMultipart multipart)
          ] Sets a VideoMultipart as the source.
 void setPseudoFullscreen(boolean fullscreen)
          Enters or leaves the fullscreen mode by using another styleable form
 void setPseudoFullscreen(VideoSource src, boolean fullscreen)
          Enters or leaves the fullscreen mode by using another styleable form
 void setRatio(VideoContainer.Ratio ratio)
          Sets the ratio to playback the video
 void setRepeat(boolean repeat)
          Sets the repeat mode
 void setScreen(Screen screen)
          Convenience method to set the screen if the VideoContainer is not added to a Screen or a Container.
 void setSource(VideoSource source)
          Sets the VideoSource of this VideoContainer.
 void setVolume(int volume)
          Sets the volume
protected  void showNotify()
          Called by the system to notify the item that it is now at least partially visible, when it previously had been completely invisible.
 void stop()
          Stops the video
 
Methods inherited from class de.enough.polish.ui.Container
add, add, add, add, add, add, add, addRepaintArea, changeChildStyles, changeChildStyles, clear, contains, createCssSelector, defocus, destroy, fireEvent, focus, focusChild, focusChild, focusClosestItem, focusClosestItemAbove, get, getChildAt, getChildAtImpl, getChildHeight, getChildWidth, getCurrentScrollYOffset, getFocusedChild, getFocusedIndex, getFocusedItem, getFocusedStyle, getFocusListener, getInternalArray, getItemAreaHeight, getItemAt, getItems, getNumberOfInteractiveItems, getPosition, getRelativeScrollYOffset, getScrollHeight, getScrollYOffset, getSynchronizationLock, getView, getView, handleCommand, handleKeyPressed, handleKeyReleased, handleKeyRepeated, handleNavigate, handleOnFocusSoftKeyboardDisplayBehavior, handlePointerDragged, handlePointerDragged, handlePointerPressed, handlePointerReleased, handlePointerScrollReleased, handlePointerTouchDown, handlePointerTouchUp, indexOf, initLayout, initMargin, isInItemArea, isItemVisible, isScrolling, isVerticalLayout, notifyItemPressedEnd, notifyItemPressedStart, onScreenSizeChanged, paintBackgroundAndBorder, paintContent, parseIndexUrl, releaseResources, remove, remove, requestDefocus, requestFullInit, resetLastPointerPressYOffset, resetStyle, scroll, scroll, scroll, scrollToBottom, scrollToTop, set, set, set, setAppearanceMode, setAutoFocusEnabled, setContentHeight, setContentWidth, setFocusListener, setItemsList, setItemWidth, setScrollHeight, setScrollYOffset, setScrollYOffset, setStyle, setStyle, setStyleWithBackground, setView, showCommands, size, startScroll, toString, updateInternalPosition
 
Methods inherited from class de.enough.polish.ui.Item
addCommand, addCommand, addCommand, addCommands, addRelativeToBackgroundRegion, addRelativeToBackgroundRegion, addRelativeToContentRegion, containsCommand, fireContinueCycle, getAbsoluteX, getAbsoluteY, getAfterWidthWithPadding, getAppearanceMode, getAttribute, getAttributes, getAvailableContentHeight, getAvailableContentWidth, getAvailableHeight, getAvailableWidth, getBackgroundHeight, getBackgroundWidth, getBackgroundX, getBackgroundY, getBeforeWidthWithPadding, getBorderWidthBottom, getBorderWidthLeft, getBorderWidthRight, getBorderWidthTop, getContentHeight, getContentWidth, getContentX, getContentY, getCycleListener, getDefaultCommand, getFocusedStyle, getInternalHeight, getInternalWidth, getInternalX, getInternalY, getItemCommandListener, getItemCommands, getItemHeight, getItemHeight, getItemStateListener, getItemWidth, getItemWidth, getLabel, getLabelItem, getLayout, getMarginBottom, getMarginLeft, getMarginRight, getMarginTop, getMaximumHeight, getMaximumHeight, getMaximumWidth, getMaximumWidth, getMinimumHeight, getMinimumHeight, getMinimumWidth, getMinimumWidth, getNativeItem, getPaddingBottom, getPaddingHorizontal, getPaddingLeft, getPaddingRight, getPaddingTop, getPaddingVertical, getParent, getParentRoot, getpreferredHeight, getpreferredWidth, getRgbData, getRgbData, getScreen, getStyle, getUiEventListener, handleCommand, handleGesture, handleGestureHold, handleGestureSwipeLeft, handleGestureSwipeRight, init, initPadding, initStyle, isFocused, isInContentArea, isInContentWithPaddingArea, isInItemArea, isInitialized, isInteractive, isLayoutBottom, isLayoutCenter, isLayoutExpand, isLayoutLeft, isLayoutNewlineAfter, isLayoutNewlineBefore, isLayoutRight, isLayoutShrink, isLayoutTop, isLayoutVerticalCenter, isLayoutVerticalExpand, isLayoutVerticalShrink, isPressed, isSameRowForContentAndLabel, isVisible, notifyStateChanged, notifyUnvisited, notifyValueChanged, notifyVisited, paintBackground, paintBorder, paintCommands, paintFilter, removeAttribute, removeCommand, repaint, repaint, repaintFully, requestInit, setAbsoluteY, setAttribute, setBackground, setBorder, setCycleListener, setDefaultCommand, setDefaultCommand, setInitialized, setItemCommandListener, setItemCommandListener, setItemHeight, setItemStateListener, setItemTransition, setLabel, setLayout, setNativeItem, setParent, setParent, setpreferredSize, setSameRowForContentAndLabel, setStyle, setUiEventListener, setVisible, show, show, toImage, toRgbImage, updateInternalArea
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

STATE_CLOSED

public static final int STATE_CLOSED
The video is stopped

See Also:
Constant Field Values

STATE_NOT_PREPARED

public static final int STATE_NOT_PREPARED
The video is not yet prepared

See Also:
Constant Field Values

STATE_READY

public static final int STATE_READY
The video is fully prepared

See Also:
Constant Field Values

STATE_PLAYING

public static final int STATE_PLAYING
The video is playing

See Also:
Constant Field Values

STATE_PAUSED

public static final int STATE_PAUSED
The video is paused

See Also:
Constant Field Values

STATE_STOPPED

public static final int STATE_STOPPED
The video is stopped

See Also:
Constant Field Values
Constructor Detail

VideoContainer

public VideoContainer()
Constructs a new VideoContainer instance


VideoContainer

public VideoContainer(Style style)
Constructs a new VideoContainer instance with the given screen as parent


VideoContainer

public VideoContainer(boolean adjustSizeAutomatically)
Constructs a new VideoContainer instance

Parameters:
adjustSizeAutomatically - true when the size of this container should be adjusted according to the video source

VideoContainer

public VideoContainer(boolean adjustSizeAutomatically,
                      Style style)
Constructs a new VideoContainer

Parameters:
adjustSizeAutomatically - true when the size of this container should be adjusted according to the video source
style - the style
Method Detail

animate

public void animate(long currentTime,
                    ClippingRegion repaintRegion)
Description copied from class: Item
Animates this item. Subclasses can override this method to create animations. The default implementation animates the background and the item view if present.

Specified by:
animate in interface Animatable
Overrides:
animate in class Container
Parameters:
currentTime - the current time in milliseconds
repaintRegion - the repaint area that needs to be updated when this item is animated
See Also:
Item.addRelativeToContentRegion(ClippingRegion, int, int, int, int)

setScreen

public void setScreen(Screen screen)
Convenience method to set the screen if the VideoContainer is not added to a Screen or a Container. Should only be used if you know what you're doing.

Parameters:
screen - the screen

setSource

public void setSource(VideoSource source)
Sets the VideoSource of this VideoContainer. If the source has already been set before, close() is called on the current, the state is reset and the new one set as the source.

Parameters:
source - the VideoSource to use

getSource

public VideoSource getSource()
Returns the current VideoSource

Returns:
the current VideoSource

setMultipart

public void setMultipart(VideoMultipart multipart)
] Sets a VideoMultipart as the source. Closes all previously used sources and sets the first part as the VideoSource

Parameters:
multipart - the VideoMultipart instance

close

public void close()
Closes this VideoContainer.


addCallback

public void addCallback(VideoCallback callback)
Adds a callback

Parameters:
callback - the callback

removeCallback

public void removeCallback(VideoCallback callback)
Removes a callback

Parameters:
callback - the callback

setEnterFullscreenGameAction

public void setEnterFullscreenGameAction(int gameAction)
Sets a shortcut

Parameters:
gameAction -

initSource

protected void initSource(VideoSource src)
                   throws Exception
Initializes the source by calling open() on it

Parameters:
src - the VideoSource to use
Throws:
Exception - if an error occurs

initDisplay

protected void initDisplay(VideoSource src)
                    throws Exception
Initializes the display by using the content area of the Container.

Parameters:
src - the VideoSource to use
Throws:
Exception - if an error occurs

setDisplay

protected void setDisplay(VideoControl control,
                          int x,
                          int y,
                          int width,
                          int height,
                          VideoContainer.Ratio ratio)
                   throws Exception
Set the display by using the given VideoControl instance and the dimensions given

Parameters:
control - the VideoControl instance
x - the x position
y - the y position
width - the width
height - the height
ratio - the ratio to use
Throws:
Exception - if an error occurs

initVolume

protected void initVolume(VideoSource source)
                   throws Exception
Initializes the volume

Parameters:
source - the VideoSource to use
Throws:
Exception - if an error occurs

prepare

public void prepare()
Starts the thread to prepare the video if the state is zero


play

public void play()
Plays the video or shows the snapshot preview window. If the video has not been prepared yet, this will be done in a background thread.


pause

public void pause()
Pauses the video


stop

public void stop()
Stops the video


seek

public void seek(long position)
Set the player to the given position. The position must be in microseconds.

Parameters:
position - the position in microseconds

getTime

public long getTime()
Returns the current time of the playback

Returns:
the current time

getLength

public long getLength()
Returns the total length of the video

Returns:
the total length

setVolume

public void setVolume(int volume)
Sets the volume

Parameters:
volume - the volume to set

getVolume

public int getVolume()
Returns the volume

Returns:
the volume

animate

public boolean animate()
Description copied from class: Item
Animates this item. Subclasses can override this method to create animations. The default implementation returns false

Overrides:
animate in class Item
Returns:
true when this item has been animated.
See Also:
Item.animate(long, ClippingRegion)

paint

public void paint(int x,
                  int y,
                  int leftBorder,
                  int rightBorder,
                  Graphics g)
Description copied from class: Item
Paints this item on the screen. This method should normally not be overriden. Override it only when you know what you are doing!

Overrides:
paint in class Item
Parameters:
x - the left start position of this item.
y - the top start position of this item.
leftBorder - the left border, nothing must be painted left of this position
rightBorder - the right border, nothing must be painted right of this position, rightBorder > x >= leftBorder
g - the Graphics on which this item should be painted.

setFullscreen

public void setFullscreen(boolean fullscreen)
Enters or leaves the fullscreen mode.

Parameters:
fullscreen - true if fullscreen mode should be entered, otherwise false
Throws:
IllegalStateException - when no video source has been set

setFullscreen

public void setFullscreen(VideoSource src,
                          boolean fullscreen)
Enters or leaves the fullscreen mode.

Parameters:
src - the video source
fullscreen - true if fullscreen mode should be entered, otherwise false
Throws:
IllegalArgumentException - when src is null
IllegalStateException - when the source cannot retrieve a VideoControl

setPseudoFullscreen

public void setPseudoFullscreen(boolean fullscreen)
Enters or leaves the fullscreen mode by using another styleable form

Parameters:
fullscreen - true if fullscreen mode should be entered, otherwise false
Throws:
IllegalArgumentException - when no video source has been set

setPseudoFullscreen

public void setPseudoFullscreen(VideoSource src,
                                boolean fullscreen)
Enters or leaves the fullscreen mode by using another styleable form

Parameters:
src - the video source
fullscreen - true if fullscreen mode should be entered, otherwise false
Throws:
IllegalArgumentException - when src is null

run

public void run()
Prepares the video by creating the player, initializing the display and controls and notifying the listeners when its done.

Specified by:
run in interface Runnable

next

public void next()

initContent

protected void initContent(int firstLineWidth,
                           int availWidth,
                           int availHeight)
Description copied from class: Item
Initialises this item. The implementation needs to calculate and set the contentWidth and contentHeight fields. The implementation should take the fields preferredWidth and preferredHeight into account.

Overrides:
initContent in class Container
Parameters:
firstLineWidth - the maximum width of the first line
availWidth - the available maximum width of this item in pixels
availHeight - the available maximum height of this item in pixels
See Also:
Item.contentWidth, Item.contentHeight, Item.preferredWidth, Item.preferredHeight

playerUpdate

public void playerUpdate(Player player,
                         String event,
                         Object data)
Specified by:
playerUpdate in interface PlayerListener

isLandscape

public boolean isLandscape()
Returns true if the video should be played in landscape mode

Returns:
true if landscape mode is set, otherwise false

setLandscape

public void setLandscape(boolean landscape)
Sets the landscape mode

Parameters:
landscape - true if the video should be played in landscape mode, otherwise false

isRepeat

public boolean isRepeat()
Returns true if the video should be repeated

Returns:
true if the video should be repeated, otherwise false

setRepeat

public void setRepeat(boolean repeat)
Sets the repeat mode

Parameters:
repeat - true if the video should be repeated, otherwise false

setRatio

public void setRatio(VideoContainer.Ratio ratio)
Sets the ratio to playback the video

Parameters:
ratio - the ratio

getState

public int getState()
Returns the current state of the VideoContainer

Returns:
the current state

isFullscreen

public boolean isFullscreen()
Returns true if the video is played in fullscreen mode

Returns:
true if the video is played in fullscreen mode, otherwise false

mute

public void mute(boolean mute)

isMute

public boolean isMute()

showNotify

protected void showNotify()
Description copied from class: Container
Called by the system to notify the item that it is now at least partially visible, when it previously had been completely invisible. The item may receive paint() calls after showNotify() has been called.

The container implementation calls showNotify() on the embedded items.

Overrides:
showNotify in class Container

capture

public byte[] capture(String encoding)
Captures a snapshot when a CaptureSource is used. A registered callback listener is notified about the actions as well. In case the internal player is not yet playing, it will started automatically.

Parameters:
encoding - the encoding of the image
Returns:
the corresponding byte[] data, null when an error occurs or when a VideoSource is used that is not a CaptureSource.

hideNotify

protected void hideNotify()
Description copied from class: Container
Called by the system to notify the item that it is now completely invisible, when it previously had been at least partially visible. No further paint() calls will be made on this item until after a showNotify() has been called again.

The container implementation calls hideNotify() on the embedded items.

Overrides:
hideNotify in class Container

onSnapshot

public void onSnapshot(byte[] data,
                       String encoding)
Description copied from interface: VideoCallback
Called when a capture is done

Specified by:
onSnapshot in interface VideoCallback
Parameters:
data - the resulting data of the capture

onVideoClose

public void onVideoClose()
Description copied from interface: VideoCallback
Called when a video is destroyed

Specified by:
onVideoClose in interface VideoCallback

onVideoError

public void onVideoError(Exception e)
Description copied from interface: VideoCallback
Called when an error occures

Specified by:
onVideoError in interface VideoCallback

getLastException

public Exception getLastException()
Retrieves the last exception that occurred, if any


onVideoPause

public void onVideoPause()
Description copied from interface: VideoCallback
Called when the video is paused

Specified by:
onVideoPause in interface VideoCallback

onVideoPlay

public void onVideoPlay()
Description copied from interface: VideoCallback
Called when the video is played

Specified by:
onVideoPlay in interface VideoCallback

onVideoReady

public void onVideoReady()
Description copied from interface: VideoCallback
Called when a video is fully prepared

Specified by:
onVideoReady in interface VideoCallback

onVideoStop

public void onVideoStop()
Description copied from interface: VideoCallback
Called when the video is stopped

Specified by:
onVideoStop in interface VideoCallback