Move ExoPlayer out of the activity into a MediaSessionService so audio keeps playing when the app is backgrounded, the screen is off, or the task is swiped away. Media3 provides the media notification, lock-screen and headset controls, audio focus and noisy-headphone handling. The UI talks to the service through a MediaController-backed AudioPlayer and sends title, artist, album and artwork metadata for the notification. Add the Android 13 notification permission request, the media playback foreground service permissions, and wake mode for network streaming.
This commit is contained in:
1 parent
9cdbb3702e
commit
b8bbf8d07c
12 files changed
+232
-82
No files matched your search
+127
@@ -0,0 +1,127 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.media3.common.MediaItem
|
||||
import androidx.media3.common.MediaMetadata
|
||||
import androidx.media3.common.Player
|
||||
import androidx.media3.session.MediaController
|
||||
import androidx.media3.session.SessionToken
|
||||
import win.taptap.musicmanager.client.api.Song
|
||||
import win.taptap.musicmanager.client.playback.AudioPlayer
|
||||
|
||||
/**
|
||||
* Client side of [PlaybackService]. The controller is created asynchronously;
|
||||
* playback requests made before it connects are queued and executed once.
|
||||
*/
|
||||
class MediaControllerAudioPlayer(context: Context) : AudioPlayer {
|
||||
|
||||
private val appContext = context.applicationContext
|
||||
private var controller: MediaController? = null
|
||||
private var pending: ((MediaController) -> Unit)? = null
|
||||
private var connected = false
|
||||
|
||||
@Volatile private var finished = false
|
||||
|
||||
private val future = MediaController.Builder(
|
||||
appContext,
|
||||
SessionToken(appContext, ComponentName(appContext, PlaybackService::class.java)),
|
||||
).buildAsync()
|
||||
|
||||
private val listener = object : Player.Listener {
|
||||
override fun onPlaybackStateChanged(playbackState: Int) {
|
||||
finished = playbackState == Player.STATE_ENDED
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
future.addListener(
|
||||
{
|
||||
val sessionController = runCatching { future.get() }.getOrNull()
|
||||
if (sessionController != null) {
|
||||
controller = sessionController
|
||||
sessionController.addListener(listener)
|
||||
connected = true
|
||||
pending?.invoke(sessionController)
|
||||
pending = null
|
||||
}
|
||||
},
|
||||
ContextCompat.getMainExecutor(appContext),
|
||||
)
|
||||
}
|
||||
|
||||
override fun play(source: String, song: Song, local: Boolean, artworkUri: String?) {
|
||||
finished = false
|
||||
withController { controller ->
|
||||
val uri = if (local) Uri.fromFile(java.io.File(source)) else Uri.parse(source)
|
||||
val metadata = MediaMetadata.Builder()
|
||||
.setTitle(song.title)
|
||||
.setArtist(song.artist)
|
||||
.setAlbumTitle(song.album)
|
||||
.setArtworkUri(artworkUri?.let(Uri::parse))
|
||||
.build()
|
||||
val item = MediaItem.Builder()
|
||||
.setUri(uri)
|
||||
.setMimeType("audio/mpeg")
|
||||
.setMediaMetadata(metadata)
|
||||
.build()
|
||||
|
||||
controller.setMediaItem(item)
|
||||
controller.prepare()
|
||||
controller.play()
|
||||
}
|
||||
}
|
||||
|
||||
override fun pause() {
|
||||
withController { it.pause() }
|
||||
}
|
||||
|
||||
override fun resume() {
|
||||
withController { it.play() }
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
finished = false
|
||||
withController {
|
||||
it.stop()
|
||||
it.clearMediaItems()
|
||||
}
|
||||
}
|
||||
|
||||
override fun isPlaying(): Boolean = controller?.isPlaying == true
|
||||
|
||||
override fun isFinished(): Boolean = finished
|
||||
|
||||
override fun positionSeconds(): Double =
|
||||
(controller?.currentPosition ?: 0L).coerceAtLeast(0L) / 1000.0
|
||||
|
||||
override fun durationSeconds(): Double {
|
||||
val duration = controller?.duration ?: 0L
|
||||
return if (duration > 0) duration / 1000.0 else 0.0
|
||||
}
|
||||
|
||||
override val canSeek: Boolean = true
|
||||
|
||||
override fun seekTo(seconds: Double) {
|
||||
withController { it.seekTo((seconds * 1000).toLong().coerceAtLeast(0L)) }
|
||||
}
|
||||
|
||||
override fun release() {
|
||||
controller?.release()
|
||||
MediaController.releaseFuture(future)
|
||||
controller = null
|
||||
connected = false
|
||||
pending = null
|
||||
}
|
||||
|
||||
private fun withController(block: (MediaController) -> Unit) {
|
||||
val current = controller
|
||||
if (current != null && connected) {
|
||||
block(current)
|
||||
} else {
|
||||
pending = block
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user