Skip to content

Commit 5afd932

Browse files
fix(android): wire stayAwake to ExoPlayer wake mode in audioplayers_android_exo
The exo implementation parses the stayAwake flag of AudioContextAndroid (AudioplayersPlugin requires it in the method channel) but never consumes it: the ExoPlayer is built without setWakeMode, so no wake/wifi lock is held during playback and network streaming with the screen off depends entirely on locks held by other plugins (e.g. audio_service). Map stayAwake=true to C.WAKE_MODE_NETWORK (partial wake lock + wifi lock, only held while actually playing), mirroring the stayAwake handling of audioplayers_android (MediaPlayer.setWakeMode in updateContext). Applied both at player creation and on context updates.
1 parent 687218b commit 5afd932

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

  • packages/audioplayers_android_exo/android/src/main/kotlin/xyz/luan/audioplayers/player

packages/audioplayers_android_exo/android/src/main/kotlin/xyz/luan/audioplayers/player/ExoPlayerWrapper.kt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,26 @@ class ExoPlayerWrapper(
8989
}
9090
}
9191

92-
return ExoPlayer.Builder(appContext).setRenderersFactory(renderersFactory).build().apply {
93-
addListener(ExoPlayerListener(wrappedPlayer))
94-
}
92+
return ExoPlayer.Builder(appContext)
93+
.setRenderersFactory(renderersFactory)
94+
.setWakeMode(wakeModeFor(wrappedPlayer.context))
95+
.build().apply {
96+
addListener(ExoPlayerListener(wrappedPlayer))
97+
}
98+
}
99+
100+
/**
101+
* Maps the `stayAwake` flag of the [AudioContextAndroid] to an ExoPlayer wake mode.
102+
*
103+
* [C.WAKE_MODE_NETWORK] holds both a partial wake lock and a wifi lock while the player
104+
* is actively playing, so playback of remote sources survives the screen turning off.
105+
* It is a superset of [C.WAKE_MODE_LOCAL] and is used unconditionally, as the source
106+
* (local or remote) is not known when the audio context is applied.
107+
* Requires the `android.permission.WAKE_LOCK` permission, same as the `stayAwake`
108+
* implementation of `audioplayers_android`.
109+
*/
110+
private fun wakeModeFor(context: AudioContextAndroid): Int {
111+
return if (context.stayAwake) C.WAKE_MODE_NETWORK else C.WAKE_MODE_NONE
95112
}
96113

97114
override fun getDuration(): Int? {
@@ -164,6 +181,7 @@ class ExoPlayerWrapper(
164181
}
165182
}
166183

184+
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
167185
override fun updateContext(context: AudioContextAndroid) {
168186
val builder = AudioAttributes.Builder()
169187
builder.setContentType(context.contentType)
@@ -173,6 +191,7 @@ class ExoPlayerWrapper(
173191
builder.build(),
174192
false,
175193
)
194+
player.setWakeMode(wakeModeFor(context))
176195
}
177196

178197
@RequiresApi(Build.VERSION_CODES.M)

0 commit comments

Comments
 (0)