Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting repeat mode with Intent arguments #1266

Merged
merged 4 commits into from
Apr 16, 2024

Conversation

hubert-mazur
Copy link

Add a new boolean command line optional argument to allow the playback to run in loop. This would help to use the app in the automated video tests. The change does not enable the option view for switching the loop mode in the UI as providing an extra argument implies force, not optional, loop playback.

@marcbaechinger marcbaechinger self-assigned this Apr 11, 2024
@marcbaechinger marcbaechinger self-requested a review April 11, 2024 08:58
@@ -293,6 +293,9 @@ protected boolean initializePlayer() {
}
player.setMediaItems(mediaItems, /* resetPosition= */ !haveStartPosition);
player.prepare();
if (getIntent().getBooleanExtra(IntentUtil.LOOP_PLAYBACK_EXTRA, false)) {
Copy link
Contributor

@marcbaechinger marcbaechinger Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind making a REPEAT_MODE_EXTRA and a REPEAT_MODE_EXTRA_VALUE_NONE, REPEAT_MODE_EXTRA_VALUE_ONE, REPEAT_MODE_EXTRA_VALUE_ALL so we cover the entire repeat mode API?

I understand the idea is to start the app directly into the PlayerActivity from the command line. We have documented the Intent arguments we support here: https://developer.android.com/media/media3/exoplayer/demo-application#3-firing

I will document this on DAC once we have landed that change. The aim is to have an addition like the following

--es repeat_mode NONE | ONE | ALL

WDYT?

See https://gist.github.com/tsohr/5711945#file-gistfile1-txt-L81

@marcbaechinger
Copy link
Contributor

Thanks for your PR! Please take a look at my suggestion in the comment!

@hubert-mazur
Copy link
Author

Thanks for the feedback! I'll do the changes and upload the second version soon.

@hubert-mazur
Copy link
Author

Hi, I uploaded the v2 of the change. Let me briefly describe the idea.

Per suggestions, I changed the optional argument repeat_mode to a String that should receive one of the following options which correspond to the Player API: NONE|ONE|ALL.

If provided, it is validated and set appropriately. In case of malicious input, the default (NONE) will be set.
Setting the player mode will only happen in case where the optional argument was provided. Otherwise nothing is set. I thought that this approach is the best, since it is an optional argument and if none provided, no action, even a default one should be taken.

Please take a look and share your thoughts.

@icbaker
Copy link
Collaborator

icbaker commented Apr 12, 2024

I would suggest making this stricter:

  • Only accept the exact strings "NONE", "ONE" and "ALL"
  • Fail if an unrecognized string is received

The issue with silently falling back to a default is it can be quite confusing if someone expects --es repeat_mode all or --es repeat_mode REPEAT_MODE_ALL to work and there's no signal (and nothing in logcat) to explain why it didn't.

You can see an existing example of an 'invalid parse' of an intent extra resulting in a failure by trying this:

$ adb shell am start -a androidx.media3.demo.main.action.VIEW \
       -d  "https://html5demos.com/assets/dizzy.mp4" \
       --es drm_scheme widevine \
       --esa drm_key_request_properties foo,bar,baz

This fails with an exception like:

java.lang.RuntimeException: Unable to resume activity {androidx.media3.demo.main/androidx.media3.demo.main.PlayerActivity}: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
...
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
                                                                                                    	at androidx.media3.demo.main.IntentUtil.populateDrmPropertiesFromIntent(IntentUtil.java:178)

Because we assume the array has an even length here:

for (int i = 0; i < keyRequestPropertiesArray.length; i += 2) {
headers.put(keyRequestPropertiesArray[i], keyRequestPropertiesArray[i + 1]);
}

@hubert-mazur
Copy link
Author

Ah, that way. Okay, this seems reasonable to fail if the argument is invalid. In that case, how about throwing an IllegalArgumentException out from getPlayerRepeatMode if the option can't get a valid match? This won't let the run app and confuse the user.

@icbaker
Copy link
Collaborator

icbaker commented Apr 12, 2024

Yep, sounds reasonable. IMO you can also remove the enum and just have a string-switch in a static method on IntentUtil:

private static @Player.RepeatMode int parseRepeatMode(String repeatMode) {
  switch (repeatMode) {
    case "ALL":
      return Player.REPEAT_MODE_ALL
    ...
    default:
      throw new IllegalArgumentException("Unrecognized repeat mode: '" + repeatMode "');
}

@hubert-mazur
Copy link
Author

Thanks Ian for the hints! I pushed the changes.

@marcbaechinger marcbaechinger changed the title Introduce a new boolean argument Allow setting repeat mode with Intent arguments Apr 15, 2024
Hubert Mazur and others added 2 commits April 15, 2024 10:54
Add a new string command line optional argument to allow the playback
to run in loop. This would help to use the app in the automated video
tests. The change does not enable the option view for switching the loop
mode in the UI as providing an extra argument implies force, not
optional, loop playback. The extra option is provided with "repeat_mode"
key and one of the possible modes can be specified: NONE|ONE|ALL. The
available options correspond to the Player API to ensure compability.
@marcbaechinger
Copy link
Contributor

I'm going to send this for internal review now. You may see some more commits being added as I make changes in response to review feedback. Please refrain from pushing any more substantive changes as it will complicate the internal review - thanks!

@marcbaechinger marcbaechinger force-pushed the looped_playback branch 2 times, most recently from e1944d3 to bfd5b84 Compare April 15, 2024 09:13
@copybara-service copybara-service bot merged commit 236c341 into androidx:main Apr 16, 2024
1 check passed
@androidx androidx locked and limited conversation to collaborators Jun 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants