# Class AbstractMedia

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

All Implemented Interfaces:
  `AsyncMedia, Media`

public abstract class AbstractMedia extends 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`
  Modifier and Type
  Interface
  Description
  `static class`
  `AsyncMedia.MediaErrorEvent`
  Encapsulates a media error event.
  `static enum`
  `AsyncMedia.MediaErrorType`
  Enum encapsulating the different types of media errors that can occur.
  `static class`
  `AsyncMedia.MediaException`
  Encapsulates a media exception.
  `static class`
  `AsyncMedia.MediaStateChangeEvent`
  Encapsulates a state-change event on a Media object.
  `static class`
  `AsyncMedia.PauseRequest`
  An async resource used to track the progress of a pauseAsync() request.
  `static class`
  `AsyncMedia.PlayRequest`
  An async resource used to track the progress of a playAsync() request.
  `static enum`
  `AsyncMedia.State`
  An enum to represent the state of a media object.

- ## 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`
  Modifier and Type
  Field
  Description
  `static final String`
  `VARIABLE_ANDROID_SEEK_PREVIEW_WORKAROUND`
  Optional boolean flag to enable an Android-specific workaround for flaky black-frame previews after `Media.setTime(int)` on paused video.
  `static final String`
  `VARIABLE_BACKGROUND_ALBUM_COVER`
  Write only variable that can be used with setVariable to pass the album cover Image to the native layer so the currently playing media title will be displayed in the lock screen where applicable
  `static final String`
  `VARIABLE_BACKGROUND_ARTIST`
  Write only variable that can be used with setVariable to pass the artist name to the native layer so the currently playing media title will be displayed in the lock screen where applicable
  `static final String`
  `VARIABLE_BACKGROUND_DURATION`
  Write only variable that can be used with setVariable to pass the duration for the media as a Long object to the native layer so the currently playing media title will be displayed in the lock screen where applicable
  `static final String`
  `VARIABLE_BACKGROUND_POSITION`
  Write only variable that can be used with setVariable to pass the position in the media (Long object) to the native layer so the currently playing media title will be displayed in the lock screen where applicable
  `static final String`
  `VARIABLE_BACKGROUND_SUPPORTED`
  Read only variable that can be used with getVariable to query whether the native layer supports displaying the currently playing media information in the lock screen.
  `static final String`
  `VARIABLE_BACKGROUND_TITLE`
  Write only variable that can be used with setVariable to pass a title for the native layer so the currently playing media title will be displayed in the lock screen where applicable
  `static final String`
  `VARIABLE_NATIVE_CONTRLOLS_EMBEDDED`
  Write-only variable that can be used with getVariable() to set whether this video should include embedded native controls.

- ## 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 Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `final Class`
  `getClass()`
  Returns the runtime class of an object.
  `int`
  `hashCode()`
  Returns a hash code value for the object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes current thread to wait until another thread invokes the method or the method for this object.
  `final void`
  `wait(long timeout)`
  Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.
  `final void`
  `wait(long timeout, int nanos)`
  Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

  ### Methods inherited from interface Media

  `cleanup, getDuration, getTime, getVariable, getVideoComponent, getVolume, isFullScreen, isNativePlayerMode, isPlaying, isVideo, prepare, setFullScreen, setNativePlayerMode, setTime, setVariable, setVolume`
  Modifier and Type
  Method
  Description
  `void`
  `cleanup()`
  Stops the audio playback and cleans up the resources related to it immediately.
  `int`
  `getDuration()`
  Returns the length in milliseconds of the audio file or -1 if not known
  `int`
  `getTime()`
  Returns the time in milliseconds in the audio file or -1 if not known
  `Object`
  `getVariable(String key)`
  Allows querying platform specific information from the media object
  `Component`
  `getVideoComponent()`
  Gets the VideoComponent of this Video.
  `int`
  `getVolume()`
  Returns the media playback volume in percentage
  `boolean`
  `isFullScreen()`
  This method returns true if this video is in full screen mode.
  `boolean`
  `isNativePlayerMode()`
  Returns true if this Video Media is in Native player mode.
  `boolean`
  `isPlaying()`
  Returns true if the media is currently playing or recording
  `boolean`
  `isVideo()`
  This method returns true if this is a Video Media
  `void`
  `prepare()`
  Optional call that allows the caller to prepare the upcoming media player.
  `void`
  `setFullScreen(boolean fullScreen)`
  Sets the Media to be displayed full screen, make sure the getVideoComponent() is called on the Video Component is added to the current Form
  `void`
  `setNativePlayerMode(boolean nativePlayer)`
  By calling this the Media (if it's a Video) will be played full screen on the native Player of the device.
  `void`
  `setTime(int time)`
  Sets the position in the audio file or doesn't effect if not supported
  `void`
  `setVariable(String key, Object value)`
  Allows for platform specific enhancements for media playback
  `void`
  `setVolume(int vol)`
  Sets the media playback volume in percentage

- ## 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`