mirror of
https://github.com/androidx/media.git
synced 2025-05-12 10:09:55 +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. */
|
/** Activity that demonstrates use of {@link SurfaceControl} with ExoPlayer. */
|
||||||
public final class MainActivity extends Activity {
|
public final class MainActivity extends Activity {
|
||||||
|
|
||||||
private static final String TAG = "MainActivity";
|
|
||||||
private static final String DEFAULT_MEDIA_URI =
|
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";
|
"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";
|
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 static final String OWNER_EXTRA = "owner";
|
||||||
|
|
||||||
private boolean isOwner;
|
private boolean isOwner;
|
||||||
private PlayerControlView playerControlView;
|
@Nullable private PlayerControlView playerControlView;
|
||||||
private SurfaceView fullScreenView;
|
@Nullable private SurfaceView fullScreenView;
|
||||||
private SurfaceView nonFullScreenView;
|
@Nullable private SurfaceView nonFullScreenView;
|
||||||
@Nullable private SurfaceView currentOutputView;
|
@Nullable private SurfaceView currentOutputView;
|
||||||
|
|
||||||
private static SimpleExoPlayer player;
|
@Nullable private static SimpleExoPlayer player;
|
||||||
private static SurfaceControl surfaceControl;
|
@Nullable private static SurfaceControl surfaceControl;
|
||||||
private static Surface videoSurface;
|
@Nullable private static Surface videoSurface;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.main_activity);
|
setContentView(R.layout.main_activity);
|
||||||
playerControlView = findViewById(R.id.player_control_view);
|
playerControlView = findViewById(R.id.player_control_view);
|
||||||
@ -80,7 +79,7 @@ public final class MainActivity extends Activity {
|
|||||||
fullScreenView.setOnClickListener(
|
fullScreenView.setOnClickListener(
|
||||||
v -> {
|
v -> {
|
||||||
setCurrentOutputView(nonFullScreenView);
|
setCurrentOutputView(nonFullScreenView);
|
||||||
fullScreenView.setVisibility(View.GONE);
|
Assertions.checkNotNull(fullScreenView).setVisibility(View.GONE);
|
||||||
});
|
});
|
||||||
attachSurfaceListener(fullScreenView);
|
attachSurfaceListener(fullScreenView);
|
||||||
isOwner = getIntent().getBooleanExtra(OWNER_EXTRA, /* defaultValue= */ true);
|
isOwner = getIntent().getBooleanExtra(OWNER_EXTRA, /* defaultValue= */ true);
|
||||||
@ -91,7 +90,7 @@ public final class MainActivity extends Activity {
|
|||||||
Button button = new Button(/* context= */ this);
|
Button button = new Button(/* context= */ this);
|
||||||
view = button;
|
view = button;
|
||||||
button.setText(getString(R.string.no_output_label));
|
button.setText(getString(R.string.no_output_label));
|
||||||
button.setOnClickListener(v -> reparent(null));
|
button.setOnClickListener(v -> reparent(/* surfaceView= */ null));
|
||||||
} else if (i == 1) {
|
} else if (i == 1) {
|
||||||
Button button = new Button(/* context= */ this);
|
Button button = new Button(/* context= */ this);
|
||||||
view = button;
|
view = button;
|
||||||
@ -99,18 +98,17 @@ public final class MainActivity extends Activity {
|
|||||||
button.setOnClickListener(
|
button.setOnClickListener(
|
||||||
v -> {
|
v -> {
|
||||||
setCurrentOutputView(fullScreenView);
|
setCurrentOutputView(fullScreenView);
|
||||||
fullScreenView.setVisibility(View.VISIBLE);
|
Assertions.checkNotNull(fullScreenView).setVisibility(View.VISIBLE);
|
||||||
});
|
});
|
||||||
} else if (i == 2) {
|
} else if (i == 2) {
|
||||||
Button button = new Button(/* context= */ this);
|
Button button = new Button(/* context= */ this);
|
||||||
view = button;
|
view = button;
|
||||||
button.setText(getString(R.string.new_activity_label));
|
button.setText(getString(R.string.new_activity_label));
|
||||||
button.setOnClickListener(
|
button.setOnClickListener(
|
||||||
v -> {
|
v ->
|
||||||
startActivity(
|
startActivity(
|
||||||
new Intent(MainActivity.this, MainActivity.class)
|
new Intent(MainActivity.this, MainActivity.class)
|
||||||
.putExtra(OWNER_EXTRA, /* value= */ false));
|
.putExtra(OWNER_EXTRA, /* value= */ false)));
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
SurfaceView surfaceView = new SurfaceView(this);
|
SurfaceView surfaceView = new SurfaceView(this);
|
||||||
view = surfaceView;
|
view = surfaceView;
|
||||||
@ -147,6 +145,8 @@ public final class MainActivity extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setCurrentOutputView(nonFullScreenView);
|
setCurrentOutputView(nonFullScreenView);
|
||||||
|
|
||||||
|
PlayerControlView playerControlView = Assertions.checkNotNull(this.playerControlView);
|
||||||
playerControlView.setPlayer(player);
|
playerControlView.setPlayer(player);
|
||||||
playerControlView.show();
|
playerControlView.show();
|
||||||
}
|
}
|
||||||
@ -154,7 +154,8 @@ public final class MainActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
playerControlView.setPlayer(null);
|
|
||||||
|
Assertions.checkNotNull(playerControlView).setPlayer(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -164,7 +165,10 @@ public final class MainActivity extends Activity {
|
|||||||
if (surfaceControl != null) {
|
if (surfaceControl != null) {
|
||||||
surfaceControl.release();
|
surfaceControl.release();
|
||||||
surfaceControl = null;
|
surfaceControl = null;
|
||||||
|
}
|
||||||
|
if (videoSurface != null) {
|
||||||
videoSurface.release();
|
videoSurface.release();
|
||||||
|
videoSurface = null;
|
||||||
}
|
}
|
||||||
if (player != null) {
|
if (player != null) {
|
||||||
player.release();
|
player.release();
|
||||||
@ -176,7 +180,10 @@ public final class MainActivity extends Activity {
|
|||||||
private void initializePlayer() {
|
private void initializePlayer() {
|
||||||
Intent intent = getIntent();
|
Intent intent = getIntent();
|
||||||
String action = intent.getAction();
|
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));
|
String userAgent = Util.getUserAgent(this, getString(R.string.application_name));
|
||||||
DrmSessionManager<ExoMediaCrypto> drmSessionManager;
|
DrmSessionManager<ExoMediaCrypto> drmSessionManager;
|
||||||
if (intent.hasExtra(DRM_SCHEME_EXTRA)) {
|
if (intent.hasExtra(DRM_SCHEME_EXTRA)) {
|
||||||
@ -212,7 +219,7 @@ public final class MainActivity extends Activity {
|
|||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
player = new SimpleExoPlayer.Builder(getApplicationContext()).build();
|
SimpleExoPlayer player = new SimpleExoPlayer.Builder(getApplicationContext()).build();
|
||||||
player.prepare(mediaSource);
|
player.prepare(mediaSource);
|
||||||
player.setPlayWhenReady(true);
|
player.setPlayWhenReady(true);
|
||||||
player.setRepeatMode(Player.REPEAT_MODE_ALL);
|
player.setRepeatMode(Player.REPEAT_MODE_ALL);
|
||||||
@ -224,6 +231,7 @@ public final class MainActivity extends Activity {
|
|||||||
.build();
|
.build();
|
||||||
videoSurface = new Surface(surfaceControl);
|
videoSurface = new Surface(surfaceControl);
|
||||||
player.setVideoSurface(videoSurface);
|
player.setVideoSurface(videoSurface);
|
||||||
|
MainActivity.player = player;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setCurrentOutputView(@Nullable SurfaceView surfaceView) {
|
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) {
|
if (surfaceView == null) {
|
||||||
new SurfaceControl.Transaction()
|
new SurfaceControl.Transaction()
|
||||||
.reparent(surfaceControl, /* newParent= */ null)
|
.reparent(surfaceControl, /* newParent= */ null)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user