Java Code Examples for com.google.android.exoplayer2.Player#STATE_READY

The following examples show how to use com.google.android.exoplayer2.Player#STATE_READY . 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: AudioFocusManager.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Sets audio attributes that should be used to manage audio focus.
 *
 * @param audioAttributes The audio attributes or {@code null} if audio focus should not be
 *     managed automatically.
 * @param playWhenReady The current state of {@link ExoPlayer#getPlayWhenReady()}.
 * @param playerState The current player state; {@link ExoPlayer#getPlaybackState()}.
 * @return A {@link PlayerCommand} to execute on the player.
 */
@PlayerCommand
public int setAudioAttributes(
    @Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState) {
  if (!Util.areEqual(this.audioAttributes, audioAttributes)) {
    this.audioAttributes = audioAttributes;
    focusGain = convertAudioAttributesToFocusGain(audioAttributes);

    Assertions.checkArgument(
        focusGain == C.AUDIOFOCUS_GAIN || focusGain == C.AUDIOFOCUS_NONE,
        "Automatic handling of audio focus is only available for USAGE_MEDIA and USAGE_GAME.");
    if (playWhenReady
        && (playerState == Player.STATE_BUFFERING || playerState == Player.STATE_READY)) {
      return requestAudioFocus();
    }
  }

  return playerState == Player.STATE_IDLE
      ? handleIdle(playWhenReady)
      : handlePrepare(playWhenReady);
}
 
Example 2
Source File: ExoMediaPlayer.java    From PainlessMusicPlayer with Apache License 2.0 6 votes vote down vote up
@Override
public void onPlayerStateChanged(final boolean playWhenReady, final int playbackState) {
    if (Log.logDEnabled()) {
        Log.d(TAG, "onPlayerStateChanged: " + playbackState);
    }
    if (mediaPlayerListener != null) {
        switch (playbackState) {
            case Player.STATE_READY:
                loadedMediaUri = loadingMediaUri;
                if (playWhenReady) {
                    mediaPlayerListener.onPlaybackStarted();
                } else {
                    mediaPlayerListener.onPlaybackPaused();
                }
                break;

            case Player.STATE_ENDED:
                mediaPlayerListener.onPlaybackFinished();
                break;
        }
    }
}
 
Example 3
Source File: ReactExoplayerView.java    From react-native-video with MIT License 6 votes vote down vote up
private void startPlayback() {
    if (player != null) {
        switch (player.getPlaybackState()) {
            case Player.STATE_IDLE:
            case Player.STATE_ENDED:
                initializePlayer();
                break;
            case Player.STATE_BUFFERING:
            case Player.STATE_READY:
                if (!player.getPlayWhenReady()) {
                    setPlayWhenReady(true);
                }
                break;
            default:
                break;
        }

    } else {
        initializePlayer();
    }
    if (!disableFocus) {
        setKeepScreenOn(preventsDisplaySleepDuringVideoPlayback);
    }
}
 
Example 4
Source File: AudioFocusManager.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Sets audio attributes that should be used to manage audio focus.
 *
 * @param audioAttributes The audio attributes or {@code null} if audio focus should not be
 *     managed automatically.
 * @param playWhenReady The current state of {@link ExoPlayer#getPlayWhenReady()}.
 * @param playerState The current player state; {@link ExoPlayer#getPlaybackState()}.
 * @return A {@link PlayerCommand} to execute on the player.
 */
@PlayerCommand
public int setAudioAttributes(
    @Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState) {
  if (!Util.areEqual(this.audioAttributes, audioAttributes)) {
    this.audioAttributes = audioAttributes;
    focusGain = convertAudioAttributesToFocusGain(audioAttributes);

    Assertions.checkArgument(
        focusGain == C.AUDIOFOCUS_GAIN || focusGain == C.AUDIOFOCUS_NONE,
        "Automatic handling of audio focus is only available for USAGE_MEDIA and USAGE_GAME.");
    if (playWhenReady
        && (playerState == Player.STATE_BUFFERING || playerState == Player.STATE_READY)) {
      return requestAudioFocus();
    }
  }

  return playerState == Player.STATE_IDLE
      ? handleIdle(playWhenReady)
      : handlePrepare(playWhenReady);
}
 
Example 5
Source File: LocalPlayback.java    From klingar with Apache License 2.0 6 votes vote down vote up
@Override @State public int getState() {
  if (exoPlayer == null) {
    return exoPlayerNullIsStopped ? PlaybackStateCompat.STATE_STOPPED
        : PlaybackStateCompat.STATE_NONE;
  }
  switch (exoPlayer.getPlaybackState()) {
    case Player.STATE_IDLE:
    case Player.STATE_ENDED:
      return PlaybackStateCompat.STATE_PAUSED;
    case Player.STATE_BUFFERING:
      return PlaybackStateCompat.STATE_BUFFERING;
    case Player.STATE_READY:
      return exoPlayer.getPlayWhenReady() ? PlaybackStateCompat.STATE_PLAYING
          : PlaybackStateCompat.STATE_PAUSED;
    default:
      return PlaybackStateCompat.STATE_NONE;
  }
}
 
Example 6
Source File: VisualizerView.java    From zom-android-matrix with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    if (exoPlayer != null) {
        long duration = exoPlayer.getDuration();
        long pos = exoPlayer.getCurrentPosition();
        if (duration == 0) {
            setPlayFraction(0);
        } else {
            setPlayFraction((float) pos / (float) duration);
        }

        if (exoPlayer.getPlaybackState() == Player.STATE_READY) {
            post(updatePlayerPositionRunnable);
        }
    }
}
 
Example 7
Source File: LocalPlayback.java    From klingar with Apache License 2.0 6 votes vote down vote up
@Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
  Timber.d("onPlayerStateChanged %s playWhenReady %s", getExoPlayerState(playbackState),
      playWhenReady);

  switch (playbackState) {
    case Player.STATE_IDLE:
    case Player.STATE_BUFFERING:
    case Player.STATE_READY:
      if (callback != null) {
        callback.onPlaybackStatusChanged();
      }
      break;
    case Player.STATE_ENDED:
      if (callback != null) {
        callback.onCompletion();
      }
      break;
    default:
      break;
  }
}
 
Example 8
Source File: AndExoPlayerView.java    From MagicalExoPlayer with MIT License 5 votes vote down vote up
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
    String stateString;
    switch (playbackState) {
        case Player.STATE_IDLE:
            stateString = "ExoPlayer.STATE_IDLE      -";
            break;
        case Player.STATE_BUFFERING:
            stateString = "ExoPlayer.STATE_BUFFERING -";
            break;
        case Player.STATE_READY:
            if (isPreparing) {
                // this is accurate
                isPreparing = false;
            }
            hideProgress();
            stateString = "ExoPlayer.STATE_READY     -";
            break;
        case Player.STATE_ENDED:
            stateString = "ExoPlayer.STATE_ENDED     -";
            break;
        default:
            stateString = "UNKNOWN_STATE             -";
            break;
    }

    Log.d(TAG, "changed state to " + stateString
            + " playWhenReady: " + playWhenReady);
}
 
Example 9
Source File: EventLogger.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private static String getStateString(int state) {
  switch (state) {
    case Player.STATE_BUFFERING:
      return "BUFFERING";
    case Player.STATE_ENDED:
      return "ENDED";
    case Player.STATE_IDLE:
      return "IDLE";
    case Player.STATE_READY:
      return "READY";
    default:
      return "?";
  }
}
 
Example 10
Source File: VideoPlayer.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
    switch (playbackState) {
        case Player.STATE_READY:
            state = PlayerState.PREPARED;
            if (callback != null) {
                callback.onPrepared();
            }

            if (playWhenReady) {
                state = PlayerState.PLAYING;
            }
            break;
        case Player.STATE_ENDED:
            // onPlayerStateChanged with Player.STATE_ENDED called twice after calling
            // setPlayWhenReady(false) in STATE_ENDED
            // so if already called then avoid on completion again.
            if (state == PlayerState.PLAYBACK_COMPLETE)
                return;
            // sometimes, error also causes onCompletion() call
            // avoid on completion if player got an error
            if (state != PlayerState.ERROR) {
                state = PlayerState.PLAYBACK_COMPLETE;
                if (callback != null) {
                    callback.onPlaybackComplete();
                }
                seekTo(0);
                exoPlayer.setPlayWhenReady(false);
                logger.debug("Playback complete");
            }
            break;
    }
}
 
Example 11
Source File: EventLogger.java    From GSYVideoPlayer with Apache License 2.0 5 votes vote down vote up
private static String getStateString(int state) {
    switch (state) {
        case Player.STATE_BUFFERING:
            return "B";
        case Player.STATE_ENDED:
            return "E";
        case Player.STATE_IDLE:
            return "I";
        case Player.STATE_READY:
            return "R";
        default:
            return "?";
    }
}
 
Example 12
Source File: EventLogger.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static String getStateString(int state) {
  switch (state) {
    case Player.STATE_BUFFERING:
      return "BUFFERING";
    case Player.STATE_ENDED:
      return "ENDED";
    case Player.STATE_IDLE:
      return "IDLE";
    case Player.STATE_READY:
      return "READY";
    default:
      return "?";
  }
}
 
Example 13
Source File: VideoPlayer.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
    maybeReportPlayerState();
    if (playWhenReady && playbackState == Player.STATE_READY) {
        NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.playerDidStartPlaying, this);
    }
    if (!videoPlayerReady && playbackState == Player.STATE_READY) {
        videoPlayerReady = true;
        checkPlayersReady();
    }
}
 
Example 14
Source File: AudioSlidePlayer.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
public void setListener(@NonNull Listener listener) {
  this.listener = new WeakReference<>(listener);

  if (this.mediaPlayer != null && this.mediaPlayer.getPlaybackState() == Player.STATE_READY) {
    notifyOnStart();
  }
}
 
Example 15
Source File: ExoMediaPlayer.java    From ExoMedia with Apache License 2.0 5 votes vote down vote up
private void reportPlayerState() {
    boolean playWhenReady = player.getPlayWhenReady();
    int playbackState = getPlaybackState();

    int newState = stateStore.getState(playWhenReady, playbackState);
    if (newState != stateStore.getMostRecentState()) {
        stateStore.setMostRecentState(playWhenReady, playbackState);

        //Makes sure the buffering notifications are sent
        if (newState == Player.STATE_READY) {
            setBufferRepeaterStarted(true);
        } else if (newState == Player.STATE_IDLE || newState == Player.STATE_ENDED) {
            setBufferRepeaterStarted(false);
        }

        //Because the playWhenReady isn't a state in itself, rather a flag to a state we will ignore informing of
        // see events when that is the only change.  Additionally, on some devices we get states ordered as
        // [seeking, ready, buffering, ready] while on others we get [seeking, buffering, ready]
        boolean informSeekCompletion = stateStore.matchesHistory(new int[]{StateStore.STATE_SEEKING, Player.STATE_BUFFERING, Player.STATE_READY}, true);
        informSeekCompletion |= stateStore.matchesHistory(new int[]{Player.STATE_BUFFERING, StateStore.STATE_SEEKING, Player.STATE_READY}, true);
        informSeekCompletion |= stateStore.matchesHistory(new int[]{StateStore.STATE_SEEKING, Player.STATE_READY, Player.STATE_BUFFERING, Player.STATE_READY}, true);

        for (ExoPlayerListener listener : listeners) {
            listener.onStateChanged(playWhenReady, playbackState);

            if (informSeekCompletion) {
                listener.onSeekComplete();
            }
        }
    }
}
 
Example 16
Source File: ExoPlayerView.java    From AerialDream with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
    if (!prepared && playbackState == Player.STATE_READY) {
        prepared = true;
        listener.onPrepared(this);
    }

    if (playWhenReady && playbackState == Player.STATE_READY) {
        removeCallbacks(timerRunnable);
        postDelayed(timerRunnable, getDuration() - DURATION);
    }
}
 
Example 17
Source File: ExoMediaPlayer.java    From DKVideoPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isPlaying() {
    if (mInternalPlayer == null)
        return false;
    int state = mInternalPlayer.getPlaybackState();
    switch (state) {
        case Player.STATE_BUFFERING:
        case Player.STATE_READY:
            return mInternalPlayer.getPlayWhenReady();
        case Player.STATE_IDLE:
        case Player.STATE_ENDED:
        default:
            return false;
    }
}
 
Example 18
Source File: LocalPlayback.java    From klingar with Apache License 2.0 5 votes vote down vote up
private static String getExoPlayerState(int state) {
  switch (state) {
    case Player.STATE_IDLE:
      return "STATE_IDLE";
    case Player.STATE_BUFFERING:
      return "STATE_BUFFERING";
    case Player.STATE_READY:
      return "STATE_READY";
    case Player.STATE_ENDED:
      return "STATE_ENDED";
    default:
      return "UNKNOWN";
  }
}
 
Example 19
Source File: IjkExo2MediaPlayer.java    From GSYVideoPlayer with Apache License 2.0 4 votes vote down vote up
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
    //重新播放状态顺序为:STATE_IDLE -》STATE_BUFFERING -》STATE_READY
    //缓冲时顺序为:STATE_BUFFERING -》STATE_READY
    //Log.e(TAG, "onPlayerStateChanged: playWhenReady = " + playWhenReady + ", playbackState = " + playbackState);
    if (isLastReportedPlayWhenReady != playWhenReady || lastReportedPlaybackState != playbackState) {
        if (isBuffering) {
            switch (playbackState) {
                case Player.STATE_ENDED:
                case Player.STATE_READY:
                    notifyOnInfo(IMediaPlayer.MEDIA_INFO_BUFFERING_END, mInternalPlayer.getBufferedPercentage());
                    isBuffering = false;
                    break;
            }
        }

        if (isPreparing) {
            switch (playbackState) {
                case Player.STATE_READY:
                    notifyOnPrepared();
                    isPreparing = false;
                    break;
            }
        }

        switch (playbackState) {
            case Player.STATE_BUFFERING:
                notifyOnInfo(IMediaPlayer.MEDIA_INFO_BUFFERING_START, mInternalPlayer.getBufferedPercentage());
                isBuffering = true;
                break;
            case Player.STATE_READY:
                break;
            case Player.STATE_ENDED:
                notifyOnCompletion();
                break;
            default:
                break;
        }
    }
    isLastReportedPlayWhenReady = playWhenReady;
    lastReportedPlaybackState = playbackState;
}
 
Example 20
Source File: ExoPlayerManager.java    From PreviewSeekBar with Apache License 2.0 4 votes vote down vote up
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
    if (playbackState == Player.STATE_READY && playWhenReady) {
        previewTimeBar.hidePreview();
    }
}