Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Commit

Permalink
Prevent sleep mode when playing with exoplayer.
Browse files Browse the repository at this point in the history
  • Loading branch information
f77 committed Jun 16, 2020
1 parent c7ded71 commit 21389ce
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Security


## [2.19.0] - 2020-06-14
### Changed
- Prevent sleep mode when playing with exoplayer.

## [2.18.0] - 2020-06-14
### Added
- Added whole thread's playlist playing in the exoplayer (exoplayer supports navigation between videos now).
Expand Down Expand Up @@ -148,6 +152,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Added this changelog.
- Rebuilt to a new Android SDK (29).

[2.19.0]: https://github.com/f77/Dashchan/compare/2.18.0...2.19.0
[2.18.0]: https://github.com/f77/Dashchan/compare/2.17.0...2.18.0
[2.17.0]: https://github.com/f77/Dashchan/compare/2.16.0...2.17.0
[2.16.0]: https://github.com/f77/Dashchan/compare/2.15.0...2.16.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import com.google.android.exoplayer2.source.ConcatenatingMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
Expand All @@ -48,7 +50,7 @@ public class PlayerActivity extends AppCompatActivity {
public static final String STATE_KEY_IS_REPEAT = "is_repeat";

private PlaybackStateListener playbackStateListener;
private static final String TAG = PlayerActivity.class.getName();
private static final String DEBUG_TAG = PlayerActivity.class.getName();

private PlayerView playerView;
private SimpleExoPlayer player;
Expand Down Expand Up @@ -214,30 +216,46 @@ protected void changeControllerVisibility() {
}

private class PlaybackStateListener implements Player.EventListener {
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == Player.STATE_IDLE || playbackState == Player.STATE_ENDED || !playWhenReady) {
// Can sleep.
playerView.setKeepScreenOn(false);
} else { // STATE_BUFFERING, STATE_READY, playWhenReady is true
// Prevent sleep.
playerView.setKeepScreenOn(true);
}

Log.d(DEBUG_TAG, "changed STATE to " + stateToString(playbackState) + " playWhenReady: " + playWhenReady);
}

@Override
public void onPlayerStateChanged(boolean playWhenReady,
int playbackState) {
String stateString;
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
Log.d(DEBUG_TAG, "currentWindowIndex changed to " + player.getCurrentWindowIndex());
}

/**
* Convert playbackState to string.
*
* @param playbackState playbackState.
* @return Description.
*/
protected String stateToString(int playbackState) {
switch (playbackState) {
case ExoPlayer.STATE_IDLE:
stateString = "ExoPlayer.STATE_IDLE -";
break;
return "ExoPlayer.STATE_IDLE -";

case ExoPlayer.STATE_BUFFERING:
stateString = "ExoPlayer.STATE_BUFFERING -";
break;
return "ExoPlayer.STATE_BUFFERING -";

case ExoPlayer.STATE_READY:
stateString = "ExoPlayer.STATE_READY -";
break;
return "ExoPlayer.STATE_READY -";

case ExoPlayer.STATE_ENDED:
stateString = "ExoPlayer.STATE_ENDED -";
break;
return "ExoPlayer.STATE_ENDED -";
default:
stateString = "UNKNOWN_STATE -";
break;
return "UNKNOWN_STATE -";
}
Log.d(TAG, "changed state to " + stateString
+ " playWhenReady: " + playWhenReady);
}
}

Expand Down

0 comments on commit 21389ce

Please sign in to comment.