media icon indicating copy to clipboard operation
media copied to clipboard

Wrong resource pulled from raw folders. (theme issue)

Open a-dimovska7 opened this issue 2 years ago • 7 comments

Version

Media3 1.2.0

More version details

No response

Devices that reproduce the issue

Samsung S20 FE 5G

Devices that do not reproduce the issue

No response

Reproducible in the demo app?

No

Reproduction steps

  1. Add a video in raw and raw-night. (each having the same resource ID).
  2. Set your app theme to night mode.
  3. Set your device theme to light mode.
  4. Load the video with exo player.

Expected result

The video will be loaded according to the app theme. (in this case, from raw-night)

Actual result

The video is loaded according to the phone theme (from raw)

Media

not applicable

Bug Report

  • [X] You will email the zip file produced by adb bugreport to [email protected] after filing this issue.

a-dimovska7 avatar Dec 19 '23 14:12 a-dimovska7

I tried this in the demo app by adding two text files with different content:

  • raw/test.txt: day time text file
  • raw-night/test.txt: night time text file

I then added the following code to SampleChooserActivity.onCreate, to compare the behaviour of ExoPlayer's RawResourceDataSource to the Android framework's Resource.openRawResource:

Log.w("issue-908", "raw.test=" + R.raw.test);
try (InputStreamReader isr =
    new InputStreamReader(getResources().openRawResource(R.raw.test))) {
  Log.w("issue-908", "From openRawResource: content=" + CharStreams.readLines(isr));
} catch (IOException e) {
  throw new RuntimeException(e);
}

RawResourceDataSource rawResourceDataSource = null;
try {
  rawResourceDataSource = new RawResourceDataSource(this);
  rawResourceDataSource.open(
      new DataSpec(
          new Uri.Builder()
              .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
              .path(Integer.toString(R.raw.test))
              .build()));
  byte[] bytes = DataSourceUtil.readToEnd(rawResourceDataSource);
  Log.w("issue-908", "From RawResourceDataSource: content=" + Util.fromUtf8Bytes(bytes));
} catch (IOException e) {
  throw new RuntimeException(e);
} finally {
  DataSourceUtil.closeQuietly(rawResourceDataSource);
}

With my Pixel 7a device in 'day mode' (i.e. dark theme disabled in settings), I observed:

raw.test=2131886081
From openRawResource: content=[day time text file]
From RawResourceDataSource: content=day time text file

And in 'night mode' with dark theme enabled:

raw.test=2131886081
From openRawResource: content=[night time text file]
From RawResourceDataSource: content=night time text file

i.e. it seems that RawResourceDataSource is respecting the device's dark mode toggle, and I'm not able to reproduce the issue you describe.

Please can you add more detail about your setup, in particular what's different from my repro attempt.

icbaker avatar Dec 21 '23 16:12 icbaker

Hi Ian, The issue is regarding videos in dark and light mode. Try reproducing it by placing different videos (I was using .mp4) under the same name in raw and raw-night, then loading them with ExoPlayer.

  • raw/video.mp4
  • raw-night/video.mp4

val videoUri = RawResourceDataSource.buildRawResourceUri(R.raw.video) val mediaItem = MediaItem.fromUri(videoUri)

If your app is in dark theme, but your phone in light theme, this always follows the system and not the app.

Restricted

From: Ian Baker @.> Date: Thursday, 21 December 2023 at 17:57 To: androidx/media @.> Cc: Angela Dimovska @.>, Author @.> Subject: Re: [androidx/media] Wrong resource pulled from raw folders. (theme issue) (Issue #908)

CAUTION: This email originated from outside the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.


I tried this in the demo app by adding two text files with different content:

  • raw/test.txt: day time text file
  • raw-night/test.txt: night time text file

I then added the following code to SampleChooserActivity.onCreate, to compare the behaviour of ExoPlayer's RawResourceDataSourcehttps://developer.android.com/reference/androidx/media3/datasource/RawResourceDataSource to the Android framework's Resource.openRawResourcehttps://developer.android.com/reference/android/content/res/Resources#openRawResource(int):

Log.w("issue-908", "raw.test=" + R.raw.test);

try (InputStreamReader isr =

new InputStreamReader(getResources().openRawResource(R.raw.test))) {

Log.w("issue-908", "From openRawResource: content=" + CharStreams.readLines(isr));

} catch (IOException e) {

throw new RuntimeException(e);

}

RawResourceDataSource rawResourceDataSource = null;

try {

rawResourceDataSource = new RawResourceDataSource(this);

rawResourceDataSource.open(

  new DataSpec(

      new Uri.Builder()

          .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)

          .path(Integer.toString(R.raw.test))

          .build()));

byte[] bytes = DataSourceUtil.readToEnd(rawResourceDataSource);

Log.w("issue-908", "From RawResourceDataSource: content=" + Util.fromUtf8Bytes(bytes));

} catch (IOException e) {

throw new RuntimeException(e);

} finally {

DataSourceUtil.closeQuietly(rawResourceDataSource);

}

With my Pixel 7a device in 'day mode' (i.e. dark theme disabled in settings), I observed:

raw.test=2131886081

From openRawResource: content=[day time text file]

From RawResourceDataSource: content=day time text file

And in 'night mode' with dark theme enabled:

raw.test=2131886081

From openRawResource: content=[night time text file]

From RawResourceDataSource: content=night time text file

i.e. it seems that RawResourceDataSource is respecting the device's dark mode toggle, and I'm not able to reproduce the issue you describe.

Please can you add more detail about your setup, in particular what's different from my repro attempt.

— Reply to this email directly, view it on GitHubhttps://github.com/androidx/media/issues/908#issuecomment-1866644791, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BEZP6MD347EVBO7NOSZHO5TYKRS6TAVCNFSM6AAAAABA3JXL6GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNRWGY2DINZZGE. You are receiving this because you authored the thread.Message ID: @.***>

a-dimovska7 avatar Jan 02 '24 13:01 a-dimovska7

Ah I missed that your repro steps require the system to be in day mode and the app in night mode. I adapted the demo app to behave like this as follows (based on https://developer.android.com/develop/ui/views/theming/darktheme):

Change the PlayerTheme to inherit from Theme.AppCompat.DayNight.NoActionBar

<style name="PlayerTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
  <item name="android:windowBackground">@android:color/black</item>
</style>

Change the mode programatically before the reading code described above in https://github.com/androidx/media/issues/908#issuecomment-1866644791:

UiModeManager uiModeManager = getSystemService(UiModeManager.class);
uiModeManager.setApplicationNightMode(UiModeManager.MODE_NIGHT_YES);

I continue to observe the night samples being read by both Android's openRawResource and media3's RawResourceDataSource:

From openRawResource: content=[night time text file]
From RawResourceDataSource: content=night time text file

Please can you describe what is different in your app to my repro example? Currently it seems to me that media3's RawResourceDataSource is acting consistently with the framework's resource handling.

I don't think it matters whether the file being read is a text file or a video file - both are 'raw resources' and just being treated as opaque bytes.

icbaker avatar Jan 02 '24 17:01 icbaker

Hey @a-dimovska7. We need more information to resolve this issue but there hasn't been an update in 14 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Jan 22 '24 02:01 google-oss-bot

Hi Ian,

Sorry for the delayed response, I was away from work. This is the current implementation in my app.

  1. This is the way I change the app theme to be different from the phone (light or dark):
  • AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
  • AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
  1. This is the way I have my videos saved in my resources folders:
  • raw/video.mp4
  • raw-night/video.mp4
  1. Here is how I load the video:

@Composable

fun BasicExoPlayer(modifier: Modifier = Modifier, @RawRes rawResourceId: Int) {

val context = LocalContext.current

val videoUri = RawResourceDataSource.buildRawResourceUri(rawResourceId)

val mediaItem = MediaItem.fromUri(videoUri)



val exoPlayer = remember {

    ExoPlayer.Builder(context).build().apply {

        setMediaItem(mediaItem)

        repeatMode = Player.REPEAT_MODE_ONE

        this.prepare()

        this.playWhenReady = true

    }

}



CompositionLocalProvider(LocalExoPlayer provides exoPlayer) {

    VideoPlayer(modifier, rawResourceId)

}

}

fun VideoPlayer(modifier: Modifier = Modifier, @RawRes rawResourceId: Int) {

val context = LocalContext.current

val exoPlayer = LocalExoPlayer.current



DisposableEffect(

    AndroidView(

        modifier = modifier,

        factory = {

            PlayerView(context).also {

                it.player = exoPlayer

                it.useController = false

                it.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIXED_WIDTH

            }

        }

    )

) {

    onDispose {

        exoPlayer.release()

    }

}

}

If the app is in light mode and the phone in dark mode, the videos are in dark mode following the system settings. (and vice versa)

Kind regards, Angela

Restricted

From: Ian Baker @.> Date: Tuesday, 2 January 2024 at 18:58 To: androidx/media @.> Cc: Angela Dimovska @.>, Author @.> Subject: Re: [androidx/media] Wrong resource pulled from raw folders. (theme issue) (Issue #908)

CAUTION: This email originated from outside the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.


Ah I missed that your repro steps require the system to be in day mode and the app in night mode. I adapted the demo app to behave like this as follows (based on https://developer.android.com/develop/ui/views/theming/darktheme):

Change the PlayerTheme to inherit from Theme.AppCompat.DayNight.NoActionBar

Change the mode programatically before the reading code described above in #908 (comment)https://github.com/androidx/media/issues/908#issuecomment-1866644791:

UiModeManager uiModeManager = getSystemService(UiModeManager.class);

uiModeManager.setApplicationNightMode(UiModeManager.MODE_NIGHT_YES);

I continue to observe the night samples being read by both Android's openRawResource and media3's RawResourceDataSource:

From openRawResource: content=[night time text file]

From RawResourceDataSource: content=night time text file

Please can you describe what is different in your app to my repro example? Currently it seems to me that media3's RawResourceDataSource is acting consistently with the framework's resource handling.

I don't think it matters whether the file being read is a text file or a video file - both are 'raw resources' and just being treated as opaque bytes.

— Reply to this email directly, view it on GitHubhttps://github.com/androidx/media/issues/908#issuecomment-1874358042, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BEZP6MBPOOLLD4RWMI5BKB3YMRDEJAVCNFSM6AAAAABA3JXL6GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZUGM2TQMBUGI. You are receiving this because you authored the thread.Message ID: @.***>

a-dimovska7 avatar Jan 29 '24 13:01 a-dimovska7

I'm afraid I'm still unable to reproduce the problem.

Using AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) and with the device in system-wide light mode I observe the 'night time text file' being read from both Context.getResources().openRawResource() and RawResourceDataSource.

Using AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) and with the device in system-wide dark mode I observe the 'day time text file' being read.


Please can you see which file is read by Context.getResources().openRawResource() without RawResourceDataSource in your problematic set-up? If it's also the wrong file, then there's something going wrong beyond media3.

icbaker avatar Feb 08 '24 16:02 icbaker

Hey @a-dimovska7. We need more information to resolve this issue but there hasn't been an update in 14 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Feb 28 '24 02:02 google-oss-bot

Since there haven't been any recent updates here, I am going to close this issue.

@a-dimovska7 if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

google-oss-bot avatar Mar 07 '24 02:03 google-oss-bot