From dc144e3c1f422dba925b0a62cbb87b7c024ef1b7 Mon Sep 17 00:00:00 2001 From: linus_lee Date: Mon, 22 Dec 2014 17:18:39 -0800 Subject: [PATCH] Eleven: Show error message for corrupt tracks (additional codepaths) Add showing error message for code paths where the media player fails gracefully Unfortunately adding the logic to remove the bad track is not trivial because the removeTracks logic automatically does other things to try to go to the next track. For now I will leave the logic so that it goes on to the next track and make a note on the music service rewrite notes to cover this scenario as well. Change-Id: Ib7ae5ce7819b15cdb3ada4995bc0734ab2d9b0a2 --- res/values/strings.xml | 2 +- .../cyanogenmod/eleven/MusicPlaybackService.java | 47 ++++++++++++++-------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 38197c9..5bf25b1 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -166,7 +166,7 @@ To copy music from your computer to your device, use a USB cable. No songs in play queue To add songs to your Play Queue, tap the options menu on a song, album, or artist and select \"Add to queue\". - Unable to play track %1$s, removing it from the queue + Unable to play track %1$s Unknown year diff --git a/src/com/cyanogenmod/eleven/MusicPlaybackService.java b/src/com/cyanogenmod/eleven/MusicPlaybackService.java index 5b71d86..73c3694 100644 --- a/src/com/cyanogenmod/eleven/MusicPlaybackService.java +++ b/src/com/cyanogenmod/eleven/MusicPlaybackService.java @@ -1156,6 +1156,8 @@ public class MusicPlaybackService extends Service { } stop(false); + boolean shutdown = false; + updateCursor(mPlaylist.get(mPlayPos).mId); while (true) { if (mCursor != null @@ -1163,6 +1165,7 @@ public class MusicPlaybackService extends Service { + mCursor.getLong(IDCOLIDX))) { break; } + // if we get here then opening the file failed. We can close the // cursor now, because // we're either going to create a new one next, or stop trying @@ -1170,12 +1173,8 @@ public class MusicPlaybackService extends Service { if (mOpenFailedCounter++ < 10 && mPlaylist.size() > 1) { final int pos = getNextPosition(false); if (pos < 0) { - scheduleDelayedShutdown(); - if (mIsSupposedToBePlaying) { - mIsSupposedToBePlaying = false; - notifyChange(PLAYSTATE_CHANGED); - } - return; + shutdown = true; + break; } mPlayPos = pos; stop(false); @@ -1184,20 +1183,29 @@ public class MusicPlaybackService extends Service { } else { mOpenFailedCounter = 0; Log.w(TAG, "Failed to open file for playback"); - scheduleDelayedShutdown(); - if (mIsSupposedToBePlaying) { - mIsSupposedToBePlaying = false; - notifyChange(PLAYSTATE_CHANGED); - } - return; + shutdown = true; + break; } } - if (openNext) { + + if (shutdown) { + scheduleDelayedShutdown(); + if (mIsSupposedToBePlaying) { + mIsSupposedToBePlaying = false; + notifyChange(PLAYSTATE_CHANGED); + } + } else if (openNext) { setNextTrack(); } } } + private void sendErrorMessage(final String trackName) { + final Intent i = new Intent(TRACK_ERROR); + i.putExtra(TrackErrorExtra.TRACK_NAME, trackName); + sendBroadcast(i); + } + /** * @param force True to force the player onto the track next, false * otherwise. @@ -1727,6 +1735,13 @@ public class MusicPlaybackService extends Service { mOpenFailedCounter = 0; return true; } + + String trackName = getTrackName(); + if (TextUtils.isEmpty(trackName)) { + trackName = path; + } + sendErrorMessage(trackName); + stop(true); return false; } @@ -1989,7 +2004,7 @@ public class MusicPlaybackService extends Service { */ public String getGenreName() { synchronized (this) { - if (mCursor == null) { + if (mCursor == null || mPlayPos < 0 || mPlayPos >= mPlaylist.size()) { return null; } String[] genreProjection = { MediaStore.Audio.Genres.NAME }; @@ -2813,10 +2828,8 @@ public class MusicPlaybackService extends Service { break; case SERVER_DIED: if (service.isPlaying()) { - final Intent i = new Intent(TRACK_ERROR); final TrackErrorInfo info = (TrackErrorInfo)msg.obj; - i.putExtra(TrackErrorExtra.TRACK_NAME, info.mTrackName); - service.sendBroadcast(i); + service.sendErrorMessage(info.mTrackName); // since the service isPlaying(), we only need to remove the offending // audio track, and the code will automatically play the next track -- 2.11.0