Repository: flutter
Issue: [video_player] The video player temporarily hides when the app resumes on Android 11 above.
Labels: platform-android,p: video_player,package,e: OS-version specific,P2,team-android,triaged-android

### Steps to reproduce

The video player temporarily hides when the app resumes on Android 11 above.

### Expected results

Ensure the video always displays and play when the app resumes.

### Actual results

<details open><summary>Logs</summary>

```console
-> App is backgroud

I/OplusFeedbackInfo(31957): 0xb4000075b3e57400 c2.mtk.avc.decoder 1080x1080 inputFps=28 outputFps=27 renderFps=30, discardFps=0
D/BufferPoolAccessor2.0(31957): bufferpool2 0xb400007545830a28 : 9(18846720 size) total buffers - 9(18846720 size) used buffers - 576/585 (recycle/alloc) - 9/584 (fetch/transfer)
D/VRI[MainActivity](31957): onFocusEvent false
I/flutter (31957): AppLifecycleState --> AppLifecycleState.inactive
D/ViewRootImplExtImpl(31957): onDisplayChanged 0 for VRI android.view.ViewRootImpl@51b0a06
I/OplusFeedbackInfo(31957): 0xb4000075b3e57400 c2.mtk.avc.decoder 1080x1080 inputFps=30 outputFps=30 renderFps=30, discardFps=0
D/VRI[MainActivity](31957): dispatchAppVisibility visible:false
I/SurfaceView(31957): 228513960 surfaceDestroyed
D/BLASTBufferQueue(31957): [SurfaceView[com.example.festo_post/com.example.festo_post.MainActivity]#4](f:0,a:1) destructor()
D/BufferQueueConsumer(31957): [SurfaceView[com.example.festo_post/com.example.festo_post.MainActivity]#4(BLAST Consumer)4](id:7cd500000007,api:0,p:-1,c:31957) disconnect
D/SurfaceView(31957): 196013809positionLost mSurfaceControl is null return;
D/CCodecBufferChannel(31957): [c2.mtk.avc.decoder#751] DEBUG: elapsed: mInputMetEos 17, hasPendingOutputsInClient 0, n=1 [in=4 pipeline=0 out=13]
D/PipelineWatcher(31957): [0xb4000075085da280] elapsed: mFramesInPipeline 9, n 17
D/BLASTBufferQueue(31957): [VRI[MainActivity]#3](f:0,a:1) destructor()
D/BufferQueueConsumer(31957): [VRI[MainActivity]#5(BLAST Consumer)5](id:7cd500000009,api:0,p:-1,c:31957) disconnect
D/VRI[MainActivity](31957): setWindowStopped stopped:true
D/BufferQueueConsumer(31957): [ImageReader-1x1f22m5-31957-2](id:7cd500000008,api:3,p:31957,c:31957) disconnect
I/ExoPlayerImpl(31957): Release 84e25e0 [AndroidXMedia3/1.4.1] [OP555BL1, IV2201, OnePlus, 33] [media3.common, media3.exoplayer, media3.decoder, media3.datasource, media3.extractor]
E/BufferQueueProducer(31957): [ImageReader-1x1f22m5-31957-2](id:7cd500000008,api:3,p:31957,c:31957) queueBuffer: BufferQueue has been abandoned
E/Codec2-OutputBufferQueue(31957): outputBuffer -- queueBuffer() failed on bufferqueue-based block. Error = -19.
I/CCodecBufferChannel(31957): [c2.mtk.avc.decoder#204] queueBuffer failed: -19
E/OplusCCodec(31957): onError [68]: (0xb4000075b7ae3380) err=-2147483648 actionCode=0
E/MediaCodec(31957): rendering to non-initilized(obsolete) surface
E/MediaCodec(31957): Codec reported err 0x80000000/UNKNOWN_ERROR, actionCode 0, while in state 6/STARTED
D/MediaCodec(31957): flushMediametrics
D/MediaCodec(31957): [0xb4000075b7aea000] setState: 0
D/MediaCodec(31957): [0xb4000075b7aea000] [c2.mtk.avc.decoder] disconnectFromSurface: mSurface 0xb4000075b7a61000


-> App is foreground

D/VRI[MainActivity](31957): dispatchAppVisibility visible:true
D/VRI[MainActivity](31957): setWindowStopped stopped:false
I/flutter (31957): AppLifecycleState --> AppLifecycleState.hidden
I/flutter (31957): AppLifecycleState --> AppLifecycleState.inactive
I/ExoPlayerImpl(31957): Init 867f62b [AndroidXMedia3/1.4.1] [OP555BL1, IV2201, OnePlus, 33]
D/BufferQueueConsumer(31957): [](id:7cd50000000b,api:0,p:-1,c:31957) connect: controlledByApp=true
I/Quality (31957): Skipped: false 2 cost 31.380924 refreshRate 11052179 bit true processName com.example.festo_post
D/BufferQueueConsumer(31957): [](id:7cd50000000c,api:0,p:-1,c:31957) connect: controlledByApp=false
E/IPCThreadState(31957): attemptIncStrongHandle(65): Not supported
E/IPCThreadState(31957): attemptIncStrongHandle(61): Not supported
D/TrafficStats(31957): tagSocket(144) with statsTag=0xffffffff, statsUid=-1
D/BufferQueueConsumer(31957): [](id:7cd50000000d,api:0,p:-1,c:31957) connect: controlledByApp=false
I/SurfaceView(31957): 228513960 visibleChanged -- surfaceCreated 
E/gralloc4(31957): ERROR: Format allocation info not found for format: 38
E/gralloc4(31957): ERROR: Format allocation info not found for format: 0
E/gralloc4(31957): Invalid base format! req_base_format = 0x0, req_format = 0x38, type = 0x0
E/gralloc4(31957): ERROR: Unrecognized and/or unsupported format 0x38 and usage 0xb00
E/Gralloc4(31957): isSupported(1, 1, 56, 1, ...) failed with 5
E/GraphicBufferAllocator(31957): Failed to allocate (4 x 4) layerCount 1 format 56 usage b00: 5
E/AHardwareBuffer(31957): GraphicBuffer(w=4, h=4, lc=1) failed (Unknown error -5), handle=0x0
E/gralloc4(31957): ERROR: Format allocation info not found for format: 38
E/gralloc4(31957): ERROR: Format allocation info not found for format: 0
E/gralloc4(31957): Invalid base format! req_base_format = 0x0, req_format = 0x38, type = 0x0
E/gralloc4(31957): ERROR: Unrecognized and/or unsupported format 0x38 and usage 0xb00
E/Gralloc4(31957): isSupported(1, 1, 56, 1, ...) failed with 5
E/GraphicBufferAllocator(31957): Failed to allocate (4 x 4) layerCount 1 format 56 usage b00: 5
E/AHardwareBuffer(31957): GraphicBuffer(w=4, h=4, lc=1) failed (Unknown error -5), handle=0x0
I/SurfaceView(31957): 228513960 surfaceChanged -- format=4 w=1080 h=2359
D/VRI[MainActivity](31957): registerCallbacksForSync syncBuffer=false
D/BLASTBufferQueue(31957): [VRI[MainActivity]#7](f:0,a:1) acquireNextBufferLocked size=1080x2400 mFrameNumber=1 applyTransaction=true mTimestamp=64878924207481(auto) mPendingTransactions.size=0 graphicBufferId=137254269879371 transform=0
D/VRI[MainActivity](31957): Received frameCommittedCallback lastAttemptedDrawFrameNum=1 didProduceBuffer=true syncBuffer=false
D/VRI[MainActivity](31957): draw finished.
D/BLASTBufferQueue(31957): [SurfaceView[com.example.festo_post/com.example.festo_post.MainActivity]#8](f:0,a:1) acquireNextBufferLocked size=1080x2359 mFrameNumber=1 applyTransaction=true mTimestamp=64878949113327(auto) mPendingTransactions.size=0 graphicBufferId=137254269879366 transform=0
D/VRI[MainActivity](31957): onFocusEvent true
I/flutter (31957): AppLifecycleState --> AppLifecycleState.resumed
D/ViewRootImplExtImpl(31957): onDisplayChanged 0 for VRI android.view.ViewRootImpl@51b0a06
I/DMCodecAdapterFactory(31957): Creating an asynchronous MediaCodec adapter for track type video
D/TrafficStats(31957): tagSocket(144) with statsTag=0xffffffff, statsUid=-1
D/MediaCodec(31957): CreateByComponentName: name c2.mtk.avc.decoder
```

</details>


### Code sample

<details open><summary>Code sample</summary>

```dart

  late VideoPlayerController videoPlayerController;
  bool videoInitialized = false;
  
  Container(
     child: viewModel.videoInitialized
         ? AspectRatio(
             aspectRatio: viewModel.videoPlayerController.value.aspectRatio,
             child: InkWell(
               onTap: () => viewModel.videoPlayback(),
               child: VideoPlayer(viewModel.videoPlayerController),
         ),
     ) : CupertinoActivityIndicator(color: ColorRes.blue1E2A38),
  )

  void initializeVideo({String? url}) {
    videoInitialized = false;
    notifyListeners();
    videoPlayerController = VideoPlayerController.networkUrl(Uri.parse(url ?? postVideo[selectPostIndex].image ?? ''))
      ..initialize().then((_) async {
        await videoPlayerController.play();
        videoPlayerController.setLooping(true);
        videoInitialized = true;
        notifyListeners();
      });
  }

 Future<void> videoPlayback() async {
     if (videoPlayerController.value.isPlaying == false || videoPlayerController.value.isCompleted) {
        await videoPlayerController.play();
      } else {
        await videoPlayerController.pause();
      }
      notifyListeners();
  }

 @override
  void dispose() {
    debugPrint("video disposed...");
    WidgetsBinding.instance.removeObserver(this);
    videoPlayerController.dispose();
    super.dispose();
  }

```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

Android 11

https://github.com/user-attachments/assets/6b98c197-9473-419a-9eeb-6d2c9ead6a6f

Android 11 above 


https://github.com/user-attachments/assets/dd301aa8-031b-4176-a02d-bab1ff2d0d16



</details>




### Logs

<details open><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[√] Flutter (Channel stable, 3.27.1, on Microsoft Windows [Version 10.0.22621.4317], locale en-US)
    • Flutter version 3.27.1 on channel stable at D:\flutterSDK
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 17025dd882 (2 weeks ago), 2024-12-17 03:23:09 +0900
    • Engine revision cb4b5fff73
    • Dart version 3.6.0
    • DevTools version 2.40.2

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at D:\androidSdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: D:\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[X] Visual Studio - develop Windows apps
    X Visual Studio not installed; this is necessary to develop Windows apps.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components

[√] Android Studio (version 2024.2)
    • Android Studio at D:\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • android-studio-dir = D:\Android Studio
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)

[√] Connected device (4 available)
    • Mi A3 (mobile)    • adb-492fb60d2fec-hqZFnj._adb-tls-connect._tcp • android-arm64  • Android 11 (API 30)
    • Windows (desktop) • windows                                       • windows-x64    • Microsoft Windows [Version 10.0.22621.4317]
    • Chrome (web)      • chrome                                        • web-javascript • Google Chrome 131.0.6778.205
    • Edge (web)        • edge                                          • web-javascript • Microsoft Edge 131.0.2903.112

[√] Network resources
    • All expected network resources are available.

```

</details>