@Override public void handleMessage(Message msg) { switch (msg.what) { case SHOW_PROGRESS: if (player != null && player.getPlaybackState() == ExoPlayer.STATE_READY && player.getPlayWhenReady() ) { long pos = player.getCurrentPosition(); eventEmitter.progressChanged(pos, player.getBufferedPercentage()); msg = obtainMessage(SHOW_PROGRESS); sendMessageDelayed(msg, 1000 - (pos % 1000)); } break; } }
private void startPlayback() { if (player != null) { switch (player.getPlaybackState()) { case ExoPlayer.STATE_IDLE: case ExoPlayer.STATE_ENDED: initializePlayer(); break; case ExoPlayer.STATE_BUFFERING: case ExoPlayer.STATE_READY: if (!player.getPlayWhenReady()) { setPlayWhenReady(true); } break; default: break; } } else { initializePlayer(); } if (!disableFocus) { setKeepScreenOn(true); } }
@Override public void attachPlayer(ExoPlayer player, EventListener eventListener, ViewGroup adUiViewGroup) { this.player = player; this.eventListener = eventListener; this.adUiViewGroup = adUiViewGroup; lastAdProgress = null; lastContentProgress = null; adDisplayContainer.setAdContainer(adUiViewGroup); player.addListener(this); if (adPlaybackState != null) { eventListener.onAdPlaybackState(adPlaybackState.copy()); if (imaPausedContent && player.getPlayWhenReady()) { adsManager.resume(); } } else { requestAds(); } }
/** * update the movie and ad playing position when players are released */ @Override protected void updateResumePosition() { super.updateResumePosition(); //keep track of movie player's position when activity resume back if (mTubiExoPlayer != null && playerUIController != null && mTubiExoPlayer.getPlaybackState() != ExoPlayer.STATE_IDLE) { int resumeWindow = mTubiExoPlayer.getCurrentWindowIndex(); long resumePosition = mTubiExoPlayer.isCurrentWindowSeekable() ? Math.max(0, mTubiExoPlayer.getCurrentPosition()) : C.TIME_UNSET; playerUIController.setMovieResumeInfo(resumeWindow, resumePosition); } //keep track of ad player's position when activity resume back, only keep track when current state is in AdPlayingState. if (fsmPlayer.getCurrentState() instanceof AdPlayingState && adPlayer != null && playerUIController != null && adPlayer.getPlaybackState() != ExoPlayer.STATE_IDLE) { int ad_resumeWindow = adPlayer.getCurrentWindowIndex(); long ad_resumePosition = adPlayer.isCurrentWindowSeekable() ? Math.max(0, adPlayer.getCurrentPosition()) : C.TIME_UNSET; playerUIController.setAdResumeInfo(ad_resumeWindow, ad_resumePosition); } }
private void seekOrSkip() { if (fsmPlayer == null) { return; } if (fsmPlayer instanceof FsmPlayer) { if (((FsmPlayer) fsmPlayer).getController() != null) { SimpleExoPlayer adPlayer = ((FsmPlayer) fsmPlayer).getController().getAdPlayer(); if (adPlayer != null && adPlayer.getPlaybackState() == ExoPlayer.STATE_BUFFERING) { long position = adPlayer.getCurrentPosition() + 1000 < adPlayer.getDuration() ? adPlayer.getCurrentPosition() + 1000 : adPlayer.getDuration(); adPlayer.seekTo(position); adPlayer.setPlayWhenReady(true); return; } } } }
/** * update the resume position of the main movice * * @param controller */ public static void updateMovieResumePosition(PlayerUIController controller) { if (controller == null) { return; } SimpleExoPlayer moviePlayer = controller.getContentPlayer(); if (moviePlayer != null && moviePlayer.getPlaybackState() != ExoPlayer.STATE_IDLE) { int resumeWindow = moviePlayer.getCurrentWindowIndex(); long resumePosition = moviePlayer.isCurrentWindowSeekable() ? Math.max(0, moviePlayer.getCurrentPosition()) : C.TIME_UNSET; controller.setMovieResumeInfo(resumeWindow, resumePosition); } }
@Override public void performWorkAndupdatePlayerUI(@Nullable FsmPlayer fsmPlayer, @NonNull PlayerUIController controller, @NonNull PlayerComponentController componentController, @NonNull MediaModel movieMedia, @Nullable AdMediaModel adMedia) { // doesn't need to do any UI work. if (isNull(fsmPlayer, controller, componentController, movieMedia, adMedia)) { return; } SimpleExoPlayer moviePlayer = controller.getContentPlayer(); // this mean, user jump out of the activity lifecycle in ReceivedAdState. if (moviePlayer!=null && moviePlayer.getPlaybackState() == ExoPlayer.STATE_IDLE) { fsmPlayer.transit(Input.ERROR); } }
@Override public final void onPlayerStateChanged(boolean playWhenReady, int playbackState) { Log.d(tag, "state [" + playWhenReady + ", " + playbackState + "]"); playerWasPrepared |= playbackState != ExoPlayer.STATE_IDLE; if (playbackState == ExoPlayer.STATE_ENDED || (playbackState == ExoPlayer.STATE_IDLE && playerWasPrepared)) { playerFinished = true; } boolean playing = playWhenReady && playbackState == ExoPlayer.STATE_READY; if (!this.playing && playing) { lastPlayingStartTimeMs = SystemClock.elapsedRealtime(); } else if (this.playing && !playing) { totalPlayingTimeMs += SystemClock.elapsedRealtime() - lastPlayingStartTimeMs; } this.playing = playing; }
@Override public void onPlayerStateChanged(boolean b, int i) { Log.e("ExoMedia", "onPlayerStateChanged " + b + i); if (i == ExoPlayer.STATE_READY) { if (!isPrepar) { isPrepar = true; iMediaCallback.onPrepared(this);//第一次初始化 } //缓冲好了 iMediaCallback.onInfo(this, MEDIA_INFO_BUFFERING_END, MEDIA_INFO_BUFFERING_END); mainThreadHandler.removeCallbacks(runnable); mainThreadHandler.post(runnable); } //播放完毕 if (i == ExoPlayer.STATE_ENDED) { mainThreadHandler.removeCallbacks(runnable); iMediaCallback.onCompletion(this); } if (i == ExoPlayer.STATE_BUFFERING) iMediaCallback.onInfo(this, MEDIA_INFO_BUFFERING_START, MEDIA_INFO_BUFFERING_START); }
@Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { switch (playbackState) { case ExoPlayer.STATE_ENDED: //Log.d(TAG, "player state -> STATE_ENDED"); mPlayer.stop(); break; case ExoPlayer.STATE_IDLE: //Log.d(TAG, "player state -> STATE_IDLE"); break; case ExoPlayer.STATE_READY: //Log.d(TAG, "player state -> STATE_READY"); break; case ExoPlayer.STATE_BUFFERING: // Log.d(TAG, "player state -> STATE_BUFFERING"); break; default: break; } }
@Override public void onClick(View v) { if (v == playView && player != null) { if (player.getPlaybackState() == ExoPlayer.STATE_ENDED) { player.seekTo(0); player.setPlayWhenReady(true); } else { player.setPlayWhenReady(!player.getPlayWhenReady()); } } else if (v == fullscreen) { Intent playerIntent = new Intent(getContext(), VideoPlayerActivity.class); playerIntent.putExtra("uri", uri); playerIntent.putExtra("position", player.getCurrentPosition()); playerIntent.putExtra("windowIndex", player.getCurrentWindowIndex()); playerIntent.putExtra("rotation", getWidth() > getHeight()); getContext().startActivity(playerIntent); disconnect(); playerInstance.startFullScreenMode(); } else if (v == videoView) { if (isConnected) { show(); } else { connect(); } } }
@Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { switch (playbackState) { case ExoPlayer.STATE_BUFFERING: case ExoPlayer.STATE_READY: replayImageButton.setVisibility(View.GONE); exoButtonsFrameLayout.setVisibility(View.VISIBLE); break; case ExoPlayer.STATE_ENDED: exoButtonsFrameLayout.setVisibility(View.GONE); replayImageButton.setVisibility(View.VISIBLE); break; default: break; } }
@Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { switch (playbackState) { case ExoPlayer.STATE_BUFFERING: replayImageButton.setVisibility(View.GONE); exoButtonsFrameLayout.setVisibility(View.VISIBLE); break; case ExoPlayer.STATE_READY: replayImageButton.setVisibility(View.GONE); exoButtonsFrameLayout.setVisibility(View.VISIBLE); break; case ExoPlayer.STATE_ENDED: exoButtonsFrameLayout.setVisibility(View.GONE); replayImageButton.setVisibility(View.VISIBLE); break; default: break; } }
@Override public void run() { int state = mediaPlayer.getPlaybackState(); if (state == ExoPlayer.STATE_READY) { long pos = mediaPlayer.getCurrentPosition(); float speed = mediaPlayer.getPlaybackParameters().speed; if (!notificationManager.startNotification()) { notificationManager.updateNotification(); } stateBuilder.setBufferedPosition(mediaPlayer.getBufferedPercentage()); int sessionState = mediaPlayer.getPlayWhenReady() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; mediaSession.setPlaybackState(stateBuilder.setState(sessionState, pos, speed).build()); if (mediaPlayer.getPlayWhenReady()) { progressHandler.postDelayed(progressRunnable, 1000 - (pos % 1000)); } } }
@Override public int getState() { if (exoPlayer == null) { return exoPlayerNullIsStopped ? PlaybackStateCompat.STATE_STOPPED : PlaybackStateCompat.STATE_NONE; } switch (exoPlayer.getPlaybackState()) { case ExoPlayer.STATE_IDLE: return PlaybackStateCompat.STATE_PAUSED; case ExoPlayer.STATE_BUFFERING: return PlaybackStateCompat.STATE_BUFFERING; case ExoPlayer.STATE_READY: return exoPlayer.getPlayWhenReady() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; case ExoPlayer.STATE_ENDED: return PlaybackStateCompat.STATE_PAUSED; default: return PlaybackStateCompat.STATE_NONE; } }
@Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { switch (playbackState) { case ExoPlayer.STATE_IDLE: case ExoPlayer.STATE_BUFFERING: case ExoPlayer.STATE_READY: if (callback != null) { callback.onPlaybackStatusChanged(getState()); } break; case ExoPlayer.STATE_ENDED: // The media player finished playing the current song. if (callback != null) { callback.onCompletion(); } break; } }
private String getPlayerStateString() { String text = "playWhenReady:" + player.getPlayWhenReady() + " playbackState:"; switch (player.getPlaybackState()) { case ExoPlayer.STATE_BUFFERING: text += "buffering"; break; case ExoPlayer.STATE_ENDED: text += "ended"; break; case ExoPlayer.STATE_IDLE: text += "idle"; break; case ExoPlayer.STATE_READY: text += "ready"; break; default: text += "unknown"; break; } return text; }
@Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { mBufferingStart = false; if (playbackState == ExoPlayer.STATE_READY && !mInitialized) { mInitialized = true; if (mSurfaceHolderGlueHost == null || mHasDisplay) { getCallback().onPreparedStateChanged(ExoPlayerAdapter.this); } } else if (playbackState == ExoPlayer.STATE_BUFFERING) { mBufferingStart = true; } else if (playbackState == ExoPlayer.STATE_ENDED) { getCallback().onPlayStateChanged(ExoPlayerAdapter.this); getCallback().onPlayCompleted(ExoPlayerAdapter.this); } notifyBufferingStartEnd(); }
@Override public void onPlayerStateChanged(final boolean playWhenReady, final int playbackState) { if (Log.logDEnabled()) { Log.d(TAG, "onPlayerStateChanged: " + playbackState); } if (mMediaPlayerListener != null) { switch (playbackState) { case ExoPlayer.STATE_READY: mLoadedMediaUri = mLoadingMediaUri; if (playWhenReady) { mMediaPlayerListener.onPlaybackStarted(); } else { mMediaPlayerListener.onPlaybackPaused(); } break; case ExoPlayer.STATE_ENDED: mMediaPlayerListener.onPlaybackFinished(); break; } } }
@NonNull private static State toState(@NonNull final ExoPlayerState exoPlayerState) { int exoplaybackState = exoPlayerState.getPlaybackState(); switch (exoplaybackState) { case ExoPlayer.STATE_IDLE: return State.IDLE; case ExoPlayer.STATE_BUFFERING: return State.BUFFERING; case ExoPlayer.STATE_READY: return exoPlayerState.getPlayWhenReady() ? State.PLAYING : State.PAUSED; case ExoPlayer.STATE_ENDED: return State.ENDED; default: throw new IllegalStateException("Unsupported Exoplayer state: " + exoplaybackState); } }
@Override public void run() { Looper.prepare(); LibvpxVideoRenderer videoRenderer = new LibvpxVideoRenderer(true, 0); DefaultTrackSelector trackSelector = new DefaultTrackSelector(); player = ExoPlayerFactory.newInstance(new Renderer[] {videoRenderer}, trackSelector); player.addListener(this); ExtractorMediaSource mediaSource = new ExtractorMediaSource( uri, new DefaultDataSourceFactory(context, "ExoPlayerExtVp9Test"), MatroskaExtractor.FACTORY, null, null); player.sendMessages(new ExoPlayer.ExoPlayerMessage(videoRenderer, LibvpxVideoRenderer.MSG_SET_OUTPUT_BUFFER_RENDERER, new VpxVideoSurfaceView(context))); player.prepare(mediaSource); player.setPlayWhenReady(true); Looper.loop(); }
@Override public int getState() { if (mExoPlayer == null) { return mExoPlayerNullIsStopped ? PlaybackStateCompat.STATE_STOPPED : PlaybackStateCompat.STATE_NONE; } switch (mExoPlayer.getPlaybackState()) { case ExoPlayer.STATE_IDLE: return PlaybackStateCompat.STATE_PAUSED; case ExoPlayer.STATE_BUFFERING: return PlaybackStateCompat.STATE_BUFFERING; case ExoPlayer.STATE_READY: return mExoPlayer.getPlayWhenReady() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; case ExoPlayer.STATE_ENDED: return PlaybackStateCompat.STATE_PAUSED; default: return PlaybackStateCompat.STATE_NONE; } }
@Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { switch (playbackState) { case ExoPlayer.STATE_IDLE: case ExoPlayer.STATE_BUFFERING: case ExoPlayer.STATE_READY: if (mCallback != null) { mCallback.onPlaybackStatusChanged(getState()); } break; case ExoPlayer.STATE_ENDED: // The media player finished playing the current song. if (mCallback != null) { mCallback.onCompletion(); } break; } }
protected void sendMessage(int renderType, int messageType, Object message, boolean blocking) { if (renderers.isEmpty()) { return; } List<ExoPlayer.ExoPlayerMessage> messages = new ArrayList<>(); for (Renderer renderer : renderers) { if (renderer.getTrackType() == renderType) { messages.add(new ExoPlayer.ExoPlayerMessage(renderer, messageType, message)); } } if (blocking) { player.blockingSendMessages(messages.toArray(new ExoPlayer.ExoPlayerMessage[messages.size()])); } else { player.sendMessages(messages.toArray(new ExoPlayer.ExoPlayerMessage[messages.size()])); } }
@Override public void run() { Looper.prepare(); LibvpxVideoRenderer videoRenderer = new LibvpxVideoRenderer(true, 0); DefaultTrackSelector trackSelector = new DefaultTrackSelector(); player = ExoPlayerFactory.newInstance(new Renderer[] {videoRenderer}, trackSelector); player.addListener(this); MediaSource mediaSource = new ExtractorMediaSource.Factory( new DefaultDataSourceFactory(context, "ExoPlayerExtVp9Test")) .setExtractorsFactory(MatroskaExtractor.FACTORY) .createMediaSource(uri); player.sendMessages(new ExoPlayer.ExoPlayerMessage(videoRenderer, LibvpxVideoRenderer.MSG_SET_OUTPUT_BUFFER_RENDERER, new VpxVideoSurfaceView(context))); player.prepare(mediaSource); player.setPlayWhenReady(true); Looper.loop(); }
@Override public void attachPlayer(ExoPlayer player, EventListener eventListener, ViewGroup adUiViewGroup) { this.player = player; this.eventListener = eventListener; this.adUiViewGroup = adUiViewGroup; lastAdProgress = null; lastContentProgress = null; adDisplayContainer.setAdContainer(adUiViewGroup); player.addListener(this); maybeNotifyAdError(); if (adPlaybackState != null) { // Pass the ad playback state to the player, and resume ads if necessary. eventListener.onAdPlaybackState(adPlaybackState.copy()); if (imaPausedContent && player.getPlayWhenReady()) { adsManager.resume(); } } else if (adsManager != null) { // Ads have loaded but the ads manager is not initialized. startAdPlayback(); } else { // Ads haven't loaded yet, so request them. requestAds(adUiViewGroup); } }
@Override public void prepareSource(final ExoPlayer player, boolean isTopLevelSource, Listener listener) { Assertions.checkArgument(isTopLevelSource); this.listener = listener; this.player = player; playerHandler = new Handler(); contentMediaSource.prepareSource(player, false, new Listener() { @Override public void onSourceInfoRefreshed(MediaSource source, Timeline timeline, Object manifest) { AdsMediaSource.this.onContentSourceInfoRefreshed(timeline, manifest); } }); mainHandler.post(new Runnable() { @Override public void run() { adsLoader.attachPlayer(player, componentListener, adUiViewGroup); } }); }
@Override public void prepareSource(ExoPlayer player, boolean isTopLevelSource, Listener listener) { this.listener = listener; if (mediaSources.length == 0) { listener.onSourceInfoRefreshed(this, Timeline.EMPTY, null); } else { for (int i = 0; i < mediaSources.length; i++) { if (!duplicateFlags[i]) { final int index = i; mediaSources[i].prepareSource(player, false, new Listener() { @Override public void onSourceInfoRefreshed(MediaSource source, Timeline timeline, Object manifest) { handleSourceInfoRefreshed(index, timeline, manifest); } }); } } } }
/************************ * ExoPlayer2.Listener ************************/ @Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { String text = "playWhenReady=" + playWhenReady + ", playbackState="; switch (playbackState) { case ExoPlayer.STATE_BUFFERING: text += "buffering"; break; case ExoPlayer.STATE_ENDED: text += "ended"; break; case ExoPlayer.STATE_IDLE: text += "idle"; break; case ExoPlayer.STATE_READY: onVideoLoaded(); text += "ready"; break; default: text += "unknown"; break; } Log.d(TAG, "onStateChanged: " + text); }
@Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { String text = "onStateChanged: playWhenReady=" + playWhenReady + ", playbackState="; switch (playbackState) { case ExoPlayer.STATE_IDLE: text += "idle"; eventEmitter.idle(); break; case ExoPlayer.STATE_BUFFERING: text += "buffering"; onBuffering(true); break; case ExoPlayer.STATE_READY: text += "ready"; eventEmitter.ready(); onBuffering(false); startProgressHandler(); videoLoaded(); break; case ExoPlayer.STATE_ENDED: text += "ended"; eventEmitter.end(); onStopPlayback(); break; default: text += "unknown"; break; } Log.d(TAG, text); }
@Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { if((playbackState == ExoPlayer.STATE_READY) && playWhenReady){ mStateBuilder.setState(PlaybackStateCompat.STATE_PLAYING, mExoPlayer.getCurrentPosition(), 1f); } else if((playbackState == ExoPlayer.STATE_READY)) { mStateBuilder.setState(PlaybackStateCompat.STATE_PAUSED, mExoPlayer.getCurrentPosition(), 1f); } mMediaSession.setPlaybackState(mStateBuilder.build()); }
private void maybeShowController(boolean isForced) { if (!useController || player == null) { return; } int playbackState = player.getPlaybackState(); boolean showIndefinitely = playbackState == ExoPlayer.STATE_IDLE || playbackState == ExoPlayer.STATE_ENDED || !player.getPlayWhenReady(); boolean wasShowingIndefinitely = controller.isVisible() && controller.getShowTimeoutMs() <= 0; controller.setShowTimeoutMs(showIndefinitely ? 0 : controllerShowTimeoutMs); if (isForced || showIndefinitely || wasShowingIndefinitely) { controller.show(); } }
private void updateProgress() { long position = player == null ? 0 : player.getCurrentPosition(); long duration = player == null ? 0 : player.getDuration(); setProgressBarMax((int) duration); if (!draggingSeekBar) { setProgressSeekTime(position, duration); setProgressBarValue(position); } long bufferedPosition = player == null ? 0 : player.getBufferedPosition(); setSecondaryProgressBarValue(bufferedPosition); playbackControlInterface.cancelRunnable(updateProgressAction); // Schedule an update if necessary. int playbackState = player == null ? ExoPlayer.STATE_IDLE : player.getPlaybackState(); if (playbackState != ExoPlayer.STATE_IDLE && playbackState != STATE_ENDED) { long delayMs; if (player.getPlayWhenReady() && playbackState == STATE_READY) { delayMs = 1000 - (position % 1000); if (delayMs < 200) { delayMs += 1000; } } else { delayMs = 1000; } if (playbackInterface.isActive()) { playbackControlInterface.postRunnable(updateProgressAction, delayMs); } } }
public void setPlaybackState(int playbackState) { this.playbackState = playbackState; switch (playbackState) { case STATE_READY: setDraggingSeekBar(false); break; case ExoPlayer.STATE_BUFFERING: case ExoPlayer.STATE_IDLE: //nothing to play case STATE_ENDED: //stream ended break; } notifyPropertyChanged(BR.playbackState); }
@Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { super.onPlayerStateChanged(playWhenReady, playbackState); //the last ad has finish playing. if (playbackState == ExoPlayer.STATE_ENDED && playWhenReady == true) { fsmPlayer.removePlayedAdAndTransitToNextState(); } }
private void stopAdandPlayerMovie(PlayerUIController controller, PlayerComponentController componentController, MediaModel movieMedia) { SimpleExoPlayer adPlayer = controller.getAdPlayer(); SimpleExoPlayer moviePlayer = controller.getContentPlayer(); //first remove the AdPlayer's listener and pause the player adPlayer.removeListener(componentController.getAdPlayingMonitor()); adPlayer.setPlayWhenReady(false); //then update the playerView with SimpleExoPlayer and Movie MediaModel TubiExoPlayerView tubiExoPlayerView = (TubiExoPlayerView) controller.getExoPlayerView(); tubiExoPlayerView.setPlayer(moviePlayer, componentController.getTubiPlaybackInterface()); tubiExoPlayerView.setMediaModel(movieMedia, false); //prepare the moviePlayer with data source and set it play boolean haveResumePosition = controller.getMovieResumePosition() != C.TIME_UNSET; if (moviePlayer.getPlaybackState() == ExoPlayer.STATE_IDLE) { moviePlayer.prepare(movieMedia.getMediaSource(), !haveResumePosition, false); } updatePlayerPosition(moviePlayer, controller); moviePlayer.setPlayWhenReady(true); hideVpaidNShowPlayer(controller); }
private static String getStateString(int state) { switch (state) { case ExoPlayer.STATE_BUFFERING: return "B"; case ExoPlayer.STATE_ENDED: return "E"; case ExoPlayer.STATE_IDLE: return "I"; case ExoPlayer.STATE_READY: return "R"; default: return "?"; } }