Class CastPlayer

  • All Implemented Interfaces:
    Player

    public final class CastPlayer
    extends BasePlayer
    Player implementation that communicates with a Cast receiver app.

    The behavior of this class depends on the underlying Cast session, which is obtained from the injected CastContext. To keep track of the session, isCastSessionAvailable() can be queried and SessionAvailabilityListener can be implemented and attached to the player.

    If no session is available, the player state will remain unchanged and calls to methods that alter it will be ignored. Querying the player state is possible even when no session is available, in which case, the last observed receiver app state is reported.

    Methods should be called on the application's main thread.

    • Method Detail

      • getItem

        @Nullable
        public com.google.android.gms.cast.MediaQueueItem getItem​(int periodId)
        Returns the item that corresponds to the period with the given id, or null if no media queue or period with id periodId exist.
        Parameters:
        periodId - The id of the period (getCurrentTimeline()) that corresponds to the item to get.
        Returns:
        The item that corresponds to the period with the given id, or null if no media queue or period with id periodId exist.
      • isCastSessionAvailable

        public boolean isCastSessionAvailable()
        Returns whether a cast session is available.
      • setSessionAvailabilityListener

        public void setSessionAvailabilityListener​(@Nullable
                                                   SessionAvailabilityListener listener)
        Sets a listener for updates on the cast session availability.
        Parameters:
        listener - The SessionAvailabilityListener, or null to clear the listener.
      • getApplicationLooper

        public 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.
      • addListener

        public 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 that was used to construct the player. However, if the thread used to construct the player does not have a Looper, then the listener will be called on the main thread.

        Parameters:
        listener - The listener to register.
      • addListener

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

        The listener's methods will be called on the thread that was used to construct the player. However, if the thread used to construct the player does not have a Looper, then the listener will be called on the main thread.

        Parameters:
        listener - The listener to register.
      • removeListener

        public 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.
      • removeListener

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

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

        public 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 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 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 void prepare()
        Description copied from interface: Player
        Prepares the player.
      • setPlayWhenReady

        public 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.
      • seekTo

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

        public void stop​(boolean reset)
      • release

        public 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.
      • setRepeatMode

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

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

        public TrackSelectionArray getCurrentTrackSelections()
        Description copied from interface: Player
        Returns the current track selections.

        A concrete implementation may include null elements if it has a fixed number of renderer components, wishes to report a TrackSelection for each of them, and has one or more renderer components that is not assigned any selected tracks.

        See Also:
        Player.Listener.onTracksChanged(TrackGroupArray, TrackSelectionArray)
      • setPlaylistMetadata

        public void setPlaylistMetadata​(MediaMetadata mediaMetadata)
        This method is not supported and does nothing.
      • getCurrentPeriodIndex

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

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

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

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

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

        public 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 windows.
      • isPlayingAd

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

        public 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 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.
      • isLoading

        public 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)
      • getContentPosition

        public 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 long getContentBufferedPosition()
        Description copied from interface: Player
        If Player.isPlayingAd() returns true, returns an estimate of the content position in the current content window 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().
      • setVolume

        public void setVolume​(float audioVolume)
        This method is not supported and does nothing.
        Parameters:
        audioVolume - Linear output gain to apply to all audio channels.
      • getVolume

        public float getVolume()
        This method is not supported and returns 1.
        Returns:
        The linear gain applied to all audio channels.
      • clearVideoSurface

        public void clearVideoSurface()
        This method is not supported and does nothing.
      • clearVideoSurface

        public void clearVideoSurface​(@Nullable
                                      Surface surface)
        This method is not supported and does nothing.
        Parameters:
        surface - The surface to clear.
      • setVideoSurface

        public void setVideoSurface​(@Nullable
                                    Surface surface)
        This method is not supported and does nothing.
        Parameters:
        surface - The Surface.
      • setVideoSurfaceHolder

        public void setVideoSurfaceHolder​(@Nullable
                                          SurfaceHolder surfaceHolder)
        This method is not supported and does nothing.
        Parameters:
        surfaceHolder - The surface holder.
      • clearVideoSurfaceHolder

        public void clearVideoSurfaceHolder​(@Nullable
                                            SurfaceHolder surfaceHolder)
        This method is not supported and does nothing.
        Parameters:
        surfaceHolder - The surface holder to clear.
      • setVideoSurfaceView

        public void setVideoSurfaceView​(@Nullable
                                        SurfaceView surfaceView)
        This method is not supported and does nothing.
        Parameters:
        surfaceView - The surface view.
      • clearVideoSurfaceView

        public void clearVideoSurfaceView​(@Nullable
                                          SurfaceView surfaceView)
        This method is not supported and does nothing.
        Parameters:
        surfaceView - The texture view to clear.
      • setVideoTextureView

        public void setVideoTextureView​(@Nullable
                                        TextureView textureView)
        This method is not supported and does nothing.
        Parameters:
        textureView - The texture view.
      • clearVideoTextureView

        public void clearVideoTextureView​(@Nullable
                                          TextureView textureView)
        This method is not supported and does nothing.
        Parameters:
        textureView - The texture view to clear.
      • getCurrentCues

        public ImmutableList<Cue> getCurrentCues()
        This method is not supported and returns an empty list.
      • getDeviceVolume

        public int getDeviceVolume()
        This method is not supported and always returns 0.
      • isDeviceMuted

        public boolean isDeviceMuted()
        This method is not supported and always returns false.
      • setDeviceVolume

        public void setDeviceVolume​(int volume)
        This method is not supported and does nothing.
        Parameters:
        volume - The volume to set.
      • increaseDeviceVolume

        public void increaseDeviceVolume()
        This method is not supported and does nothing.
      • decreaseDeviceVolume

        public void decreaseDeviceVolume()
        This method is not supported and does nothing.
      • setDeviceMuted

        public void setDeviceMuted​(boolean muted)
        This method is not supported and does nothing.