Use Util to request permissions in Transformer demo

This avoids requesting storage permissions for URLs that don't require it.

Also tidy clean-up into `cleanUpExport`, rename `releasePlayer` to
`releasePlayers` as there are players for both input and output, and reduce
live range of `uri` variable.

PiperOrigin-RevId: 712857115
This commit is contained in:
andrewlewis 2025-01-07 04:11:12 -08:00 committed by Copybara-Service
parent 0074a97333
commit 5a87aab2f7

View File

@ -15,11 +15,8 @@
*/ */
package androidx.media3.demo.transformer; package androidx.media3.demo.transformer;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.READ_MEDIA_VIDEO;
import static androidx.media3.common.util.Assertions.checkNotNull; import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkState; import static androidx.media3.common.util.Assertions.checkState;
import static androidx.media3.common.util.Util.SDK_INT;
import static androidx.media3.exoplayer.DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS; import static androidx.media3.exoplayer.DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS;
import static androidx.media3.exoplayer.DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS; import static androidx.media3.exoplayer.DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS;
import static androidx.media3.transformer.Transformer.PROGRESS_STATE_NOT_STARTED; import static androidx.media3.transformer.Transformer.PROGRESS_STATE_NOT_STARTED;
@ -48,7 +45,6 @@ import android.widget.Toast;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.media3.common.C; import androidx.media3.common.C;
import androidx.media3.common.DebugViewProvider; import androidx.media3.common.DebugViewProvider;
import androidx.media3.common.Effect; import androidx.media3.common.Effect;
@ -193,30 +189,13 @@ public final class TransformerActivity extends AppCompatActivity {
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
if (transformer != null) {
transformer.cancel();
transformer = null;
}
// The stop watch is reset after cancelling the export, in case cancelling causes the stop watch
// to be stopped in a transformer callback.
exportStopwatch.reset();
inputPlayerView.onPause(); inputPlayerView.onPause();
outputPlayerView.onPause(); outputPlayerView.onPause();
releasePlayer(); releasePlayers();
cleanUpExport();
outputFile.delete();
outputFile = null;
if (oldOutputFile != null) {
oldOutputFile.delete();
oldOutputFile = null;
}
} }
private void startExport() { private void startExport() {
requestReadVideoPermission(/* activity= */ this);
Intent intent = getIntent(); Intent intent = getIntent();
Uri inputUri = checkNotNull(intent.getData()); Uri inputUri = checkNotNull(intent.getData());
try { try {
@ -228,6 +207,7 @@ public final class TransformerActivity extends AppCompatActivity {
String outputFilePath = outputFile.getAbsolutePath(); String outputFilePath = outputFile.getAbsolutePath();
@Nullable Bundle bundle = intent.getExtras(); @Nullable Bundle bundle = intent.getExtras();
MediaItem mediaItem = createMediaItem(bundle, inputUri); MediaItem mediaItem = createMediaItem(bundle, inputUri);
Util.maybeRequestReadStoragePermission(/* activity= */ this, mediaItem);
Transformer transformer = createTransformer(bundle, inputUri, outputFilePath); Transformer transformer = createTransformer(bundle, inputUri, outputFilePath);
Composition composition = createComposition(mediaItem, bundle); Composition composition = createComposition(mediaItem, bundle);
exportStopwatch.reset(); exportStopwatch.reset();
@ -702,9 +682,8 @@ public final class TransformerActivity extends AppCompatActivity {
private void playMediaItems(MediaItem inputMediaItem, MediaItem outputMediaItem) { private void playMediaItems(MediaItem inputMediaItem, MediaItem outputMediaItem) {
inputPlayerView.setPlayer(null); inputPlayerView.setPlayer(null);
outputPlayerView.setPlayer(null); outputPlayerView.setPlayer(null);
releasePlayer(); releasePlayers();
Uri uri = checkNotNull(inputMediaItem.localConfiguration).uri;
ExoPlayer outputPlayer = ExoPlayer outputPlayer =
new ExoPlayer.Builder(/* context= */ this) new ExoPlayer.Builder(/* context= */ this)
.setLoadControl( .setLoadControl(
@ -723,6 +702,7 @@ public final class TransformerActivity extends AppCompatActivity {
this.outputPlayer = outputPlayer; this.outputPlayer = outputPlayer;
// Only support showing jpg images. // Only support showing jpg images.
Uri uri = checkNotNull(inputMediaItem.localConfiguration).uri;
if (uri.toString().endsWith("jpg")) { if (uri.toString().endsWith("jpg")) {
inputPlayerView.setVisibility(View.GONE); inputPlayerView.setVisibility(View.GONE);
inputImageView.setVisibility(View.VISIBLE); inputImageView.setVisibility(View.VISIBLE);
@ -786,7 +766,7 @@ public final class TransformerActivity extends AppCompatActivity {
} }
} }
private void releasePlayer() { private void releasePlayers() {
if (debugTextViewHelper != null) { if (debugTextViewHelper != null) {
debugTextViewHelper.stop(); debugTextViewHelper.stop();
debugTextViewHelper = null; debugTextViewHelper = null;
@ -801,12 +781,22 @@ public final class TransformerActivity extends AppCompatActivity {
} }
} }
private static void requestReadVideoPermission(AppCompatActivity activity) { private void cleanUpExport() {
String permission = SDK_INT >= 33 ? READ_MEDIA_VIDEO : READ_EXTERNAL_STORAGE; if (transformer != null) {
if (ActivityCompat.checkSelfPermission(activity, permission) transformer.cancel();
!= PackageManager.PERMISSION_GRANTED) { transformer = null;
ActivityCompat.requestPermissions(activity, new String[] {permission}, /* requestCode= */ 0);
} }
if (outputFile != null) {
outputFile.delete();
outputFile = null;
}
if (oldOutputFile != null) {
oldOutputFile.delete();
oldOutputFile = null;
}
// The stop watch is reset after cancelling the export, in case cancelling causes the stop watch
// to be stopped in a transformer callback.
exportStopwatch.reset();
} }
private void showToast(@StringRes int messageResource) { private void showToast(@StringRes int messageResource) {