com.google.android.exoplayer2.ExoPlayer Java Examples

The following examples show how to use com.google.android.exoplayer2.ExoPlayer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java    From ExoplayerExample with The Unlicense 6 votes vote down vote up
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
    Log.i(TAG,"onPlayerStateChanged: playWhenReady = "+String.valueOf(playWhenReady)
            +" playbackState = "+playbackState);
    switch (playbackState){
        case ExoPlayer.STATE_ENDED:
            Log.i(TAG,"Playback ended!");
            //Stop playback and return to start position
            setPlayPause(false);
            exoPlayer.seekTo(0);
            break;
        case ExoPlayer.STATE_READY:
            Log.i(TAG,"ExoPlayer ready! pos: "+exoPlayer.getCurrentPosition()
                    +" max: "+stringForTime((int)exoPlayer.getDuration()));
            setProgress();
            break;
        case ExoPlayer.STATE_BUFFERING:
            Log.i(TAG,"Playback buffering!");
            break;
        case ExoPlayer.STATE_IDLE:
            Log.i(TAG,"ExoPlayer idle!");
            break;
    }
}
 
Example #2
Source File: SsMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void prepareSourceInternal(
    ExoPlayer player,
    boolean isTopLevelSource,
    @Nullable TransferListener mediaTransferListener) {
  this.mediaTransferListener = mediaTransferListener;
  if (sideloadedManifest) {
    manifestLoaderErrorThrower = new LoaderErrorThrower.Dummy();
    processManifest();
  } else {
    manifestDataSource = manifestDataSourceFactory.createDataSource();
    manifestLoader = new Loader("Loader:Manifest");
    manifestLoaderErrorThrower = manifestLoader;
    manifestRefreshHandler = new Handler();
    startLoadingManifest();
  }
}
 
Example #3
Source File: AdPlayingMonitor.java    From TubiPlayer with MIT License 6 votes vote down vote up
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;
            }
        }
    }
}
 
Example #4
Source File: ReceiveAdState.java    From TubiPlayer with MIT License 6 votes vote down vote up
@Override
public void performWorkAndUpdatePlayerUI(@NonNull FsmPlayer fsmPlayer) {
    super.performWorkAndUpdatePlayerUI(fsmPlayer);

    // doesn't need to do any UI work.
    if (isNull(fsmPlayer)) {
        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);
    }

}
 
Example #5
Source File: DebugTextViewHelper.java    From K-Sonic with MIT License 6 votes vote down vote up
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;
}
 
Example #6
Source File: SsMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void prepareSourceInternal(
    ExoPlayer player,
    boolean isTopLevelSource,
    @Nullable TransferListener mediaTransferListener) {
  this.mediaTransferListener = mediaTransferListener;
  if (sideloadedManifest) {
    manifestLoaderErrorThrower = new LoaderErrorThrower.Dummy();
    processManifest();
  } else {
    manifestDataSource = manifestDataSourceFactory.createDataSource();
    manifestLoader = new Loader("Loader:Manifest");
    manifestLoaderErrorThrower = manifestLoader;
    manifestRefreshHandler = new Handler();
    startLoadingManifest();
  }
}
 
Example #7
Source File: MediaController.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onStateChanged(boolean playWhenReady, int playbackState) {
    if (tag != playerNum) {
        return;
    }
    if (playbackState == ExoPlayer.STATE_ENDED || (playbackState == ExoPlayer.STATE_IDLE || playbackState == ExoPlayer.STATE_BUFFERING) && playWhenReady && messageObject.audioProgress >= 0.999f) {
        if (!playlist.isEmpty() && (playlist.size() > 1 || !messageObject.isVoice())) {
            playNextMessageWithoutOrder(true);
        } else {
            cleanupPlayer(true, true, messageObject != null && messageObject.isVoice(), false);
        }
    } else if (seekToProgressPending != 0 && (playbackState == ExoPlayer.STATE_READY || playbackState == ExoPlayer.STATE_IDLE)) {
        int seekTo = (int) (audioPlayer.getDuration() * seekToProgressPending);
        audioPlayer.seekTo(seekTo);
        lastProgress = seekTo;
        seekToProgressPending = 0;
    }
}
 
Example #8
Source File: ExoMedia.java    From QSVideoPlayer with Apache License 2.0 6 votes vote down vote up
@Override
public void onPlayerStateChanged(boolean b, int i) {
    Log.e("ExoMedia", "onPlayerStateChanged " + b + i);

    if (i == ExoPlayer.STATE_READY) {
        //缓冲好了
        iMediaCallback.onInfo(this, MEDIA_INFO_BUFFERING_END, MEDIA_INFO_BUFFERING_END);
        mainThreadHandler.removeCallbacks(runnable);
        mainThreadHandler.post(runnable);
        if (!isPrepar) {
            isPrepar = true;
            iMediaCallback.onPrepared(this);//第一次初始化
        }

    }
    //播放完毕
    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);

}
 
Example #9
Source File: WebPlayerView.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onStateChanged(boolean playWhenReady, int playbackState) {
    if (playbackState != ExoPlayer.STATE_BUFFERING) {
        if (videoPlayer.getDuration() != C.TIME_UNSET) {
            controlsView.setDuration((int) (videoPlayer.getDuration() / 1000));
        } else {
            controlsView.setDuration(0);
        }
    }
    if (playbackState != ExoPlayer.STATE_ENDED && playbackState != ExoPlayer.STATE_IDLE && videoPlayer.isPlaying()) {
        delegate.onPlayStateChanged(this, true);
    } else {
        delegate.onPlayStateChanged(this, false);
    }
    if (videoPlayer.isPlaying() && playbackState != ExoPlayer.STATE_ENDED) {
        updatePlayButton();
    } else {
        if (playbackState == ExoPlayer.STATE_ENDED) {
            isCompleted = true;
            videoPlayer.pause();
            videoPlayer.seekTo(0);
            updatePlayButton();
            controlsView.show(true, true);
        }
    }
}
 
Example #10
Source File: WebPlayerView.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onStateChanged(boolean playWhenReady, int playbackState) {
    if (playbackState != ExoPlayer.STATE_BUFFERING) {
        if (videoPlayer.getDuration() != C.TIME_UNSET) {
            controlsView.setDuration((int) (videoPlayer.getDuration() / 1000));
        } else {
            controlsView.setDuration(0);
        }
    }
    if (playbackState != ExoPlayer.STATE_ENDED && playbackState != ExoPlayer.STATE_IDLE && videoPlayer.isPlaying()) {
        delegate.onPlayStateChanged(this, true);
    } else {
        delegate.onPlayStateChanged(this, false);
    }
    if (videoPlayer.isPlaying() && playbackState != ExoPlayer.STATE_ENDED) {
        updatePlayButton();
    } else {
        if (playbackState == ExoPlayer.STATE_ENDED) {
            isCompleted = true;
            videoPlayer.pause();
            videoPlayer.seekTo(0);
            updatePlayButton();
            controlsView.show(true, true);
        }
    }
}
 
Example #11
Source File: WebPlayerView.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onStateChanged(boolean playWhenReady, int playbackState) {
    if (playbackState != ExoPlayer.STATE_BUFFERING) {
        if (videoPlayer.getDuration() != C.TIME_UNSET) {
            controlsView.setDuration((int) (videoPlayer.getDuration() / 1000));
        } else {
            controlsView.setDuration(0);
        }
    }
    if (playbackState != ExoPlayer.STATE_ENDED && playbackState != ExoPlayer.STATE_IDLE && videoPlayer.isPlaying()) {
        delegate.onPlayStateChanged(this, true);
    } else {
        delegate.onPlayStateChanged(this, false);
    }
    if (videoPlayer.isPlaying() && playbackState != ExoPlayer.STATE_ENDED) {
        updatePlayButton();
    } else {
        if (playbackState == ExoPlayer.STATE_ENDED) {
            isCompleted = true;
            videoPlayer.pause();
            videoPlayer.seekTo(0);
            updatePlayButton();
            controlsView.show(true, true);
        }
    }
}
 
Example #12
Source File: MediaController.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onStateChanged(boolean playWhenReady, int playbackState) {
    if (tag != playerNum) {
        return;
    }
    if (playbackState == ExoPlayer.STATE_ENDED || (playbackState == ExoPlayer.STATE_IDLE || playbackState == ExoPlayer.STATE_BUFFERING) && playWhenReady && messageObject.audioProgress >= 0.999f) {
        if (!playlist.isEmpty() && (playlist.size() > 1 || !messageObject.isVoice())) {
            playNextMessageWithoutOrder(true);
        } else {
            cleanupPlayer(true, true, messageObject != null && messageObject.isVoice(), false);
        }
    } else if (seekToProgressPending != 0 && (playbackState == ExoPlayer.STATE_READY || playbackState == ExoPlayer.STATE_IDLE)) {
        int seekTo = (int) (audioPlayer.getDuration() * seekToProgressPending);
        audioPlayer.seekTo(seekTo);
        lastProgress = seekTo;
        seekToProgressPending = 0;
    }
}
 
Example #13
Source File: VideoExoPlayer.java    From TigerVideo with Apache License 2.0 5 votes vote down vote up
@Override
public void play() {

    if(mExoPlayer.getPlaybackState() == ExoPlayer.STATE_READY) {
        mExoPlayer.setPlayWhenReady(true);
    }
}
 
Example #14
Source File: PlaybackControlView.java    From K-Sonic with MIT License 5 votes vote down vote up
private void updateProgress() {
  if (!isVisible() || !isAttachedToWindow) {
    return;
  }
  long duration = player == null ? 0 : player.getDuration();
  long position = player == null ? 0 : player.getCurrentPosition();
  if (durationView != null) {
    durationView.setText(stringForTime(duration));
  }
  if (positionView != null && !dragging) {
    positionView.setText(stringForTime(position));
  }

  if (progressBar != null) {
    if (!dragging) {
      progressBar.setProgress(progressBarValue(position));
    }
    long bufferedPosition = player == null ? 0 : player.getBufferedPosition();
    progressBar.setSecondaryProgress(progressBarValue(bufferedPosition));
    // Remove scheduled updates.
  }
  removeCallbacks(updateProgressAction);
  // Schedule an update if necessary.
  int playbackState = player == null ? ExoPlayer.STATE_IDLE : player.getPlaybackState();
  if (playbackState != ExoPlayer.STATE_IDLE && playbackState != ExoPlayer.STATE_ENDED) {
    long delayMs;
    if (player.getPlayWhenReady() && playbackState == ExoPlayer.STATE_READY) {
      delayMs = 1000 - (position % 1000);
      if (delayMs < 200) {
        delayMs += 1000;
      }
    } else {
      delayMs = 1000;
    }
    postDelayed(updateProgressAction, delayMs);
  }
}
 
Example #15
Source File: VideoExoPlayer.java    From TigerVideo with Apache License 2.0 5 votes vote down vote up
@Override
public void pause() {

    if(mExoPlayer.getPlaybackState() == ExoPlayer.STATE_READY) {
        mExoPlayer.setPlayWhenReady(false);
    }
}
 
Example #16
Source File: VideoExoPlayer.java    From TigerVideo with Apache License 2.0 5 votes vote down vote up
/**--------------------- ExoPlayer.EventListener ----------------------------**/

    @Override
    public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

        if(playbackState == ExoPlayer.STATE_ENDED) {//播放结束
            onCompletion();
        } else if(playbackState == ExoPlayer.STATE_READY) {//准备播放
            if(getState() == PlayState.STATE_LOADING) {//加载视频
                onPrepared();
            } else if(getState() == PlayState.STATE_PLAYING_BUFFERING_START) {//seek to complete
                onSeekComplete();
            }
        }
    }
 
Example #17
Source File: MergingMediaSource.java    From K-Sonic with MIT License 5 votes vote down vote up
@Override
public void prepareSource(ExoPlayer player, boolean isTopLevelSource, Listener listener) {
  this.listener = listener;
  for (int i = 0; i < mediaSources.length; i++) {
    final int sourceIndex = i;
    mediaSources[sourceIndex].prepareSource(player, false, new Listener() {
      @Override
      public void onSourceInfoRefreshed(Timeline timeline, Object manifest) {
        handleSourceInfoRefreshed(sourceIndex, timeline, manifest);
      }
    });
  }
}
 
Example #18
Source File: QBPlaybackControlView.java    From ChatMessagesAdapter-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void restoreState(ExoPlayer player) {
    if(player != null) {
        setPlayer(player);
        updatePositionDurationViews();
        updateViewState();
    }
}
 
Example #19
Source File: EventLogger.java    From LiveVideoBroadcaster with Apache License 2.0 5 votes vote down vote up
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 "?";
  }
}
 
Example #20
Source File: PlayerActivity.java    From ExoPlayer-Offline with Apache License 2.0 5 votes vote down vote up
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
    if (playbackState == ExoPlayer.STATE_ENDED) {
        showControls();
    }
    updateButtonVisibilities();
}
 
Example #21
Source File: EventLogger.java    From ExoPlayer-Offline with Apache License 2.0 5 votes vote down vote up
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 "?";
  }
}
 
Example #22
Source File: MediaPlayback21.java    From Melophile with Apache License 2.0 5 votes vote down vote up
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
  switch (playbackState) {
    case ExoPlayer.STATE_READY:
      if (callback != null) {
        if (isPause) callback.onPause();
        else callback.onPlay();
      }
      break;
    case ExoPlayer.STATE_ENDED:
      if (callback != null) callback.onCompletetion();
      break;
  }
}
 
Example #23
Source File: DashMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void prepareSourceInternal(
    ExoPlayer player,
    boolean isTopLevelSource,
    @Nullable TransferListener mediaTransferListener) {
  this.mediaTransferListener = mediaTransferListener;
  if (sideloadedManifest) {
    processManifest(false);
  } else {
    dataSource = manifestDataSourceFactory.createDataSource();
    loader = new Loader("Loader:DashMediaSource");
    handler = new Handler();
    startLoadingManifest();
  }
}
 
Example #24
Source File: MergingMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void prepareSourceInternal(
    ExoPlayer player,
    boolean isTopLevelSource,
    @Nullable TransferListener mediaTransferListener) {
  super.prepareSourceInternal(player, isTopLevelSource, mediaTransferListener);
  for (int i = 0; i < mediaSources.length; i++) {
    prepareChildSource(i, mediaSources[i]);
  }
}
 
Example #25
Source File: ExtractorMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void prepareSourceInternal(
    ExoPlayer player,
    boolean isTopLevelSource,
    @Nullable TransferListener mediaTransferListener) {
  transferListener = mediaTransferListener;
  notifySourceInfoRefreshed(timelineDurationUs, /* isSeekable= */ false);
}
 
Example #26
Source File: SingleSampleMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void prepareSourceInternal(
    ExoPlayer player,
    boolean isTopLevelSource,
    @Nullable TransferListener mediaTransferListener) {
  transferListener = mediaTransferListener;
  refreshSourceInfo(timeline, /* manifest= */ null);
}
 
Example #27
Source File: BaseMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void prepareSource(
    ExoPlayer player,
    boolean isTopLevelSource,
    SourceInfoRefreshListener listener,
    @Nullable TransferListener mediaTransferListener) {
  Assertions.checkArgument(this.player == null || this.player == player);
  sourceInfoListeners.add(listener);
  if (this.player == null) {
    this.player = player;
    prepareSourceInternal(player, isTopLevelSource, mediaTransferListener);
  } else if (timeline != null) {
    listener.onSourceInfoRefreshed(/* source= */ this, timeline, manifest);
  }
}
 
Example #28
Source File: CumulusTvPlayer.java    From CumulusTV with MIT License 5 votes vote down vote up
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
    for (Callback tvCallback : mTvCallbacks) {
        if (playWhenReady && playbackState == ExoPlayer.STATE_ENDED) {
            tvCallback.onCompleted();
        } else if (playWhenReady && playbackState == ExoPlayer.STATE_READY) {
            tvCallback.onStarted();
        }
    }
    Log.d(TAG, "Player state changed to " + playbackState + ", PWR: " + playWhenReady);
}
 
Example #29
Source File: VideoPlayer.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public void setLooping(boolean looping) {
    if (this.looping != looping) {
        this.looping = looping;
        if (player != null) {
            player.setRepeatMode(looping ? ExoPlayer.REPEAT_MODE_ALL : ExoPlayer.REPEAT_MODE_OFF);
        }
    }
}
 
Example #30
Source File: LoopingMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void prepareSourceInternal(
    ExoPlayer player,
    boolean isTopLevelSource,
    @Nullable TransferListener mediaTransferListener) {
  super.prepareSourceInternal(player, isTopLevelSource, mediaTransferListener);
  prepareChildSource(/* id= */ null, childSource);
}