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 associated with Player.getApplicationLooper().

        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.
      • setMediaItems

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

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

      • 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 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.
      • stop

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

      • 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
                                  @com.google.android.exoplayer2.Player.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 media items is enabled.
        Parameters:
        shuffleModeEnabled - Whether shuffling is enabled.
      • getTrackSelectionParameters

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

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

        public 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 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 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 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 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 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 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 volume)
        This method is not supported and does nothing.
        Parameters:
        volume - 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 CueGroup getCurrentCues()
        This method is not supported and returns an empty CueGroup.
      • 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.