Class SimpleBasePlayer

  • All Implemented Interfaces:
    Player
    Direct Known Subclasses:
    LegacyMediaPlayerWrapper

    public abstract class SimpleBasePlayer
    extends BasePlayer
    A base implementation for Player that reduces the number of methods to implement to a minimum.

    Implementation notes:

    This base class handles various aspects of the player implementation to simplify the subclass:
    • The SimpleBasePlayer.State can only be created with allowed combinations of state values, avoiding any invalid player states.
    • Only functionality that is declared as available needs to be implemented. Other methods are automatically ignored.
    • Listener handling and informing listeners of state changes is handled automatically.
    • The base class provides a framework for asynchronous handling of method calls. It changes the visible playback state immediately to the most likely outcome to ensure the user-visible state changes look like synchronous operations. The state is then updated again once the asynchronous method calls have been fully handled.
    • Constructor Detail

      • SimpleBasePlayer

        protected SimpleBasePlayer​(Looper applicationLooper)
        Creates the base class.
        Parameters:
        applicationLooper - The Looper that must be used for all calls to the player and that is used to call listeners on.
      • SimpleBasePlayer

        protected SimpleBasePlayer​(Looper applicationLooper,
                                   Clock clock)
        Creates the base class.
        Parameters:
        applicationLooper - The Looper that must be used for all calls to the player and that is used to call listeners on.
        clock - The Clock that will be used by the player.
    • Method Detail

      • addListener

        public final void addListener​(Player.Listener listener)
        Description copied from interface: Player
        Registers a listener to receive all events from the player.

        The listener's methods will be called on the thread associated with Player.getApplicationLooper().

        Parameters:
        listener - The listener to register.
      • removeListener

        public final void removeListener​(Player.Listener listener)
        Description copied from interface: Player
        Unregister a listener registered through Player.addListener(Listener). The listener will no longer receive events.
        Parameters:
        listener - The listener to unregister.
      • getApplicationLooper

        public final Looper getApplicationLooper()
        Description copied from interface: Player
        Returns the Looper associated with the application thread that's used to access the player and on which player events are received.
      • setPlayWhenReady

        public final void setPlayWhenReady​(boolean playWhenReady)
        Description copied from interface: Player
        Sets whether playback should proceed when Player.getPlaybackState() == Player.STATE_READY.

        If the player is already in the ready state then this method pauses and resumes playback.

        Parameters:
        playWhenReady - Whether playback should proceed when ready.
      • setMediaItems

        public final void setMediaItems​(List<MediaItem> mediaItems,
                                        int startIndex,
                                        long startPositionMs)
        Description copied from interface: Player
        Clears the playlist and adds the specified MediaItems.
        Parameters:
        mediaItems - The new MediaItems.
        startIndex - The MediaItem 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 MediaItem is used. In any case, if startIndex is set to C.INDEX_UNSET, this parameter is ignored and the position is not reset at all.
      • addMediaItems

        public final void addMediaItems​(int index,
                                        List<MediaItem> mediaItems)
        Description copied from interface: Player
        Adds a list of media items at the given index of the playlist.
        Parameters:
        index - The index at which to add the media items. If the index is larger than the size of the playlist, the media items are added to the end of the playlist.
        mediaItems - The MediaItems to add.
      • moveMediaItems

        public final void moveMediaItems​(int fromIndex,
                                         int toIndex,
                                         int newIndex)
        Description copied from interface: Player
        Moves the media item range to the new index.
        Parameters:
        fromIndex - The start of the range to move.
        toIndex - The first item not to be included in the range (exclusive).
        newIndex - The new index of the first media item of the range. If the new index is larger than the size of the remaining playlist after removing the range, the range is moved to the end of the playlist.
      • removeMediaItems

        public final void removeMediaItems​(int fromIndex,
                                           int toIndex)
        Description copied from interface: Player
        Removes a range of media items from the playlist.
        Parameters:
        fromIndex - The index at which to start removing media items.
        toIndex - The index of the first item to be kept (exclusive). If the index is larger than the size of the playlist, media items to the end of the playlist are removed.
      • prepare

        public final void prepare()
        Description copied from interface: Player
        Prepares the player.

        This will move the player out of idle state and the player will start loading media and acquire resources needed for playback.

      • setRepeatMode

        public final void setRepeatMode​(int repeatMode)
        Description copied from interface: Player
        Sets the Player.RepeatMode to be used for playback.
        Parameters:
        repeatMode - The repeat mode.
      • setShuffleModeEnabled

        public final void setShuffleModeEnabled​(boolean shuffleModeEnabled)
        Description copied from interface: Player
        Sets whether shuffling of media items is enabled.
        Parameters:
        shuffleModeEnabled - Whether shuffling is enabled.
      • isLoading

        public final boolean isLoading()
        Description copied from interface: Player
        Whether the player is currently loading the source.
        Returns:
        Whether the player is currently loading the source.
        See Also:
        Player.Listener.onIsLoadingChanged(boolean)
      • seekTo

        public final void seekTo​(int mediaItemIndex,
                                 long positionMs)
        Description copied from interface: Player
        Seeks to a position specified in milliseconds in the specified MediaItem.
        Parameters:
        mediaItemIndex - The index of the MediaItem.
        positionMs - The seek position in the specified MediaItem, or C.TIME_UNSET to seek to the media item's default position.
      • setPlaybackParameters

        public final void setPlaybackParameters​(PlaybackParameters playbackParameters)
        Description copied from interface: Player
        Attempts to set the playback parameters. Passing PlaybackParameters.DEFAULT resets the player to the default, which means there is no speed or pitch adjustment.

        Playback parameters changes may cause the player to buffer. Player.Listener.onPlaybackParametersChanged(PlaybackParameters) will be called whenever the currently active playback parameters change.

        Parameters:
        playbackParameters - The playback parameters.
      • stop

        public final void stop()
        Description copied from interface: Player
        Stops playback without resetting the playlist. Use Player.pause() rather than this method if the intention is to pause playback.

        Calling this method will cause the playback state to transition to Player.STATE_IDLE and the player will release the loaded media and resources required for playback. The player instance can still be used by calling Player.prepare() again, and Player.release() must still be called on the player if it's no longer required.

        Calling this method does not clear the playlist, reset the playback position or the playback error.

      • stop

        public final void stop​(boolean reset)
      • release

        public final void release()
        Description copied from interface: Player
        Releases the player. This method must be called when the player is no longer required. The player must not be used after calling this method.
      • getTrackSelectionParameters

        public final TrackSelectionParameters getTrackSelectionParameters()
        Description copied from interface: Player
        Returns the parameters constraining the track selection.
        See Also:
        }
      • setTrackSelectionParameters

        public final void setTrackSelectionParameters​(TrackSelectionParameters parameters)
        Description copied from interface: Player
        Sets the parameters constraining the track selection.

        Unsupported parameters will be silently ignored.

        Use Player.getTrackSelectionParameters() to retrieve the current parameters. For example, the following snippet restricts video to SD whilst keep other track selection parameters unchanged:

        
         player.setTrackSelectionParameters(
           player.getTrackSelectionParameters()
                 .buildUpon()
                 .setMaxVideoSizeSd()
                 .build())
         
      • setPlaylistMetadata

        public final void setPlaylistMetadata​(MediaMetadata mediaMetadata)
        Description copied from interface: Player
        Sets the playlist MediaMetadata.
      • getCurrentPeriodIndex

        public final int getCurrentPeriodIndex()
        Description copied from interface: Player
        Returns the index of the period currently being played.
      • getCurrentMediaItemIndex

        public final int getCurrentMediaItemIndex()
        Description copied from interface: Player
        Returns the index of the current MediaItem in the timeline, or the prospective index if the current timeline is empty.
      • getDuration

        public final long getDuration()
        Description copied from interface: Player
        Returns the duration of the current content or ad in milliseconds, or C.TIME_UNSET if the duration is not known.
      • getCurrentPosition

        public final long getCurrentPosition()
        Description copied from interface: Player
        Returns the playback position in the current content or ad, in milliseconds, or the prospective position in milliseconds if the current timeline is empty.
      • getBufferedPosition

        public final long getBufferedPosition()
        Description copied from interface: Player
        Returns an estimate of the position in the current content or ad up to which data is buffered, in milliseconds.
      • getTotalBufferedDuration

        public final long getTotalBufferedDuration()
        Description copied from interface: Player
        Returns an estimate of the total buffered duration from the current position, in milliseconds. This includes pre-buffered data for subsequent ads and media items.
      • isPlayingAd

        public final boolean isPlayingAd()
        Description copied from interface: Player
        Returns whether the player is currently playing an ad.
      • getCurrentAdGroupIndex

        public final int getCurrentAdGroupIndex()
        Description copied from interface: Player
        If Player.isPlayingAd() returns true, returns the index of the ad group in the period currently being played. Returns C.INDEX_UNSET otherwise.
      • getCurrentAdIndexInAdGroup

        public final int getCurrentAdIndexInAdGroup()
        Description copied from interface: Player
        If Player.isPlayingAd() returns true, returns the index of the ad in its ad group. Returns C.INDEX_UNSET otherwise.
      • getContentPosition

        public final long getContentPosition()
        Description copied from interface: Player
        If Player.isPlayingAd() returns true, returns the content position that will be played once all ads in the ad group have finished playing, in milliseconds. If there is no ad playing, the returned position is the same as that returned by Player.getCurrentPosition().
      • getContentBufferedPosition

        public final long getContentBufferedPosition()
        Description copied from interface: Player
        If Player.isPlayingAd() returns true, returns an estimate of the content position in the current content up to which data is buffered, in milliseconds. If there is no ad playing, the returned position is the same as that returned by Player.getBufferedPosition().
      • getAudioAttributes

        public final AudioAttributes getAudioAttributes()
        Description copied from interface: Player
        Returns the attributes for audio playback.
      • setVolume

        public final void setVolume​(float volume)
        Description copied from interface: Player
        Sets the audio volume, valid values are between 0 (silence) and 1 (unity gain, signal unchanged), inclusive.
        Parameters:
        volume - Linear output gain to apply to all audio channels.
      • getVolume

        public final float getVolume()
        Description copied from interface: Player
        Returns the audio volume, with 0 being silence and 1 being unity gain (signal unchanged).
        Returns:
        The linear gain applied to all audio channels.
      • clearVideoSurface

        public final void clearVideoSurface​(@Nullable
                                            Surface surface)
        Description copied from interface: Player
        Clears the Surface onto which video is being rendered if it matches the one passed. Else does nothing.
        Parameters:
        surface - The surface to clear.
      • setVideoSurfaceHolder

        public final void setVideoSurfaceHolder​(@Nullable
                                                SurfaceHolder surfaceHolder)
        Description copied from interface: Player
        Sets the SurfaceHolder that holds the Surface onto which video will be rendered. The player will track the lifecycle of the surface automatically.

        The thread that calls the SurfaceHolder.Callback methods must be the thread associated with Player.getApplicationLooper().

        Parameters:
        surfaceHolder - The surface holder.
      • clearVideoSurfaceHolder

        public final void clearVideoSurfaceHolder​(@Nullable
                                                  SurfaceHolder surfaceHolder)
        Description copied from interface: Player
        Clears the SurfaceHolder that holds the Surface onto which video is being rendered if it matches the one passed. Else does nothing.
        Parameters:
        surfaceHolder - The surface holder to clear.
      • setVideoSurfaceView

        public final void setVideoSurfaceView​(@Nullable
                                              SurfaceView surfaceView)
        Description copied from interface: Player
        Sets the SurfaceView onto which video will be rendered. The player will track the lifecycle of the surface automatically.

        The thread that calls the SurfaceHolder.Callback methods must be the thread associated with Player.getApplicationLooper().

        Parameters:
        surfaceView - The surface view.
      • clearVideoSurfaceView

        public final void clearVideoSurfaceView​(@Nullable
                                                SurfaceView surfaceView)
        Description copied from interface: Player
        Clears the SurfaceView onto which video is being rendered if it matches the one passed. Else does nothing.
        Parameters:
        surfaceView - The texture view to clear.
      • clearVideoTextureView

        public final void clearVideoTextureView​(@Nullable
                                                TextureView textureView)
        Description copied from interface: Player
        Clears the TextureView onto which video is being rendered if it matches the one passed. Else does nothing.
        Parameters:
        textureView - The texture view to clear.
      • getCurrentCues

        public final CueGroup getCurrentCues()
        Description copied from interface: Player
        Returns the current CueGroup.
      • getDeviceInfo

        public final DeviceInfo getDeviceInfo()
        Description copied from interface: Player
        Gets the device information.
      • isDeviceMuted

        public final boolean isDeviceMuted()
        Description copied from interface: Player
        Gets whether the device is muted or not.
      • setDeviceVolume

        public final void setDeviceVolume​(int volume)
        Description copied from interface: Player
        Sets the volume of the device.
        Parameters:
        volume - The volume to set.
      • increaseDeviceVolume

        public final void increaseDeviceVolume()
        Description copied from interface: Player
        Increases the volume of the device.
      • decreaseDeviceVolume

        public final void decreaseDeviceVolume()
        Description copied from interface: Player
        Decreases the volume of the device.
      • setDeviceMuted

        public final void setDeviceMuted​(boolean muted)
        Description copied from interface: Player
        Sets the mute state of the device.
      • invalidateState

        protected final void invalidateState()
        Invalidates the current state.

        Triggers a call to getState() and informs listeners if the state changed.

        Note that this may not have an immediate effect while there are still player methods being handled asynchronously. The state will be invalidated automatically once these pending synchronous operations are finished and there is no need to call this method again.

      • getState

        @ForOverride
        protected abstract SimpleBasePlayer.State getState()
        Returns the current SimpleBasePlayer.State of the player.

        The SimpleBasePlayer.State should include all available commands indicating which player methods are allowed to be called.

        Note that this method won't be called while asynchronous handling of player methods is in progress. This means that the implementation doesn't need to handle state changes caused by these asynchronous operations until they are done and can return the currently known state directly. The placeholder state used while these asynchronous operations are in progress can be customized by overriding getPlaceholderState(State) if required.

      • getPlaceholderState

        @ForOverride
        protected SimpleBasePlayer.State getPlaceholderState​(SimpleBasePlayer.State suggestedPlaceholderState)
        Returns the placeholder state used while a player method is handled asynchronously.

        The suggestedPlaceholderState already contains the most likely state update, for example setting SimpleBasePlayer.State.playWhenReady to true if player.setPlayWhenReady(true) is called, and an implementations only needs to override this method if it can determine a more accurate placeholder state.

        Parameters:
        suggestedPlaceholderState - The suggested placeholder SimpleBasePlayer.State, including the most likely outcome of handling all pending asynchronous operations.
        Returns:
        The placeholder SimpleBasePlayer.State to use while asynchronous operations are pending.