mirror of
https://github.com/androidx/media.git
synced 2025-05-11 09:39:52 +08:00
Fix surfacedemo nullness issues
PiperOrigin-RevId: 273296545
This commit is contained in:
parent
fed89d76ca
commit
48bfb37e93
@ -50,7 +50,6 @@ import java.util.UUID;
|
||||
/** 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";
|
||||
@ -62,17 +61,17 @@ 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);
|
||||
@ -80,7 +79,7 @@ public final class MainActivity extends Activity {
|
||||
fullScreenView.setOnClickListener(
|
||||
v -> {
|
||||
setCurrentOutputView(nonFullScreenView);
|
||||
fullScreenView.setVisibility(View.GONE);
|
||||
Assertions.checkNotNull(fullScreenView).setVisibility(View.GONE);
|
||||
});
|
||||
attachSurfaceListener(fullScreenView);
|
||||
isOwner = getIntent().getBooleanExtra(OWNER_EXTRA, /* defaultValue= */ true);
|
||||
@ -91,7 +90,7 @@ public final class MainActivity extends Activity {
|
||||
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;
|
||||
@ -99,18 +98,17 @@ public final class MainActivity extends Activity {
|
||||
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;
|
||||
@ -147,6 +145,8 @@ public final class MainActivity extends Activity {
|
||||
}
|
||||
|
||||
setCurrentOutputView(nonFullScreenView);
|
||||
|
||||
PlayerControlView playerControlView = Assertions.checkNotNull(this.playerControlView);
|
||||
playerControlView.setPlayer(player);
|
||||
playerControlView.show();
|
||||
}
|
||||
@ -154,7 +154,8 @@ public final class MainActivity extends Activity {
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
playerControlView.setPlayer(null);
|
||||
|
||||
Assertions.checkNotNull(playerControlView).setPlayer(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -164,7 +165,10 @@ public final class MainActivity extends Activity {
|
||||
if (surfaceControl != null) {
|
||||
surfaceControl.release();
|
||||
surfaceControl = null;
|
||||
}
|
||||
if (videoSurface != null) {
|
||||
videoSurface.release();
|
||||
videoSurface = null;
|
||||
}
|
||||
if (player != null) {
|
||||
player.release();
|
||||
@ -176,7 +180,10 @@ public final class MainActivity extends Activity {
|
||||
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)) {
|
||||
@ -212,7 +219,7 @@ public final class MainActivity extends Activity {
|
||||
} 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);
|
||||
@ -224,6 +231,7 @@ public final class MainActivity extends Activity {
|
||||
.build();
|
||||
videoSurface = new Surface(surfaceControl);
|
||||
player.setVideoSurface(videoSurface);
|
||||
MainActivity.player = player;
|
||||
}
|
||||
|
||||
private void setCurrentOutputView(@Nullable SurfaceView surfaceView) {
|
||||
@ -254,7 +262,8 @@ public final class MainActivity extends Activity {
|
||||
});
|
||||
}
|
||||
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user