# Class AbstractMedia

java.lang.Object
com.codename1.media.AbstractMedia

All Implemented Interfaces:
  `AsyncMedia, Media`

public abstract class AbstractMedia extends java.lang.Object implements AsyncMedia
An abstract base class for AsyncMedia. Most media returned from `MediaManager` will be descendants of this class.

- ## Nested Class Summary

  ### Nested classes/interfaces inherited from interface AsyncMedia

  `AsyncMedia.MediaErrorEvent, AsyncMedia.MediaErrorType, AsyncMedia.MediaException, AsyncMedia.MediaStateChangeEvent, AsyncMedia.PauseRequest, AsyncMedia.PlayRequest, AsyncMedia.State`

- ## Field Summary

  ### Fields inherited from interface Media

  `VARIABLE_ANDROID_SEEK_PREVIEW_WORKAROUND, VARIABLE_BACKGROUND_ALBUM_COVER, VARIABLE_BACKGROUND_ARTIST, VARIABLE_BACKGROUND_DURATION, VARIABLE_BACKGROUND_POSITION, VARIABLE_BACKGROUND_SUPPORTED, VARIABLE_BACKGROUND_TITLE, VARIABLE_NATIVE_CONTRLOLS_EMBEDDED`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `AbstractMedia()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addMediaCompletionHandler(Runnable onComplete)`
  Adds a callback to be run when the media has played to completion.
  `void`
  `addMediaErrorListener(ActionListener<AsyncMedia.MediaErrorEvent> l)`
  Adds a listener to be notified when an error occurs in the media.
  `void`
  `addMediaStateChangeListener(ActionListener<AsyncMedia.MediaStateChangeEvent> l)`
  Adds a listener to be notified when the state of the media changes.
  `protected AsyncMedia.MediaErrorEvent`
  `fireMediaError(AsyncMedia.MediaException ex)`
  Fires a media error event to registered listeners.
  `protected AsyncMedia.MediaStateChangeEvent`
  `fireMediaStateChange(AsyncMedia.State newState)`
  Fires a media state change event to the registered state change listeners.
  `AsyncMedia.State`
  `getState()`
  Gets the current state of the media object.
  `final void`
  `pause()`
  Pauses (actually stops) the playback or the recording of the media file
  `AsyncMedia.PauseRequest`
  `pauseAsync()`
  Initiates a pause request.
  `protected abstract void`
  `pauseImpl()`
  Initiates a pause request on the media.
  `final void`
  `play()`
  Starts playing or recording the media file
  `AsyncMedia.PlayRequest`
  `playAsync()`
  Initiates a play request.
  `protected abstract void`
  `playImpl()`
  Initiates a play request on the media.
  `void`
  `removeMediaErrorListener(ActionListener<AsyncMedia.MediaErrorEvent> l)`
  Removes a listener so that it will no longer be notified of errors.
  `void`
  `removeMediaStateChangeListener(ActionListener<AsyncMedia.MediaStateChangeEvent> l)`
  Removes a listener so that it will no longer be notified of state changes.

  ### Methods inherited from class java.lang.Object

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

  ### Methods inherited from interface Media

  `cleanup, getDuration, getTime, getVariable, getVideoComponent, getVolume, isFullScreen, isNativePlayerMode, isPlaying, isVideo, prepare, setFullScreen, setNativePlayerMode, setTime, setVariable, setVolume`

- ## Constructor Details

  - ### AbstractMedia

    public AbstractMedia()

- ## Method Details

  - ### getState

    public AsyncMedia.State getState()

    Gets the current state of the media object.

    ###### Returns

    The state of the media object.

    Specified by:
      `getState` in interface `AsyncMedia`

  - ### fireMediaStateChange

    protected AsyncMedia.MediaStateChangeEvent fireMediaStateChange(AsyncMedia.State newState)

    Fires a media state change event to the registered state change listeners.

    ###### Parameters

    - `newState`: The new state

    ###### Returns

    The state change event.

  - ### addMediaStateChangeListener

    public void addMediaStateChangeListener(ActionListener<AsyncMedia.MediaStateChangeEvent> l)

    Adds a listener to be notified when the state of the media changes.

    ###### Parameters

    - `l`: Listener
    Specified by:
      `addMediaStateChangeListener` in interface `AsyncMedia`

  - ### removeMediaStateChangeListener

    public void removeMediaStateChangeListener(ActionListener<AsyncMedia.MediaStateChangeEvent> l)

    Removes a listener so that it will no longer be notified of state changes.

    ###### Parameters

    - `l`: Listener
    Specified by:
      `removeMediaStateChangeListener` in interface `AsyncMedia`

  - ### fireMediaError

    protected AsyncMedia.MediaErrorEvent fireMediaError(AsyncMedia.MediaException ex)

    Fires a media error event to registered listeners.

    ###### Parameters

    - `ex`: The MediaException to deliver

    ###### Returns

    The MediaErrorEvent object sent to listeners.

    ###### See also

    - #addMediaErrorListener(com.codename1.ui.events.ActionListener)

    - #removeMediaErrorListener(com.codename1.ui.events.ActionListener)

  - ### addMediaErrorListener

    public void addMediaErrorListener(ActionListener<AsyncMedia.MediaErrorEvent> l)

    Adds a listener to be notified when an error occurs in the media.

    ###### Parameters

    - `l`: Listener
    Specified by:
      `addMediaErrorListener` in interface `AsyncMedia`

  - ### removeMediaErrorListener

    public void removeMediaErrorListener(ActionListener<AsyncMedia.MediaErrorEvent> l)

    Removes a listener so that it will no longer be notified of errors.

    ###### Parameters

    - `l`: Listener
    Specified by:
      `removeMediaErrorListener` in interface `AsyncMedia`

  - ### addMediaCompletionHandler

    public void addMediaCompletionHandler(Runnable onComplete)

    Adds a callback to be run when the media has played to completion.

    ###### Parameters

    - `onComplete`
    Specified by:
      `addMediaCompletionHandler` in interface `AsyncMedia`

  - ### playAsync

    public AsyncMedia.PlayRequest playAsync()
    Initiates a play request. Returns immediately without blocking. Caller can use the returned PlayRequest object to be notified when playing has actually started.
    Specified by:
      `playAsync` in interface `AsyncMedia`

  - ### pauseAsync

    public AsyncMedia.PauseRequest pauseAsync()
    Initiates a pause request. Returns immediately without blocking. Caller can use the returned PauseRequest object to be notified when media has actually paused.
    Specified by:
      `pauseAsync` in interface `AsyncMedia`

  - ### playImpl

    protected abstract void playImpl()
    Initiates a play request on the media.

  - ### pauseImpl

    protected abstract void pauseImpl()
    Initiates a pause request on the media.

  - ### play

    public final void play()
    Starts playing or recording the media file
    Specified by:
      `play` in interface `Media`

  - ### pause

    public final void pause()
    Pauses (actually stops) the playback or the recording of the media file
    Specified by:
      `pause` in interface `Media`