Prevent a media3 crash

This commit is contained in:
birdbird 2023-06-27 08:22:57 +00:00
parent 843cf8eb28
commit d420f70225
4 changed files with 10 additions and 9 deletions

View File

@ -135,7 +135,6 @@ abstract class EntryListFragment<T : GenericEntry> : MultiListFragment<T>() {
item.id, item.id,
append = false, append = false,
autoPlay = true, autoPlay = true,
shuffle = false,
playNext = false, playNext = false,
isArtist = isArtist isArtist = isArtist
) )
@ -145,7 +144,6 @@ abstract class EntryListFragment<T : GenericEntry> : MultiListFragment<T>() {
item.id, item.id,
append = false, append = false,
autoPlay = true, autoPlay = true,
shuffle = true,
playNext = true, playNext = true,
isArtist = isArtist isArtist = isArtist
) )
@ -155,7 +153,6 @@ abstract class EntryListFragment<T : GenericEntry> : MultiListFragment<T>() {
item.id, item.id,
append = true, append = true,
autoPlay = false, autoPlay = false,
shuffle = false,
playNext = false, playNext = false,
isArtist = isArtist isArtist = isArtist
) )

View File

@ -316,7 +316,6 @@ open class TrackCollectionFragment(
append = append, append = append,
playNext = false, playNext = false,
autoPlay = !append, autoPlay = !append,
shuffle = false,
playlistName = null, playlistName = null,
fragment = this fragment = this
) )
@ -616,7 +615,6 @@ open class TrackCollectionFragment(
append = true, append = true,
playNext = true, playNext = true,
autoPlay = false, autoPlay = false,
shuffle = false,
playlistName = navArgs.playlistName, playlistName = navArgs.playlistName,
fragment = this@TrackCollectionFragment fragment = this@TrackCollectionFragment
) )

View File

@ -429,7 +429,10 @@ class MediaPlayerManager(
when (insertionMode) { when (insertionMode) {
InsertionMode.CLEAR -> clear() InsertionMode.CLEAR -> clear()
InsertionMode.APPEND -> insertAt = mediaItemCount InsertionMode.APPEND -> insertAt = mediaItemCount
InsertionMode.AFTER_CURRENT -> insertAt = currentMediaItemIndex + 1 InsertionMode.AFTER_CURRENT -> {
// Must never be larger than the count of items (especially when empty)
insertAt = (currentMediaItemIndex + 1).coerceAtMost(mediaItemCount)
}
} }
val mediaItems: List<MediaItem> = songs.map { val mediaItems: List<MediaItem> = songs.map {
@ -437,10 +440,13 @@ class MediaPlayerManager(
result result
} }
if (shuffle) isShufflePlayEnabled = true
Timber.w("Adding ${mediaItems.size} media items") Timber.w("Adding ${mediaItems.size} media items")
controller?.addMediaItems(insertAt, mediaItems) controller?.addMediaItems(insertAt, mediaItems)
// There is a bug in media3 ( https://github.com/androidx/media/issues/480 ),
// so we must first add the tracks, and then enable shuffle
if (shuffle) isShufflePlayEnabled = true
prepare() prepare()
// Playback doesn't start correctly when the player is in STATE_ENDED. // Playback doesn't start correctly when the player is in STATE_ENDED.

View File

@ -98,7 +98,7 @@ class DownloadHandler(
isDirectory: Boolean = true, isDirectory: Boolean = true,
append: Boolean, append: Boolean,
autoPlay: Boolean, autoPlay: Boolean,
shuffle: Boolean, shuffle: Boolean = false,
playNext: Boolean, playNext: Boolean,
isArtist: Boolean = false isArtist: Boolean = false
) { ) {
@ -141,7 +141,7 @@ class DownloadHandler(
append: Boolean, append: Boolean,
playNext: Boolean, playNext: Boolean,
autoPlay: Boolean, autoPlay: Boolean,
shuffle: Boolean, shuffle: Boolean = false,
playlistName: String? = null, playlistName: String? = null,
fragment: Fragment fragment: Fragment
) { ) {