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

Add support for automatic wake lock acquisition/release in SimpleExoPlayer #5846

Closed
andrewlewis opened this issue May 7, 2019 · 1 comment
Assignees

Comments

@andrewlewis
Copy link
Collaborator

[REQUIRED] Use case description

Apps may need to acquire a wake lock when playing audio with the screen off (see #930 for details). For these use cases, it's possible for apps to acquire/release the wake lock themselves but generally will need to keep track of the player state to do this properly.

Proposed solution

Add support in SimpleExoPlayer for automatically acquiring/releasing a wake lock as needed based on player state.

Alternatives considered

n/a

@nomisRev
Copy link

nomisRev commented Aug 6, 2019

Hi,

I'd love to see something like, and/or at least some additional documentation in the documentation. Anyway, I can contribute to this?

Currently, I am using the following solution for this. (This has not been extensively tested in the field).

fun Context.locks(controller: MediaControllerCompat) = object : MediaControllerCompat.Callback() {

  private val acquired = AtomicBoolean(false)

  val wakeLock: PowerManager.WakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager)
    .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "musicservice:wakelock")

  val wifiLock: WifiManager.WifiLock = (applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager)
    .createWifiLock(WifiManager.WIFI_MODE_FULL, "musicservice:wifilock")

  override fun onMetadataChanged(metadata: MediaMetadataCompat?) {
    controller.playbackState?.let(this::updateNotification)
  }

  override fun onPlaybackStateChanged(state: PlaybackStateCompat?) {
    state?.let(this::updateNotification)
  }

  private fun updateNotification(state: PlaybackStateCompat) = when (state.state) {
    PlaybackStateCompat.STATE_BUFFERING, PlaybackStateCompat.STATE_PLAYING -> acquire()
    PlaybackStateCompat.STATE_STOPPED, PlaybackStateCompat.STATE_PAUSED -> release()
    else -> { }
  }

  @SuppressLint("WakelockTimeout")
  private fun acquire() {
    if (acquired.compareAndSet(false, true)) {
      Timber.wtf("Releasing WakeLock & WifiLock")
      wakeLock.acquire()
      wifiLock.acquire()
    }
  }

  private fun release() {
    if (acquired.compareAndSet(true, false)) {
      Timber.wtf("Releasing WakeLock & WifiLock")
      wakeLock.release()
      wifiLock.release()
    }
  }
}

And it can be consumed as follows.

private fun MediaBrowserServiceCompat.createMediaController(mediaSession: MediaSessionCompat) =
  MediaControllerCompat(this, mediaSession).apply {
    registerCallback(audioBecomingNoisyHandler(mediaSession.sessionToken))
    registerCallback(locks(controller))
  }

@andrewlewis andrewlewis assigned Samrobbo and unassigned andrewlewis Sep 18, 2019
ojw28 pushed a commit that referenced this issue Oct 2, 2019
Created the WakeLockManager for use in SimpleExoPlayer.
Added a setter in SimpleExoPlayer to adjust this functionality.

Issue:#5846
PiperOrigin-RevId: 272176998
@ojw28 ojw28 closed this as completed Oct 2, 2019
@google google locked and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants