Skip to content

Commit

Permalink
Fix surfacedemo nullness issues
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 273296545
  • Loading branch information
andrewlewis authored and ojw28 committed Oct 7, 2019
1 parent fed89d7 commit 48bfb37
Showing 1 changed file with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
/** Activity that demonstrates use of {@link SurfaceControl} with ExoPlayer. */
public final class MainActivity extends Activity {

private static final String TAG = "MainActivity";
private static final String DEFAULT_MEDIA_URI =
"https://storage.googleapis.com/exoplayer-test-media-1/mkv/android-screens-lavf-56.36.100-aac-avc-main-1280x720.mkv";
private static final String SURFACE_CONTROL_NAME = "surfacedemo";
Expand All @@ -62,25 +61,25 @@ public final class MainActivity extends Activity {
private static final String OWNER_EXTRA = "owner";

private boolean isOwner;
private PlayerControlView playerControlView;
private SurfaceView fullScreenView;
private SurfaceView nonFullScreenView;
@Nullable private PlayerControlView playerControlView;
@Nullable private SurfaceView fullScreenView;
@Nullable private SurfaceView nonFullScreenView;
@Nullable private SurfaceView currentOutputView;

private static SimpleExoPlayer player;
private static SurfaceControl surfaceControl;
private static Surface videoSurface;
@Nullable private static SimpleExoPlayer player;
@Nullable private static SurfaceControl surfaceControl;
@Nullable private static Surface videoSurface;

@Override
protected void onCreate(Bundle savedInstanceState) {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
playerControlView = findViewById(R.id.player_control_view);
fullScreenView = findViewById(R.id.full_screen_view);
fullScreenView.setOnClickListener(
v -> {
setCurrentOutputView(nonFullScreenView);
fullScreenView.setVisibility(View.GONE);
Assertions.checkNotNull(fullScreenView).setVisibility(View.GONE);
});
attachSurfaceListener(fullScreenView);
isOwner = getIntent().getBooleanExtra(OWNER_EXTRA, /* defaultValue= */ true);
Expand All @@ -91,26 +90,25 @@ protected void onCreate(Bundle savedInstanceState) {
Button button = new Button(/* context= */ this);
view = button;
button.setText(getString(R.string.no_output_label));
button.setOnClickListener(v -> reparent(null));
button.setOnClickListener(v -> reparent(/* surfaceView= */ null));
} else if (i == 1) {
Button button = new Button(/* context= */ this);
view = button;
button.setText(getString(R.string.full_screen_label));
button.setOnClickListener(
v -> {
setCurrentOutputView(fullScreenView);
fullScreenView.setVisibility(View.VISIBLE);
Assertions.checkNotNull(fullScreenView).setVisibility(View.VISIBLE);
});
} else if (i == 2) {
Button button = new Button(/* context= */ this);
view = button;
button.setText(getString(R.string.new_activity_label));
button.setOnClickListener(
v -> {
startActivity(
new Intent(MainActivity.this, MainActivity.class)
.putExtra(OWNER_EXTRA, /* value= */ false));
});
v ->
startActivity(
new Intent(MainActivity.this, MainActivity.class)
.putExtra(OWNER_EXTRA, /* value= */ false)));
} else {
SurfaceView surfaceView = new SurfaceView(this);
view = surfaceView;
Expand Down Expand Up @@ -147,14 +145,17 @@ public void onResume() {
}

setCurrentOutputView(nonFullScreenView);

PlayerControlView playerControlView = Assertions.checkNotNull(this.playerControlView);
playerControlView.setPlayer(player);
playerControlView.show();
}

@Override
public void onPause() {
super.onPause();
playerControlView.setPlayer(null);

Assertions.checkNotNull(playerControlView).setPlayer(null);
}

@Override
Expand All @@ -164,7 +165,10 @@ public void onDestroy() {
if (surfaceControl != null) {
surfaceControl.release();
surfaceControl = null;
}
if (videoSurface != null) {
videoSurface.release();
videoSurface = null;
}
if (player != null) {
player.release();
Expand All @@ -176,7 +180,10 @@ public void onDestroy() {
private void initializePlayer() {
Intent intent = getIntent();
String action = intent.getAction();
Uri uri = ACTION_VIEW.equals(action) ? intent.getData() : Uri.parse(DEFAULT_MEDIA_URI);
Uri uri =
ACTION_VIEW.equals(action)
? Assertions.checkNotNull(intent.getData())
: Uri.parse(DEFAULT_MEDIA_URI);
String userAgent = Util.getUserAgent(this, getString(R.string.application_name));
DrmSessionManager<ExoMediaCrypto> drmSessionManager;
if (intent.hasExtra(DRM_SCHEME_EXTRA)) {
Expand Down Expand Up @@ -212,7 +219,7 @@ private void initializePlayer() {
} else {
throw new IllegalStateException();
}
player = new SimpleExoPlayer.Builder(getApplicationContext()).build();
SimpleExoPlayer player = new SimpleExoPlayer.Builder(getApplicationContext()).build();
player.prepare(mediaSource);
player.setPlayWhenReady(true);
player.setRepeatMode(Player.REPEAT_MODE_ALL);
Expand All @@ -224,6 +231,7 @@ private void initializePlayer() {
.build();
videoSurface = new Surface(surfaceControl);
player.setVideoSurface(videoSurface);
MainActivity.player = player;
}

private void setCurrentOutputView(@Nullable SurfaceView surfaceView) {
Expand Down Expand Up @@ -254,7 +262,8 @@ public void surfaceDestroyed(SurfaceHolder surfaceHolder) {}
});
}

private void reparent(@Nullable SurfaceView surfaceView) {
private static void reparent(@Nullable SurfaceView surfaceView) {
SurfaceControl surfaceControl = Assertions.checkNotNull(MainActivity.surfaceControl);
if (surfaceView == null) {
new SurfaceControl.Transaction()
.reparent(surfaceControl, /* newParent= */ null)
Expand Down

0 comments on commit 48bfb37

Please sign in to comment.