TheWidlarzGroup / react-native-video

A <Video /> component for react-native
https://docs.thewidlarzgroup.com/react-native-video/
MIT License
7.23k stars 2.9k forks source link

[BUG]: Android: Notification play control does not request audio focus or pay attention to other audio after one interruption #4209

Open paulrinaldi opened 1 month ago

paulrinaldi commented 1 month ago

Version

6.6.3

What platforms are you having the problem on?

Android

System Version

14

On what device are you experiencing the issue?

Real device, Simulator

Architecture

Old architecture

What happened?"

Original issue title: Tapping UnPause in Notification Media Controls does not pause external media

Notification Controls do not pause non-RNV media that is currently playing (e.g. YouTube). Additionally, if RNV continues playing after the scenario above, it does not pause when other audio starts playing.

Reproduction Link

https://github.com/TheWidlarzGroup/react-native-video/tree/master/examples/basic

Reproduction

Step to reproduce this bug are:

  1. Edit examples/basic/src/VideoPlayer.tsx's rnv videoplayer component's prop playInBackground={false} to playInBackground={true}
  2. Run yarn run android with a simulator open (video player app opens)
  3. Tap to Enable notification controls
  4. Background the app
  5. Open YouTube
  6. Play a video in YouTube to interrupt the RNV AV (observe this interrupt and pause the current video player)
  7. Slide down the notification controls
  8. Tap the play button in the notification controls of RNV

Actual Result:

  1. RNV's AV continues while YouTube's AV continues

Expected Result:

  1. RNV's AV continues but YouTube's AV is interrupted and pauses
github-actions[bot] commented 1 month ago
Previous bot comment (click to expand)
Previous bot comment (click to expand)
Previous bot comment (click to expand) Thank you for your bug report. We will review it and get back to you if we need more information.
paulrinaldi commented 1 month ago

Looks like we could call setPlayWhenReady(true) in order to request audio focus (see similar resumePlayback() in ReactExoPlayerView.kt). Will try this.

paulrinaldi commented 1 month ago

The implementation is more difficult than this.

Additionally if RNV continues playing after the scenario above, it does not pause when other audio starts playing.

After implementing an audio manager and listener in the VideoPlayerService, I still reproduced these bugs. This makes me think of abstracting the service. Will continue to implement a first fix.

This leads me The audio listening must even

paulrinaldi commented 1 month ago

Current work here: https://github.com/paulrinaldi/react-native-video/tree/fix-android-bkg-secondary-interruption-handling-and-focus-gaining

freeboub commented 1 month ago

@paulrinaldi Thank you for sharing you investigations 🙏

github-actions[bot] commented 1 month ago
Previous bot comment (click to expand)
Previous bot comment (click to expand) Thank you for your bug report. We will review it and get back to you if we need more information.
github-actions[bot] commented 1 month ago
Previous bot comment (click to expand) Thank you for your bug report. We will review it and get back to you if we need more information. There is a newer version of the library available. You are using version 6.6.3, while the latest stable version is 6.6.4. Please update to the latest version and check if the issue still exists. > Note: If the issue still exists, please update the issue report with the latest information.
github-actions[bot] commented 1 month ago

Thank you for your bug report. We will review it and get back to you if we need more information.

There is a newer version of the library available. You are using version 6.6.3, while the latest stable version is 6.6.4. Please update to the latest version and check if the issue still exists.

Note: If the issue still exists, please update the issue report with the latest information.

paulrinaldi commented 1 month ago

On android 14+ I realized that handleCommand is not receiving the toggle play event because notifications are build automatically on anything Tiramisu and higher so I need to look into what happens on android 14+.

paulrinaldi commented 1 month ago

For android 12-, Added a request but this request is not being accepted unfortunately to get control.

For android 14+, Still wondering why onPause and onPlay in the callback say they do not override anything so I commented them out.

So still not working on either.

Thesis for both is that both do not call requestAudioFocus, biggest struggle I'm facing is implementing that call given my unfamiliarity with Kotlin and Android. https://github.com/paulrinaldi/react-native-video/commit/a94707f7c2f1b9aaf0dd64d5374af4185073add5

paulrinaldi commented 1 month ago

Realized that focus is manually managed by the code in this library instead of letting exoplayer manage itself. Attempts to manually request focus though are failing to work and adding a listener in the service is also not effective.

paulrinaldi commented 1 month ago

Using just the exoplayer works well; testing setAudioAttributes(audioAttributes, true); instead of setAudioAttributes(audioAttributes, false); showed correct responses to an incoming phone call, outgoing phone call, and interruptions from youtube (though, I also made other changes see below). All AC's pass. Will continue to test this with a patch in my project but I wonder if there could be a manual way to implement this. Had much trouble doing that.

https://github.com/paulrinaldi/react-native-video/tree/fix-android-bkg-secondary-interruption-handling-and-focus-gaining

paulrinaldi commented 1 month ago

So if I let exoplayer handle its own pausing and playing due to interruptions, I can't detect interruptions manually for altering the external controls I have (e.g. to sync up for after youtube interrupts a video that is playing in the background).

paulrinaldi commented 1 month ago

I let exoplayer handle its own interruptions and just use the playback state change handler to ensure my external video controls nearby the player component to be synced on Android. Unfortunately, this means that after a phone call, the video does not resume. This is still better than the problem experienced earlier.

paulrinaldi commented 1 month ago

Any ideas?

paulrinaldi commented 1 month ago

"Note: If you use ExoPlayer, consider letting ExoPlayer automatically manage audio focus by calling setAudioAttributes with true for the handleAudioFocus parameter. With this behavior enabled, your app shouldn't include any code for requesting or responding to audio focus changes." - https://developer.android.com/media/optimize/audio-focus

Given this information, specifically that last line "or responding to audio focus changes", I would be suspicious for an audio listener to even detect an audio focus change event - however it may. I will explore this today because resumption after a phone call is the previous experience my users expect with react-native-video.

github-actions[bot] commented 1 week ago

This issue is stale because it has been open for 30 days with no activity. If there won't be any activity in the next 14 days, this issue will be closed automatically.