google / accompanist

A collection of extension libraries for Jetpack Compose
https://google.github.io/accompanist
Apache License 2.0
7.43k stars 598 forks source link

[WebView] after rotating, playing video stops playing as the embedding page gets reloaded #1661

Closed diegum closed 1 year ago

diegum commented 1 year ago

Description Unlike Google Chrome or Firefox, in which rotating the device while a video is playing in the browser doesn't alter the playback experience (the clip stays playing before, during and after the rotation,) the story is different with Jetpack Compose Accompanist WebView. I'm using the current version 0.31.3-beta, which already resolves the issue about the navigation state (thanks!!) But the playing video will either start from the beginning (if the page features autoplay,) or stop playing, but staying ready to play from the beginning.

Steps to reproduce

  1. I have a Composable sporting the Accompanist WebView (at the time of this writing, it's the latest version 0.31.3-beta.)
  2. I navigate to, namely, YouTube dot com.
  3. I select any video from its landing page. The WebView navigates to that video in YouTube and, because autoplay is on by default, the video (or possibly an ad) starts playing on its own. So far, so good.
  4. I rotate the device.
  5. As the page is reloaded -which I think, it shouldn't have happened- the video restarts from the beginning.

Expected behavior In spite of re-rendering the page, the video playback keeps its pace like if no rotation would have occurred.

Additional context I inspected the Accompanist implementation in version 0.31.3-beta. I see that WebView(...) accepts a factory that, given a context, returns a WebKit WebView. If the factory is null, such WebView is created every time. I tried to provide my factory reusing the same WebKit WebView after rotating, but the app crashed because that WebView already had a parent, besides leaking the context of the dismissed activity. I wish I could see a working sample passing the factory parameter. It seems a lot to me that the solution to my issue involves it.

bentrengrove commented 1 year ago

The only way around this is to opt out of config changes to stop the webview being destroyed and recreated.

Add this to your activity in the AndroidManifest

<activity
...
    android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
>