# Class MediaPlayer

java.lang.Object
com.codename1.ui.Component
com.codename1.ui.Container
com.codename1.components.MediaPlayer

All Implemented Interfaces:
  `Animation, Editable, StyleListener, Iterable<Component>`

public class MediaPlayer extends Container

Video playback component with control buttons for back, play/pause and forward buttons. In the simulator those controls are implemented locally but on the device the native playback controls are used.

    final Form hi = new Form("MediaPlayer", new BorderLayout());
    hi.setToolbar(new Toolbar());
    Style s = UIManager.getInstance().getComponentStyle("Title");
    FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_VIDEO_LIBRARY, s);
    hi.getToolbar().addCommandToRightBar(new Command("", icon) {
    @Override
        public void actionPerformed(ActionEvent evt) {
            Display.getInstance().openGallery((e) -> {
                if(e != null && e.getSource() != null) {
                    String file = (String)e.getSource();
                    try {
                        Media video = MediaManager.createMedia(file, true);
                        hi.removeAll();
                        hi.add(BorderLayout.CENTER, new MediaPlayer(video));
                        hi.revalidate();
                    } catch(IOException err) {
                        Log.e(err);
                    }
                }
            }, Display.GALLERY_VIDEO);
        }
    });
    hi.show();

- ## Field Summary

  ### Fields inherited from class Component

  `BASELINE, BOTTOM, BRB_CENTER_OFFSET, BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT, BRB_OTHER, CENTER, CROSSHAIR_CURSOR, DEFAULT_CURSOR, DRAG_REGION_IMMEDIATELY_DRAG_X, DRAG_REGION_IMMEDIATELY_DRAG_XY, DRAG_REGION_IMMEDIATELY_DRAG_Y, DRAG_REGION_LIKELY_DRAG_X, DRAG_REGION_LIKELY_DRAG_XY, DRAG_REGION_LIKELY_DRAG_Y, DRAG_REGION_NOT_DRAGGABLE, DRAG_REGION_POSSIBLE_DRAG_X, DRAG_REGION_POSSIBLE_DRAG_XY, DRAG_REGION_POSSIBLE_DRAG_Y, E_RESIZE_CURSOR, HAND_CURSOR, LEFT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NW_RESIZE_CURSOR, RIGHT, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, TOP, W_RESIZE_CURSOR, WAIT_CURSOR`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `MediaPlayer()`
  Empty constructor
  `MediaPlayer(Media video)`
  Empty constructor

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected Dimension`
  `calcPreferredSize()`
  Calculates the preferred size based on component content.
  `protected void`
  `deinitialize()`
  Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy.
  `Image`
  `getBackIcon()`

  ###### Returns

  `String`
  `getDataSource()`
  Convenience JavaBean method, see other version of this method
  `Image`
  `getFwdIcon()`

  ###### Returns

  `Media`
  `getMedia()`
  Returns the Media Object of this MediaPlayer
  `Image`
  `getPauseIcon()`

  ###### Returns

  `Image`
  `getPlayIcon()`

  ###### Returns

  `String[]`
  `getPropertyNames()`
  A component may expose mutable property names for a UI designer to manipulate, this API is designed for usage internally by the GUI builder code
  `Class[]`
  `getPropertyTypes()`
  Matches the property names method (see that method for further details).
  `Object`
  `getPropertyValue(String name)`
  Returns the current value of the property name, this method is used by the GUI builder
  `String`
  `getSeekBarUIID()`
  UIID for the seekBar slider
  `void`
  `hideControls()`
  Hides the controls for this media player.
  `protected void`
  `initComponent()`
  Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state
  `boolean`
  `isAutoplay()`
  Sets playback to start automatically
  `boolean`
  `isHideNativeVideoControls()`
  On platforms that include native video player controls (Android and iOS), this indicates whether these controls should be hidden for this media player.
  `boolean`
  `isLoop()`
  Sets playback to loop
  `boolean`
  `isMaximize()`
  Includes a maximize icon in the bar to show the native player
  `boolean`
  `isOnTopMode()`
  Shows the buttons on top of the video
  `boolean`
  `isSeekBar()`
  Shows video position bar as a slider
  `void`
  `run()`
  `void`
  `setAutoplay(boolean autoplay)`
  Sets playback to start automatically
  `void`
  `setBackIcon(Image backIcon)`
  Sets the back Button Icon
  `void`
  `setDataSource(InputStream is, String mimeType, Runnable onCompletion)`
  Sets the data source of this video player
  `void`
  `setDataSource(String uri)`
  Convenience JavaBean method, see other version of this method
  `void`
  `setDataSource(String uri, Runnable onCompletion)`
  Sets the data source of this video player
  `void`
  `setFwdIcon(Image fwdIcon)`
  Sets the forward Button Icon
  `void`
  `setHideNativeVideoControls(boolean hideNativeControls)`
  On platforms that include native video player controls (Android and iOS), this allows you to hide those controls.
  `void`
  `setLoop(boolean loop)`
  Sets playback to loop
  `void`
  `setMaxIcon(Image maxIcon)`
  Sets the maximize Button Icon
  `void`
  `setMaximize(boolean maximize)`
  Includes a maximize icon in the bar to show the native player
  `void`
  `setOnTopMode(boolean onTopMode)`
  Shows the buttons on top of the video
  `void`
  `setPauseIcon(Image pauseIcon)`
  Sets the pause Button Icon
  `void`
  `setPlayIcon(Image playIcon)`
  Sets the play Button Icon
  `String`
  `setPropertyValue(String name, Object value)`
  Sets a new value to the given property, returns an error message if failed and null if successful.
  `void`
  `setSeekBar(boolean seekBar)`
  Shows video position bar as a slider
  `void`
  `setSeekBarUIID(String seekBarUIID)`
  UIID for the seekBar slider
  `void`
  `showControls()`
  Shows the controls for this media player.
  `boolean`
  `usesNativeVideoControls()`
  Checks to see if this player uses native video controls.

  ### Methods inherited from class Container

  `add, add, add, add, add, add, addAll, addComponent, addComponent, addComponent, addComponent, animateHierarchy, animateHierarchyAndWait, animateHierarchyFade, animateHierarchyFadeAndWait, animateLayout, animateLayoutAndWait, animateLayoutFade, animateLayoutFadeAndWait, animateUnlayout, animateUnlayoutAndWait, applyRTL, cancelRepaints, clearClientProperties, constrainHeightWhenScrollable, constrainWidthWhenScrollable, contains, createAnimateHierarchy, createAnimateHierarchyFade, createAnimateLayout, createAnimateLayoutFade, createAnimateLayoutFadeAndWait, createAnimateMotion, createAnimateUnlayout, createReplaceTransition, dragInitiated, drop, encloseIn, encloseIn, findDropTargetAt, findFirstFocusable, fireClicked, flushReplace, forceRevalidate, getBottomGap, getChildrenAsList, getClosestComponentTo, getComponentAt, getComponentAt, getComponentCount, getComponentIndex, getGridPosX, getGridPosY, getLayout, getLayoutHeight, getLayoutWidth, getLeadComponent, getLeadParent, getResponderAt, getSafeAreaRoot, getScrollIncrement, getSideGap, getUIManager, initLaf, invalidate, isEnabled, isSafeArea, isSafeAreaRoot, isScrollableX, isScrollableY, isSelectableInteraction, isSurface, iterator, iterator, keyPressed, keyReleased, layoutContainer, morph, morphAndWait, paint, paintComponentBackground, paintGlass, paramString, pointerPressed, refreshTheme, removeAll, removeComponent, replace, replace, replaceAndWait, replaceAndWait, replaceAndWait, revalidate, revalidateLater, revalidateWithAnimationSafety, scrollComponentToVisible, setCellRenderer, setEnabled, setLayout, setLeadComponent, setSafeArea, setSafeAreaRoot, setScrollable, setScrollableX, setScrollableY, setScrollIncrement, setShouldCalcPreferredSize, setShouldLayout, setUIManager, updateTabIndices`

  ### Methods inherited from class Component

  `addDragFinishedListener, addDragOverListener, addDropListener, addFocusListener, addLongPressListener, addPointerDraggedListener, addPointerPressedListener, addPointerReleasedListener, addPullToRefresh, addScrollListener, addStateChangeListener, animate, announceForAccessibility, bindProperty, blocksSideSwipe, calcScrollSize, contains, containsOrOwns, createStyleAnimation, deinitializeCustomStyle, dragEnter, dragExit, dragFinished, draggingOver, drawDraggedImage, focusGained, focusLost, getAbsoluteX, getAbsoluteY, getAccessibilityText, getAllStyles, getAnimationManager, getBaseline, getBaselineResizeBehavior, getBindablePropertyNames, getBindablePropertyTypes, getBorder, getBoundPropertyValue, getBounds, getBounds, getClientProperty, getCloudBoundProperty, getCloudDestinationProperty, getComponentForm, getComponentState, getCursor, getDefaultDragTransparency, getDirtyRegion, getDisabledStyle, getDraggedx, getDraggedy, getDragImage, getDragRegionStatus, getDragSpeed, getDragTransparency, getEditingDelegate, getHeight, getInlineAllStyles, getInlineDisabledStyles, getInlinePressedStyles, getInlineSelectedStyles, getInlineStylesTheme, getInlineUnselectedStyles, getInnerHeight, getInnerPreferredH, getInnerPreferredW, getInnerWidth, getInnerX, getInnerY, getLabelForComponent, getName, getNativeOverlay, getNextFocusDown, getNextFocusLeft, getNextFocusRight, getNextFocusUp, getOuterHeight, getOuterPreferredH, getOuterPreferredW, getOuterWidth, getOuterX, getOuterY, getOwner, getParent, getPreferredH, getPreferredSize, getPreferredSizeStr, getPreferredTabIndex, getPreferredW, getPressedStyle, getPropertyTypeNames, getSameHeight, getSameWidth, getScrollable, getScrollAnimationSpeed, getScrollDimension, getScrollOpacity, getScrollOpacityChangeSpeed, getScrollX, getScrollY, getSelectCommandText, getSelectedRect, getSelectedStyle, getStyle, getTabIndex, getTensileLength, getTextSelectionSupport, getTooltip, getUIID, getUnselectedStyle, getVisibleBounds, getVisibleBounds, getWidth, getX, getY, growShrink, handlesInput, hasFixedPreferredSize, hasFocus, hideNativeOverlay, initCustomStyle, initDisabledStyle, initPressedStyle, initSelectedStyle, initUnselectedStyle, installDefaultPainter, isAlwaysTensile, isBlockLead, isCellRenderer, isChildOf, isDragActivated, isDragAndDropOperation, isDraggable, isDragRegion, isDropTarget, isEditable, isEditing, isFlatten, isFocusable, isGrabsPointerEvents, isHidden, isHidden, isHideInLandscape, isHideInPortrait, isIgnorePointerEvents, isInClippingRegion, isInitialized, isOpaque, isOwnedBy, isPinchBlocksDragAndDrop, isRippleEffect, isRTL, isScrollable, isScrollVisible, isSetCursorSupported, isSmoothScrolling, isSnapToGrid, isStickyDrag, isTactileTouch, isTactileTouch, isTensileDragEnabled, isTraversable, isVisible, keyRepeated, laidOut, longKeyPress, longPointerPress, onScrollX, onScrollY, onSetFocusable, paintBackground, paintBackgrounds, paintBorder, paintBorderBackground, paintComponent, paintComponent, paintIntersectingComponentsAbove, paintLock, paintLockRelease, paintRippleOverlay, paintScrollbars, paintScrollbarX, paintScrollbarY, paintShadows, parsePreferredSize, pinch, pinch, pinchReleased, pointerDragged, pointerDragged, pointerHover, pointerHoverPressed, pointerHoverReleased, pointerPressed, pointerReleased, pointerReleased, putClientProperty, refreshTheme, refreshTheme, remove, removeDragFinishedListener, removeDragOverListener, removeDropListener, removeFocusListener, removeLongPressListener, removePointerDraggedListener, removePointerPressedListener, removePointerReleasedListener, removeScrollListener, removeStateChangeListener, repaint, repaint, requestFocus, resetFocusable, respondsToPointerEvents, scrollRectToVisible, scrollRectToVisible, setAccessibilityText, setAlwaysTensile, setBlockLead, setBoundPropertyValue, setCloudBoundProperty, setCloudDestinationProperty, setComponentState, setCursor, setDefaultDragTransparency, setDirtyRegion, setDisabledStyle, setDraggable, setDragTransparency, setDropTarget, setEditingDelegate, setFlatten, setFocus, setFocusable, setGrabsPointerEvents, setHandlesInput, setHeight, setHidden, setHidden, setHideInLandscape, setHideInPortrait, setHorizontalScrollBounds, setIgnorePointerEvents, setInitialized, setInlineAllStyles, setInlineDisabledStyles, setInlinePressedStyles, setInlineSelectedStyles, setInlineStylesTheme, setInlineUnselectedStyles, setIsScrollVisible, setLabelForComponent, setName, setNextFocusDown, setNextFocusLeft, setNextFocusRight, setNextFocusUp, setOpaque, setOwner, setPinchBlocksDragAndDrop, setPreferredH, setPreferredSize, setPreferredSizeStr, setPreferredTabIndex, setPreferredW, setPressedStyle, setPullToRefresh, setRippleEffect, setRTL, setSameHeight, setSameSize, setSameWidth, setScrollAnimationSpeed, setScrollOpacityChangeSpeed, setScrollSize, setScrollVisible, setScrollX, setScrollY, setSelectCommandText, setSelectedStyle, setSize, setSmoothScrolling, setSnapToGrid, setTabIndex, setTactileTouch, setTensileDragEnabled, setTensileLength, setTooltip, setTraversable, setUIID, setUIID, setUIIDFinal, setUnselectedStyle, setVerticalScrollBounds, setVisible, setWidth, setX, setY, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, shouldRenderComponentSelection, showNativeOverlay, startEditingAsync, stopEditing, stripMarginAndPadding, styleChanged, toImage, toString, unbindProperty, updateNativeOverlay, visibleBoundsContains`

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface Iterable

  `forEach`

- ## Constructor Details

  - ### MediaPlayer

    public MediaPlayer()
    Empty constructor

  - ### MediaPlayer

    public MediaPlayer(Media video)
    Empty constructor

- ## Method Details

  - ### isHideNativeVideoControls

    public boolean isHideNativeVideoControls()

    On platforms that include native video player controls (Android and iOS), this indicates whether these controls should be hidden for this media player.

    ###### Returns

    true if native video player controls should be hidden.

    ###### See also

    - Display#isNativeVideoPlayerControlsIncluded()

    - #setHideNativeVideoControls(boolean)

    - #usesNativeVideoControls()

  - ### setHideNativeVideoControls

    public void setHideNativeVideoControls(boolean hideNativeControls)

    On platforms that include native video player controls (Android and iOS), this allows you to hide those controls.

    ###### Parameters

    - `hideNativeControls`: Set true to hide the native video controls for this player.

    ###### See also

    - Display#isNativeVideoPlayerControlsIncluded()

    - #setHideNativeVideoControls(boolean)

    - #usesNativeVideoControls()

  - ### usesNativeVideoControls

    public boolean usesNativeVideoControls()

    Checks to see if this player uses native video controls. For this to be true, the platform must support native video controls (iOS and Android) (See `Display#isNativeVideoPlayerControlsIncluded()` to find out if current platform supports this; AND `#isHideNativeVideoControls()` must be false.

    Note: on IOS, the controls won't display until the video's `Media#prepare()` is called. This will happen automatically if `#isAutoplay()` is true, or if `Media#play()` is called.

    ###### Returns

    True if this player uses native video controls.

    ###### See also

    - #isHideNativeVideoControls()

    - #setHideNativeVideoControls(boolean)

    - Display#isNativeVideoPlayerControlsIncluded()

  - ### showControls

    public void showControls()

    Shows the controls for this media player. If the player is set to use native controls, then this will show the native controls. Otherwise it shows the lightweight controls.

    Note: on IOS, the controls won't display until the video's `Media#prepare()` is called. This will happen automatically if `#isAutoplay()` is true, or if `Media#play()` is called.

  - ### hideControls

    public void hideControls()
    Hides the controls for this media player. If the player is set to use native controls, then this will hide the native controls. Otherwise it hides the lightweight controls.

  - ### getMedia

    public Media getMedia()
    Returns the Media Object of this MediaPlayer

  - ### initComponent

    protected void initComponent()
    Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state
    Overrides:
      `initComponent` in class `Component`

  - ### deinitialize

    protected void deinitialize()
    Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. This allows the component to deregister animators and cleanup after itself. This method is the opposite of the initComponent() method.
    Overrides:
      `deinitialize` in class `Component`

  - ### calcPreferredSize

    protected Dimension calcPreferredSize()

    Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

    ###### Returns

    the calculated preferred size based on component content

    Overrides:
      `calcPreferredSize` in class `Container`

  - ### setMaxIcon

    public void setMaxIcon(Image maxIcon)

    Sets the maximize Button Icon

    ###### Parameters

    - `maxIcon`

  - ### setDataSource

    public void setDataSource(String uri, Runnable onCompletion) throws java.io.IOException

    Sets the data source of this video player

    ###### Parameters

    - `uri`: @param uri the uri of the media can start with file://, http:// (can also use rtsp:// although may not be supported on all target platforms)

    ###### Throws

    - `IOException`: if creation of media from the given URI has failed
    Throws:
      `java.io.IOException`

  - ### getDataSource

    public String getDataSource()

    Convenience JavaBean method, see other version of this method

    ###### Returns

    the data source uri

  - ### setDataSource

    public void setDataSource(String uri)

    Convenience JavaBean method, see other version of this method

    ###### Parameters

    - `uri`: the URL for the media

  - ### setDataSource

    public void setDataSource(InputStream is, String mimeType, Runnable onCompletion) throws java.io.IOException

    Sets the data source of this video player

    ###### Parameters

    - `is`: the stream containing the media data

    - `mimeType`: the type of the data in the stream

    ###### Throws

    - `java.io.IOException`: if the creation of the Media has failed
    Throws:
      `java.io.IOException`

  - ### run

    public void run()

  - ### getPropertyNames

    public String[] getPropertyNames()

    A component may expose mutable property names for a UI designer to manipulate, this API is designed for usage internally by the GUI builder code

    ###### Returns

    the property names allowing mutation

    Overrides:
      `getPropertyNames` in class `Component`

  - ### getPropertyTypes

    public Class[] getPropertyTypes()

    Matches the property names method (see that method for further details).

    ###### Returns

    the types of the properties

    Overrides:
      `getPropertyTypes` in class `Component`

  - ### getPropertyValue

    public Object getPropertyValue(String name)

    Returns the current value of the property name, this method is used by the GUI builder

    ###### Parameters

    - `name`: the name of the property

    ###### Returns

    the value of said property

    Overrides:
      `getPropertyValue` in class `Component`

  - ### setPropertyValue

    public String setPropertyValue(String name, Object value)

    Sets a new value to the given property, returns an error message if failed and null if successful. Notice that some builtin properties such as "$designMode" might be sent to components to indicate application state.

    ###### Parameters

    - `name`: the name of the property

    - `value`: new value for the property

    ###### Returns

    error message or null

    Overrides:
      `setPropertyValue` in class `Component`

  - ### getPlayIcon

    public Image getPlayIcon()

    ###### Returns

    the playIcon

  - ### setPlayIcon

    public void setPlayIcon(Image playIcon)

    Sets the play Button Icon

    ###### Parameters

    - `playIcon`

  - ### getPauseIcon

    public Image getPauseIcon()

    ###### Returns

    the pauseIcon

  - ### setPauseIcon

    public void setPauseIcon(Image pauseIcon)

    Sets the pause Button Icon

    ###### Parameters

    - `pauseIcon`

  - ### getBackIcon

    public Image getBackIcon()

    ###### Returns

    the backIcon

  - ### setBackIcon

    public void setBackIcon(Image backIcon)

    Sets the back Button Icon

    ###### Parameters

    - `backIcon`

  - ### getFwdIcon

    public Image getFwdIcon()

    ###### Returns

    the fwdIcon

  - ### setFwdIcon

    public void setFwdIcon(Image fwdIcon)

    Sets the forward Button Icon

    ###### Parameters

    - `fwdIcon`

  - ### isAutoplay

    public boolean isAutoplay()

    Sets playback to start automatically

    ###### Returns

    the autoplay

  - ### setAutoplay

    public void setAutoplay(boolean autoplay)

    Sets playback to start automatically

    ###### Parameters

    - `autoplay`: the autoplay to set

  - ### isLoop

    public boolean isLoop()

    Sets playback to loop

    ###### Returns

    the loop

  - ### setLoop

    public void setLoop(boolean loop)

    Sets playback to loop

    ###### Parameters

    - `loop`: the loop to set

  - ### isOnTopMode

    public boolean isOnTopMode()

    Shows the buttons on top of the video

    ###### Returns

    the onTopMode

  - ### setOnTopMode

    public void setOnTopMode(boolean onTopMode)

    Shows the buttons on top of the video

    ###### Parameters

    - `onTopMode`: the onTopMode to set

  - ### isSeekBar

    public boolean isSeekBar()

    Shows video position bar as a slider

    ###### Returns

    the seekBar

  - ### setSeekBar

    public void setSeekBar(boolean seekBar)

    Shows video position bar as a slider

    ###### Parameters

    - `seekBar`: the seekBar to set

  - ### getSeekBarUIID

    public String getSeekBarUIID()

    UIID for the seekBar slider

    ###### Returns

    the seekBarUIID

  - ### setSeekBarUIID

    public void setSeekBarUIID(String seekBarUIID)

    UIID for the seekBar slider

    ###### Parameters

    - `seekBarUIID`: the seekBarUIID to set

  - ### isMaximize

    public boolean isMaximize()

    Includes a maximize icon in the bar to show the native player

    ###### Returns

    the maximize

  - ### setMaximize

    public void setMaximize(boolean maximize)

    Includes a maximize icon in the bar to show the native player

    ###### Parameters

    - `maximize`: the maximize to set