Chromecast SDK(Android) - 有没有办法检查播放设备上播放的媒体是否已播放完毕?

时间:2013-10-28 22:08:30

标签: android google-cast chromecast

是否有某种onFinished监听器?或者我们是否必须将当前流位置与轨道持续时间进行比较?

3 个答案:

答案 0 :(得分:11)

它不漂亮,但你可以打这个电话:

if (mRemoteMediaPlayer.getMediaStatus().getPlayerState() ==  MediaStatus.PLAYER_STATE_IDLE
    && mRemoteMediaPlayer.getMediaStatus().getIdleReason() ==  MediaStatus.IDLE_REASON_FINISHED) {
  ...

}

答案 1 :(得分:4)

炳, 目前没有回调注册此类活动。一种替代(并且不那么漂亮)的方法如下:在接收器上,监听媒体元素的“已结束”事件,并通过专用信道将事件发送回发送者。另一种方法是你的建议:检查位置与持续时间。当SDK逐渐升级到普遍可用性时,可以使用更好,更清晰的方法来实现您的目标。

答案 2 :(得分:1)

这是解决方案: 您只需要再使用一个变量mIdleReason

1)将mIdleReason初始化为

public int mIdleReason=MediaStatus.IDLE_REASON_NONE;

2)方法loadMedia

的更新值
 public void loadMedia(String url, MediaMetadata movieMetadata, CastSession castSession, boolean autoPlay, long position) {
        if (castSession == null || !castSession.isConnected()) {
            return;
        }
        MediaInfo mediaInfo = new MediaInfo.Builder(url)
                .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
                .setContentType("videos/m3u8")
                .setMetadata(movieMetadata)
                .build();
        mRemoteMediaClient = castSession.getRemoteMediaClient();
        mRemoteMediaClient.addListener(mRemoteMediaClientListener);
        mRemoteMediaClient.load(mediaInfo, autoPlay, position);

        mIdleReason = MediaStatus.IDLE_REASON_NONE;
    }

3)onStatusUpdate更新值:

 private RemoteMediaClient.Listener mRemoteMediaClientListener = new RemoteMediaClient.Listener() {
            @Override
            public void onStatusUpdated() {
                if (mRemoteMediaClient == null || mediaPlayerListener == null) {
                    return;
                }
                MediaStatus mediaStatus = mRemoteMediaClient.getMediaStatus();

                if (mediaStatus != null) {
                    int playerStatus = mediaStatus.getPlayerState();
                    Log.d("PlayerState", "onStatusUpdated() called, progress= "+mSeekBar.getProgress() +", stream duration= "+ mRemoteMediaClient.getStreamDuration()+" mSeekBar.getProgress() == mRemoteMediaClient.getStreamDuration()="+(mSeekBar.getProgress() == mRemoteMediaClient.getStreamDuration()));
                    Log.d("PlayerState", "onStatusUpdated() called playerStatus="+playerStatus+", idleReason="+mediaStatus.getIdleReason());

 if (playerStatus == MediaStatus.PLAYER_STATE_PLAYING) {
                        mediaPlayerListener.playing();
                        mIdleReason = MediaStatus.IDLE_REASON_FINISHED;
                    } else if (playerStatus == MediaStatus.PLAYER_STATE_BUFFERING) {
                        mediaPlayerListener.buffering();
                        mIdleReason = MediaStatus.IDLE_REASON_FINISHED;
                    } else if (playerStatus == MediaStatus.PLAYER_STATE_PAUSED) {
                        mediaPlayerListener.paused();
                    } else if (playerStatus == MediaStatus.IDLE_REASON_INTERRUPTED) {
                        mediaPlayerListener.error();
                    } else if (playerStatus == MediaStatus.IDLE_REASON_ERROR) {
                        mediaPlayerListener.error();
                    }else if(playerStatus == MediaStatus.PLAYER_STATE_IDLE && mediaStatus.getIdleReason() == MediaStatus.IDLE_REASON_FINISHED&& mIdleReason == MediaStatus.IDLE_REASON_FINISHED){
                        mediaPlayerListener.played();
                    }
                }
            }

            @Override
            public void onMetadataUpdated() {
                Log.d("", "onMetadataUpdated: ");
            }

            @Override
            public void onQueueStatusUpdated() {
                Log.d("", "onQueueStatusUpdated: ");
            }

            @Override
            public void onPreloadStatusUpdated() {
                Log.d("", "onPreloadStatusUpdated: ");
            }

            @Override
            public void onSendingRemoteMediaRequest() {
                Log.d("", "onSendingRemoteMediaRequest: ");
            }

            @Override
            public void onAdBreakStatusUpdated() {
                Log.d("", "onAdBreakStatusUpdated: ");
            }
        };