Interface ExoPlayer

  • All Superinterfaces:
    Player
    All Known Implementing Classes:
    SimpleExoPlayer, StubExoPlayer

    public interface ExoPlayer
    extends Player
    An extensible media player that plays MediaSources. Instances can be obtained from ExoPlayer.Builder.

    Player components

    ExoPlayer is designed to make few assumptions about (and hence impose few restrictions on) the type of the media being played, how and where it is stored, and how it is rendered. Rather than implementing the loading and rendering of media directly, ExoPlayer implementations delegate this work to components that are injected when a player is created or when it's prepared for playback. Components common to all ExoPlayer implementations are:

    • MediaSources that define the media to be played, load the media, and from which the loaded media can be read. MediaSources are created from MediaItems by the MediaSource.Factory injected into the player Builder, or can be added directly by methods like setMediaSource(MediaSource). The library provides a DefaultMediaSourceFactory for progressive media files, DASH, SmoothStreaming and HLS, which also includes functionality for side-loading subtitle files and clipping media.
    • Renderers that render individual components of the media. The library provides default implementations for common media types (MediaCodecVideoRenderer, MediaCodecAudioRenderer, TextRenderer and MetadataRenderer). A Renderer consumes media from the MediaSource being played. Renderers are injected when the player is created. The number of renderers and their respective track types can be obtained by calling getRendererCount() and getRendererType(int).
    • A TrackSelector that selects tracks provided by the MediaSource to be consumed by each of the available Renderers. The library provides a default implementation (DefaultTrackSelector) suitable for most use cases. A TrackSelector is injected when the player is created.
    • A LoadControl that controls when the MediaSource buffers more media, and how much media is buffered. The library provides a default implementation (DefaultLoadControl) suitable for most use cases. A LoadControl is injected when the player is created.

    An ExoPlayer can be built using the default components provided by the library, but may also be built using custom implementations if non-standard behaviors are required. For example a custom LoadControl could be injected to change the player's buffering strategy, or a custom Renderer could be injected to add support for a video codec not supported natively by Android.

    The concept of injecting components that implement pieces of player functionality is present throughout the library. The default component implementations listed above delegate work to further injected components. This allows many sub-components to be individually replaced with custom implementations. For example the default MediaSource implementations require one or more DataSource factories to be injected via their constructors. By providing a custom factory it's possible to load data from a non-standard source, or through a different network stack.

    Threading model

    The figure below shows ExoPlayer's threading model.

    ExoPlayer's
 threading model

    • ExoPlayer instances must be accessed from a single application thread. For the vast majority of cases this should be the application's main thread. Using the application's main thread is also a requirement when using ExoPlayer's UI components or the IMA extension. The thread on which an ExoPlayer instance must be accessed can be explicitly specified by passing a `Looper` when creating the player. If no `Looper` is specified, then the `Looper` of the thread that the player is created on is used, or if that thread does not have a `Looper`, the `Looper` of the application's main thread is used. In all cases the `Looper` of the thread from which the player must be accessed can be queried using Player.getApplicationLooper().
    • Registered listeners are called on the thread associated with Player.getApplicationLooper(). Note that this means registered listeners are called on the same thread which must be used to access the player.
    • An internal playback thread is responsible for playback. Injected player components such as Renderers, MediaSources, TrackSelectors and LoadControls are called by the player on this thread.
    • When the application performs an operation on the player, for example a seek, a message is delivered to the internal playback thread via a message queue. The internal playback thread consumes messages from the queue and performs the corresponding operations. Similarly, when a playback event occurs on the internal playback thread, a message is delivered to the application thread via a second message queue. The application thread consumes messages from the queue, updating the application visible state and calling corresponding listener methods.
    • Injected player components may use additional background threads. For example a MediaSource may use background threads to load data. These are implementation specific.
    • Method Detail

      • addAudioOffloadListener

        void addAudioOffloadListener​(ExoPlayer.AudioOffloadListener listener)
        Adds a listener to receive audio offload events.
        Parameters:
        listener - The listener to register.
      • removeAudioOffloadListener

        void removeAudioOffloadListener​(ExoPlayer.AudioOffloadListener listener)
        Removes a listener of audio offload events.
        Parameters:
        listener - The listener to unregister.
      • addAnalyticsListener

        void addAnalyticsListener​(AnalyticsListener listener)
        Adds an AnalyticsListener to receive analytics events.
        Parameters:
        listener - The listener to be added.
      • getRendererCount

        int getRendererCount()
        Returns the number of renderers.
      • getRendererType

        @com.google.android.exoplayer2.C.TrackType int getRendererType​(int index)
        Returns the track type that the renderer at a given index handles.

        For example, a video renderer will return C.TRACK_TYPE_VIDEO, an audio renderer will return C.TRACK_TYPE_AUDIO and a text renderer will return C.TRACK_TYPE_TEXT.

        Parameters:
        index - The index of the renderer.
        Returns:
        The track type that the renderer handles.
      • getRenderer

        Renderer getRenderer​(int index)
        Returns the renderer at the given index.
        Parameters:
        index - The index of the renderer.
        Returns:
        The renderer at this index.
      • getTrackSelector

        @Nullable
        TrackSelector getTrackSelector()
        Returns the track selector that this player uses, or null if track selection is not supported.
      • getPlaybackLooper

        Looper getPlaybackLooper()
        Returns the Looper associated with the playback thread.
      • getClock

        Clock getClock()
        Returns the Clock used for playback.
      • setMediaSources

        void setMediaSources​(List<MediaSource> mediaSources)
        Clears the playlist, adds the specified MediaSources and resets the position to the default position.
        Parameters:
        mediaSources - The new MediaSources.
      • setMediaSources

        void setMediaSources​(List<MediaSource> mediaSources,
                             int startMediaItemIndex,
                             long startPositionMs)
        Clears the playlist and adds the specified MediaSources.
        Parameters:
        mediaSources - The new MediaSources.
        startMediaItemIndex - The media item index to start playback from. If C.INDEX_UNSET is passed, the current position is not reset.
        startPositionMs - The position in milliseconds to start playback from. If C.TIME_UNSET is passed, the default position of the given media item is used. In any case, if startMediaItemIndex is set to C.INDEX_UNSET, this parameter is ignored and the position is not reset at all.
      • setMediaSource

        void setMediaSource​(MediaSource mediaSource)
        Clears the playlist, adds the specified MediaSource and resets the position to the default position.
        Parameters:
        mediaSource - The new MediaSource.
      • setMediaSource

        void setMediaSource​(MediaSource mediaSource,
                            long startPositionMs)
        Clears the playlist and adds the specified MediaSource.
        Parameters:
        mediaSource - The new MediaSource.
        startPositionMs - The position in milliseconds to start playback from.
      • addMediaSource

        void addMediaSource​(MediaSource mediaSource)
        Adds a media source to the end of the playlist.
        Parameters:
        mediaSource - The MediaSource to add.
      • addMediaSource

        void addMediaSource​(int index,
                            MediaSource mediaSource)
        Adds a media source at the given index of the playlist.
        Parameters:
        index - The index at which to add the source.
        mediaSource - The MediaSource to add.
      • addMediaSources

        void addMediaSources​(List<MediaSource> mediaSources)
        Adds a list of media sources to the end of the playlist.
        Parameters:
        mediaSources - The MediaSources to add.
      • addMediaSources

        void addMediaSources​(int index,
                             List<MediaSource> mediaSources)
        Adds a list of media sources at the given index of the playlist.
        Parameters:
        index - The index at which to add the media sources.
        mediaSources - The MediaSources to add.
      • setShuffleOrder

        void setShuffleOrder​(ShuffleOrder shuffleOrder)
        Sets the shuffle order.
        Parameters:
        shuffleOrder - The shuffle order.
      • setAudioAttributes

        void setAudioAttributes​(AudioAttributes audioAttributes,
                                boolean handleAudioFocus)
        Sets the attributes for audio playback, used by the underlying audio track. If not set, the default audio attributes will be used. They are suitable for general media playback.

        Setting the audio attributes during playback may introduce a short gap in audio output as the audio track is recreated. A new audio session id will also be generated.

        If tunneling is enabled by the track selector, the specified audio attributes will be ignored, but they will take effect if audio is later played without tunneling.

        If the device is running a build before platform API version 21, audio attributes cannot be set directly on the underlying audio track. In this case, the usage will be mapped onto an equivalent stream type using Util.getStreamTypeForAudioUsage(int).

        If audio focus should be handled, the AudioAttributes.usage must be C.USAGE_MEDIA or C.USAGE_GAME. Other usages will throw an IllegalArgumentException.

        Parameters:
        audioAttributes - The attributes to use for audio playback.
        handleAudioFocus - True if the player should handle audio focus, false otherwise.
      • getAudioSessionId

        int getAudioSessionId()
        Returns the audio session identifier, or C.AUDIO_SESSION_ID_UNSET if not set.
      • setAuxEffectInfo

        void setAuxEffectInfo​(AuxEffectInfo auxEffectInfo)
        Sets information on an auxiliary audio effect to attach to the underlying audio track.
      • clearAuxEffectInfo

        void clearAuxEffectInfo()
        Detaches any previously attached auxiliary audio effect from the underlying audio track.
      • setSkipSilenceEnabled

        void setSkipSilenceEnabled​(boolean skipSilenceEnabled)
        Sets whether skipping silences in the audio stream is enabled.
        Parameters:
        skipSilenceEnabled - Whether skipping silences in the audio stream is enabled.
      • getSkipSilenceEnabled

        boolean getSkipSilenceEnabled()
        Returns whether skipping silences in the audio stream is enabled.
      • setVideoFrameMetadataListener

        void setVideoFrameMetadataListener​(VideoFrameMetadataListener listener)
        Sets a listener to receive video frame metadata events.

        This method is intended to be called by the same component that sets the Surface onto which video will be rendered. If using ExoPlayer's standard UI components, this method should not be called directly from application code.

        Parameters:
        listener - The listener.
      • clearVideoFrameMetadataListener

        void clearVideoFrameMetadataListener​(VideoFrameMetadataListener listener)
        Clears the listener which receives video frame metadata events if it matches the one passed. Else does nothing.
        Parameters:
        listener - The listener to clear.
      • setCameraMotionListener

        void setCameraMotionListener​(CameraMotionListener listener)
        Sets a listener of camera motion events.
        Parameters:
        listener - The listener.
      • clearCameraMotionListener

        void clearCameraMotionListener​(CameraMotionListener listener)
        Clears the listener which receives camera motion events if it matches the one passed. Else does nothing.
        Parameters:
        listener - The listener to clear.
      • setSeekParameters

        void setSeekParameters​(@Nullable
                               SeekParameters seekParameters)
        Sets the parameters that control how seek operations are performed.
        Parameters:
        seekParameters - The seek parameters, or null to use the defaults.
      • setForegroundMode

        void setForegroundMode​(boolean foregroundMode)
        Sets whether the player is allowed to keep holding limited resources such as video decoders, even when in the idle state. By doing so, the player may be able to reduce latency when starting to play another piece of content for which the same resources are required.

        This mode should be used with caution, since holding limited resources may prevent other players of media components from acquiring them. It should only be enabled when both of the following conditions are true:

        • The application that owns the player is in the foreground.
        • The player is used in a way that may benefit from foreground mode. For this to be true, the same player instance must be used to play multiple pieces of content, and there must be gaps between the playbacks (i.e. Player.stop() is called to halt one playback, and prepare(com.google.android.exoplayer2.source.MediaSource) is called some time later to start a new one).

        Note that foreground mode is not useful for switching between content without gaps between the playbacks. For this use case Player.stop() does not need to be called, and simply calling prepare(com.google.android.exoplayer2.source.MediaSource) for the new media will cause limited resources to be retained even if foreground mode is not enabled.

        If foreground mode is enabled, it's the application's responsibility to disable it when the conditions described above no longer hold.

        Parameters:
        foregroundMode - Whether the player is allowed to keep limited resources even when in the idle state.
      • getPauseAtEndOfMediaItems

        boolean getPauseAtEndOfMediaItems()
        Returns whether the player pauses playback at the end of each media item.
        See Also:
        setPauseAtEndOfMediaItems(boolean)
      • getAudioFormat

        @Nullable
        Format getAudioFormat()
        Returns the audio format currently being played, or null if no audio is being played.
      • getVideoFormat

        @Nullable
        Format getVideoFormat()
        Returns the video format currently being played, or null if no video is being played.
      • getAudioDecoderCounters

        @Nullable
        DecoderCounters getAudioDecoderCounters()
        Returns DecoderCounters for audio, or null if no audio is being played.
      • getVideoDecoderCounters

        @Nullable
        DecoderCounters getVideoDecoderCounters()
        Returns DecoderCounters for video, or null if no video is being played.
      • setHandleAudioBecomingNoisy

        void setHandleAudioBecomingNoisy​(boolean handleAudioBecomingNoisy)
        Sets whether the player should pause automatically when audio is rerouted from a headset to device speakers. See the audio becoming noisy documentation for more information.
        Parameters:
        handleAudioBecomingNoisy - Whether the player should pause automatically when audio is rerouted from a headset to device speakers.
      • setWakeMode

        void setWakeMode​(@WakeMode
                         @com.google.android.exoplayer2.C.WakeMode int wakeMode)
        Sets how the player should keep the device awake for playback when the screen is off.

        Enabling this feature requires the Manifest.permission.WAKE_LOCK permission. It should be used together with a foreground Service for use cases where playback occurs and the screen is off (e.g. background audio playback). It is not useful when the screen will be kept on during playback (e.g. foreground video playback).

        When enabled, the locks (PowerManager.WakeLock / WifiManager.WifiLock) will be held whenever the player is in the Player.STATE_READY or Player.STATE_BUFFERING states with playWhenReady = true. The locks held depends on the specified C.WakeMode.

        Parameters:
        wakeMode - The C.WakeMode option to keep the device awake during playback.
      • setPriorityTaskManager

        void setPriorityTaskManager​(@Nullable
                                    PriorityTaskManager priorityTaskManager)
        Sets a PriorityTaskManager, or null to clear a previously set priority task manager.

        The priority C.PRIORITY_PLAYBACK will be set while the player is loading.

        Parameters:
        priorityTaskManager - The PriorityTaskManager, or null to clear a previously set priority task manager.
      • experimentalSetOffloadSchedulingEnabled

        void experimentalSetOffloadSchedulingEnabled​(boolean offloadSchedulingEnabled)
        Sets whether audio offload scheduling is enabled. If enabled, ExoPlayer's main loop will run as rarely as possible when playing an audio stream using audio offload.

        Only use this scheduling mode if the player is not displaying anything to the user. For example when the application is in the background, or the screen is off. The player state (including position) is rarely updated (roughly between every 10 seconds and 1 minute).

        While offload scheduling is enabled, player events may be delivered severely delayed and apps should not interact with the player. When returning to the foreground, disable offload scheduling and wait for ExoPlayer.AudioOffloadListener.onExperimentalOffloadSchedulingEnabledChanged(boolean) to be called with offloadSchedulingEnabled = false before interacting with the player.

        This mode should save significant power when the phone is playing offload audio with the screen off.

        This mode only has an effect when playing an audio track in offload mode, which requires all the following:

        The state where ExoPlayer main loop has been paused to save power during offload playback can be queried with experimentalIsSleepingForOffload().

        This method is experimental, and will be renamed or removed in a future release.

        Parameters:
        offloadSchedulingEnabled - Whether to enable offload scheduling.