react-native-video/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ExoPlayerFullscreenVideoActivity.java

140 lines
5.3 KiB
Java
Raw Normal View History

2020-07-04 17:41:15 +07:00
package com.brentvatne.exoplayer;
2020-07-10 10:45:41 +07:00
import android.content.pm.ActivityInfo;
2020-07-04 17:41:15 +07:00
import android.os.Bundle;
2020-07-05 11:00:25 +07:00
import android.view.KeyEvent;
2020-07-04 17:41:15 +07:00
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.brentvatne.react.R;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ui.PlayerControlView;
public class ExoPlayerFullscreenVideoActivity extends AppCompatActivity implements ReactExoplayerView.FullScreenDelegate {
public static final String EXTRA_ID = "extra_id";
2020-07-10 10:45:41 +07:00
public static final String EXTRA_ORIENTATION = "extra_orientation";
2020-07-04 17:41:15 +07:00
private int id;
private PlayerControlView playerControlView;
private SimpleExoPlayer player;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
id = getIntent().getIntExtra(EXTRA_ID, -1);
2020-07-10 10:45:41 +07:00
String orientation = getIntent().getStringExtra(EXTRA_ORIENTATION);
if ("landscape".equals(orientation)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
} else if ("portrait".equals(orientation)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
setContentView(R.layout.exo_player_fullscreen_video);
2020-07-04 17:41:15 +07:00
player = ReactExoplayerView.getViewInstance(id).getPlayer();
ExoPlayerView playerView = findViewById(R.id.player_view);
playerView.setPlayer(player);
playerView.setOnClickListener(v -> togglePlayerControlVisibility());
playerControlView = findViewById(R.id.player_controls);
playerControlView.setPlayer(player);
// Set the fullscreen button to "close fullscreen" icon
ImageView fullscreenIcon = playerControlView.findViewById(R.id.exo_fullscreen_icon);
fullscreenIcon.setImageResource(R.drawable.exo_controls_fullscreen_exit);
playerControlView.findViewById(R.id.exo_fullscreen_button)
2020-07-05 11:00:25 +07:00
.setOnClickListener(v -> ReactExoplayerView.getViewInstance(id).setFullscreen(false));
//Handling the playButton click event
playerControlView.findViewById(R.id.exo_play).setOnClickListener(v -> {
if (player != null && player.getPlaybackState() == Player.STATE_ENDED) {
player.seekTo(0);
}
ReactExoplayerView.getViewInstance(id).setPausedModifier(false);
});
//Handling the pauseButton click event
playerControlView.findViewById(R.id.exo_pause).setOnClickListener(v -> ReactExoplayerView.getViewInstance(id).setPausedModifier(true));
2020-07-04 17:41:15 +07:00
}
@Override
public void onResume() {
super.onResume();
2020-07-05 11:00:25 +07:00
boolean isPaused = ReactExoplayerView.getViewInstance(id).isPaused();
player.setPlayWhenReady(!isPaused);
2020-07-10 10:45:41 +07:00
if (ReactExoplayerView.getViewInstance(id) != null) {
ReactExoplayerView.getViewInstance(id).registerFullScreenDelegate(this);
}
2020-07-04 17:41:15 +07:00
}
@Override
public void onPause() {
super.onPause();
player.setPlayWhenReady(false);
2020-07-10 10:45:41 +07:00
if (ReactExoplayerView.getViewInstance(id) != null) {
ReactExoplayerView.getViewInstance(id).registerFullScreenDelegate(null);
}
2020-07-05 11:00:25 +07:00
}
2020-07-04 17:41:15 +07:00
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
hideSystemUI();
}
}
2020-07-05 11:00:25 +07:00
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
2020-07-10 10:45:41 +07:00
if (ReactExoplayerView.getViewInstance(id) != null) {
ReactExoplayerView.getViewInstance(id).setFullscreen(false);
return false;
}
return true;
2020-07-05 11:00:25 +07:00
}
return super.onKeyDown(keyCode, event);
}
2020-07-04 17:41:15 +07:00
private void togglePlayerControlVisibility() {
if (playerControlView.isVisible()) {
playerControlView.hide();
} else {
playerControlView.show();
}
}
/**
* Enables regular immersive mode.
*/
private void hideSystemUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
/**
* Shows the system bars by removing all the flags
* except for the ones that make the content appear under the system bars.
*/
private void showSystemUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
@Override
public void closeFullScreen() {
finish();
}
}