Skip to content

Commit

Permalink
fix: when play error or music remove by other app.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZTFtrue committed Jan 23, 2024
1 parent c1b1a65 commit 8ed931e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 33 deletions.
11 changes: 7 additions & 4 deletions app/src/main/java/com/ztftrue/music/MusicViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,13 @@ class MusicViewModel : ViewModel() {
}

fun getCover(path: String): Bitmap? {
retriever.setDataSource(path)
val coverT = retriever.embeddedPicture
if (coverT != null) {
return BitmapFactory.decodeByteArray(coverT, 0, coverT.size)
try {
retriever.setDataSource(path)
val coverT = retriever.embeddedPicture
if (coverT != null) {
return BitmapFactory.decodeByteArray(coverT, 0, coverT.size)
}
} catch (_: Exception) {
}
return null
}
Expand Down
73 changes: 44 additions & 29 deletions app/src/main/java/com/ztftrue/music/play/PlayService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import android.support.v4.media.MediaMetadataCompat
import android.support.v4.media.session.MediaControllerCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import android.widget.Toast
import androidx.compose.runtime.mutableLongStateOf
import androidx.media.MediaBrowserServiceCompat
import androidx.media3.common.AudioAttributes
import androidx.media3.common.BuildConfig
import androidx.media3.common.C
import androidx.media3.common.Format
import androidx.media3.common.MediaItem
Expand Down Expand Up @@ -679,13 +681,12 @@ class PlayService : MediaBrowserServiceCompat() {
db.QueueDao().insertAll(musicQueue)
}
}
if(!exoPlayer.isPlaying){
if (!exoPlayer.isPlaying) {
exoPlayer.playWhenReady = false
}
exoPlayer.addMediaItem(index, MediaItem.fromUri(musicItem.path))
} else if (musicItems != null) {
val list = ArrayList<MediaItem>()

musicItems.forEach {
list.add(MediaItem.fromUri(it.path))
}
Expand All @@ -709,7 +710,7 @@ class PlayService : MediaBrowserServiceCompat() {
db.QueueDao().insertAll(musicQueue)
}
}
if(!exoPlayer.isPlaying){
if (!exoPlayer.isPlaying) {
exoPlayer.playWhenReady = false
}
exoPlayer.addMediaItems(index, list)
Expand Down Expand Up @@ -749,7 +750,7 @@ class PlayService : MediaBrowserServiceCompat() {
}
}
if (musicQueue.isNotEmpty()) {
val currentIndex=exoPlayer.currentMediaItemIndex
val currentIndex = exoPlayer.currentMediaItemIndex
val bundle = Bundle()
bundle.putInt("type", EVENT_changePlayQueue)
bundle.putInt("index", currentIndex)
Expand All @@ -758,7 +759,7 @@ class PlayService : MediaBrowserServiceCompat() {
// TODO
mediaSession?.setExtras(bundle)
CoroutineScope(Dispatchers.IO).launch {
saveSelectIdAndPosition(musicQueue[currentIndex].id, currentIndex)
saveSelectMusicId(musicQueue[currentIndex].id)
}
result.sendResult(bundle)
return
Expand Down Expand Up @@ -1022,14 +1023,18 @@ class PlayService : MediaBrowserServiceCompat() {
playListCurrent =
AnyListBase(plaC.listID, enumValueOf(plaC.type))
}
foldersListTracksHashMap.entries.forEach {
for (it in foldersListTracksHashMap.entries) {
if (it.value.isNotEmpty()) {
currentPlayTrack = it.value[id]
if (currentPlayTrack != null) {
return@forEach
break
}
}
}
if (currentPlayTrack == null) {
playListCurrent = null
saveSelectMusicId(-1)
}
}
}
},
Expand Down Expand Up @@ -1101,12 +1106,16 @@ class PlayService : MediaBrowserServiceCompat() {
exoPlayer.playbackParameters = p
if (musicQueue.isNotEmpty()) {
val t1 = ArrayList<MediaItem>()
musicQueue.forEach {
var currentIndex = 0
musicQueue.forEachIndexed { index, it ->
t1.add(MediaItem.fromUri(it.path))
if (it.id == currentPlayTrack?.id) {
currentIndex = index
}
}
exoPlayer.setMediaItems(t1)
if (currentPlayTrack != null) {
exoPlayer.seekToDefaultPosition(getSelectPosition())
exoPlayer.seekToDefaultPosition(currentIndex)
}
}
if (currentPlayTrack != null && musicQueue.isNotEmpty()) {
Expand Down Expand Up @@ -1211,7 +1220,7 @@ class PlayService : MediaBrowserServiceCompat() {
mediaController?.transportControls?.play()
}
} else {
saveSelectIdAndPosition(musicQueue[index].id, index)
saveSelectMusicId(musicQueue[index].id)
exoPlayer.seekToDefaultPosition(index)
exoPlayer.playWhenReady = true
exoPlayer.prepare()
Expand Down Expand Up @@ -1298,7 +1307,7 @@ class PlayService : MediaBrowserServiceCompat() {
CoroutineScope(Dispatchers.IO).launch {
db.QueueDao().deleteAllQueue()
db.QueueDao().insertAll(musicQueue)
saveSelectIdAndPosition(musicQueue[index].id, index)
saveSelectMusicId(musicQueue[index].id)
}
exoPlayer.seekToDefaultPosition(index)
exoPlayer.playWhenReady = true
Expand Down Expand Up @@ -1398,6 +1407,7 @@ class PlayService : MediaBrowserServiceCompat() {
}
}

var errorCount = 0;
private fun playerAddListener() {
exoPlayer.addListener(@UnstableApi object : Player.Listener {
override fun onIsPlayingChanged(isPlaying: Boolean) {
Expand All @@ -1423,13 +1433,25 @@ class PlayService : MediaBrowserServiceCompat() {

override fun onPlayerError(error: PlaybackException) {
super.onPlayerError(error)
error.printStackTrace()
notify?.updateNotification(
this@PlayService,
currentPlayTrack?.name ?: "",
error.errorCodeName,
exoPlayer
)
if (BuildConfig.DEBUG) {
error.printStackTrace()
}
if (errorCount > 3) {
Toast.makeText(
this@PlayService,
"Many times play error, Play paused",
Toast.LENGTH_SHORT
).show()
} else {
Toast.makeText(
this@PlayService,
"Play error, auto play next",
Toast.LENGTH_SHORT
).show()
exoPlayer.seekToNextMediaItem()
exoPlayer.prepare()
errorCount++
}
}

override fun onPlaybackStateChanged(playbackState: Int) {
Expand Down Expand Up @@ -1514,19 +1536,18 @@ class PlayService : MediaBrowserServiceCompat() {
bundle.putInt("type", EVENT_MEDIA_ITEM_Change)
bundle.putParcelable("current", currentPlayTrack)
bundle.putInt("index", exoPlayer.currentMediaItemIndex)
saveSelectIdAndPosition(
musicQueue[exoPlayer.currentMediaItemIndex].id,
exoPlayer.currentMediaItemIndex
saveSelectMusicId(
musicQueue[exoPlayer.currentMediaItemIndex].id
)
mediaSession?.setExtras(bundle)
mediaSession?.setMetadata(metadataBuilder.build())
}

private fun saveSelectIdAndPosition(id: Long, index: Int) {
private fun saveSelectMusicId(id: Long) {
this@PlayService.getSharedPreferences(
"SelectedPlayTrack",
Context.MODE_PRIVATE
).edit().putLong("SelectedPlayTrack", id).putInt("PlayTrackIndex", index).apply()
).edit().putLong("SelectedPlayTrack", id).apply()
}

private fun getCurrentPlayId(): Long {
Expand All @@ -1536,10 +1557,4 @@ class PlayService : MediaBrowserServiceCompat() {
).getLong("SelectedPlayTrack", -1)
}

private fun getSelectPosition(): Int {
return this@PlayService.getSharedPreferences(
"SelectedPlayTrack",
Context.MODE_PRIVATE
).getInt("PlayTrackIndex", 0)
}
}

0 comments on commit 8ed931e

Please sign in to comment.