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

Correct backgrounding behavior for both video and audio (does not work) #4162

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

mcclure
Copy link
Collaborator

@mcclure mcclure commented Dec 11, 2023

See #4161 for full explanation.

This is a followup to #4160. It causes keep-screen-alive to only occur for videos (and GIFVs), and for audio attachments it uses setWakeMode (with the required permission added to AndroidManifest.xml). This patch does not work. I can confirm we are hitting the setWakeMode branch*, but when the screen goes to sleep the audio also stops. The documentation says that setWakeMode "should be used together with a foreground android.app.Service for use cases where playback occurs and the screen is off (e.g. background audio playback)", and we have a foreground service but there is no specific provision to switch the audio playback from the activity to the foreground service. I do not know what to do from here.

If the foreground service could assume audio playback, would that mean we could allow audio attachments to keep playing even after the user switches to another app? I think that would be pretty neat.

* I added a log.d to the setWakeMode if, and it printed. I looked in the logcat and there were no warnings around the setWakeMode indicating a permission denial or anything. One minute later I see in the log:

ActivityTrigger activityPauseTrigger

I then immediately see a bunch of audio-related messages I can't decipher, such as:

2023-12-10 20:42:26.887  6141-6315  Avrcp_ext               pid-6141                             D  AudioManager Player: AudioPlaybackConfiguration piid:55 deviceId:0 type:android.media.SoundPool u/pid:1000/1445 state:idle attr:AudioAttributes: usage=USAGE_ASSISTANCE_SONIFICATION content=CONTENT_TYPE_SONIFICATION flags=0x800 tags= bundle=null sessionId:0
2023-12-10 20:42:26.887  6141-6315  Avrcp_ext               pid-6141                             D  AudioManager Player: AudioPlaybackConfiguration piid:71 deviceId:0 type:android.media.SoundPool u/pid:1027/4928 state:idle attr:AudioAttributes: usage=USAGE_ASSISTANCE_SONIFICATION content=CONTENT_TYPE_SONIFICATION flags=0x800 tags= bundle=null sessionId:0
2023-12-10 20:42:26.887  6141-6315  Avrcp_ext               pid-6141                             D  AudioManager Player: AudioPlaybackConfiguration piid:503 deviceId:3 type:android.media.AudioTrack u/pid:10749/14450 state:paused attr:AudioAttributes: usage=USAGE_MEDIA content=CONTENT_TYPE_UNKNOWN flags=0xA00 tags= bundle=null sessionId:633
2023-12-10 20:42:26.887  6141-6315  Avrcp_ext               pid-6141                             D  AudioManager Player: AudioPlaybackConfiguration piid:63 deviceId:0 type:android.media.SoundPool u/pid:10303/2655 state:idle attr:AudioAttributes: usage=USAGE_ASSISTANCE_SONIFICATION content=CONTENT_TYPE_SONIFICATION flags=0x800 tags= bundle=null sessionId:0
2023-12-10 20:42:26.887  6141-6315  Avrcp_ext               pid-6141                             D  AudioManager isPlaying: false, mAudioPlaybackIsActive = true
2023-12-10 20:42:26.887  6141-6315  Avrcp_ext               pid-6141                             D  AudioManager Reset Active Player
2023-12-10 20:42:26.887  6141-6315  Avrcp_ext               pid-6141                             V  updateCurrentMediaState: mMediaController: null
2023-12-10 20:42:26.888  6141-6315  Avrcp_ext               pid-6141                             W  isMusicActive: true getBluetoothPlayState: 1 A2dp State: 11 mAudioPlaybackIsActive: false
2023-12-10 20:42:26.888  6141-6315  Avrcp_ext               pid-6141                             W  updateCurrentMediaState: isPlaying = true
2023-12-10 20:42:26.888  6141-6315  Avrcp_ext               pid-6141                             V  Media update: id [MediaAttributes: none] : [MediaAttributes: none]
2023-12-10 20:42:26.888  6141-6315  Avrcp_ext               pid-6141                             D  isPackageNameValid: browsedPackage = isValid = false
2023-12-10 20:42:26.888  6141-6315  Avrcp_ext               pid-6141                             V  updatePlaybackState, state: PlaybackState {state=3, position=-1, buffered position=0, speed=1.0, updated=96108045, actions=0, custom actions=[], active item id=-1, error=null} device: null

There were more similar messages after this but I didn't catch them all before they cleared. Various objects claiming both that the music is playing and is not playing.

@duskmoss
Copy link

Do you mind if I take a look at getting this hooked up with the foreground service?

@mcclure
Copy link
Collaborator Author

mcclure commented Jan 23, 2024

Feel free, I am no longer looking at this as I do not have background with the parts that need to be connected.

@cbeyls
Copy link
Contributor

cbeyls commented Apr 17, 2024

This pull request is now outdated and should either be closed or rewritten.

Here are a few updates:

  • ViewMediaActivity now contains a method adjustScreenWakefulness() which sets FLAG_KEEP_SCREEN_ON when the content is audio, gif or video (anything but image). So the screen is properly left on during video playback.
  • ExoPlayer and media3 are able to handle the wake locks at the player level using setWakeMode(), but this is currently disabled because wake locks need to work in combination with a foreground service to ensure the OS will not kill the app during background playback. Since currently the audio playback only works from a regular activity in the foreground and background audio playback is not officially supported yet, then no wake locks are needed. A future pull request may be created to add proper support for background audio playback using both wake lock and a foreground service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants