Bug fixing
- clean ups - media player null pointer bug fixed - async playing queue saving working now
This commit is contained in:
parent
26d85b44ce
commit
594a328311
3 changed files with 54 additions and 29 deletions
|
|
@ -193,10 +193,6 @@ public class MusicPlayerRemote implements OnMusicRemoteEventListener {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void notifyPlayingQueueChanged() {
|
|
||||||
final long currentSongId = getCurrentSongId();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getRepeatMode() {
|
public int getRepeatMode() {
|
||||||
if (musicBound) {
|
if (musicBound) {
|
||||||
return musicService.getRepeatMode();
|
return musicService.getRepeatMode();
|
||||||
|
|
|
||||||
|
|
@ -66,6 +66,8 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
|
||||||
private boolean isPlayerPrepared;
|
private boolean isPlayerPrepared;
|
||||||
private boolean wasPlayingBeforeFocusLoss;
|
private boolean wasPlayingBeforeFocusLoss;
|
||||||
private boolean thingsRegistered;
|
private boolean thingsRegistered;
|
||||||
|
private boolean saveQueuesAgain;
|
||||||
|
private boolean isSavingQueues;
|
||||||
private NotificationHelper notificationHelper;
|
private NotificationHelper notificationHelper;
|
||||||
private AudioManager audioManager;
|
private AudioManager audioManager;
|
||||||
private RemoteControlClient remoteControlClient;
|
private RemoteControlClient remoteControlClient;
|
||||||
|
|
@ -105,7 +107,7 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean requestFocus() {
|
private boolean requestFocus() {
|
||||||
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
|
int result = getAudioManager().requestAudioFocus(this, AudioManager.STREAM_MUSIC,
|
||||||
AudioManager.AUDIOFOCUS_GAIN);
|
AudioManager.AUDIOFOCUS_GAIN);
|
||||||
|
|
||||||
return (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
|
return (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
|
||||||
|
|
@ -140,11 +142,13 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
|
||||||
unregisterReceiver(receiver);
|
unregisterReceiver(receiver);
|
||||||
getAudioManager().unregisterRemoteControlClient(remoteControlClient);
|
getAudioManager().unregisterRemoteControlClient(remoteControlClient);
|
||||||
getAudioManager().unregisterMediaButtonEventReceiver(new ComponentName(getApplicationContext(), MediaButtonIntentReceiver.class));
|
getAudioManager().unregisterMediaButtonEventReceiver(new ComponentName(getApplicationContext(), MediaButtonIntentReceiver.class));
|
||||||
|
getAudioManager().abandonAudioFocus(this);
|
||||||
thingsRegistered = false;
|
thingsRegistered = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateRemoteControlClient(Song song) {
|
private void updateRemoteControlClient() {
|
||||||
|
Song song = getPlayingQueue().get(getPosition());
|
||||||
Bitmap loadedImage = ImageLoader.getInstance().loadImageSync(MusicUtil.getAlbumArtUri(song.albumId).toString());
|
Bitmap loadedImage = ImageLoader.getInstance().loadImageSync(MusicUtil.getAlbumArtUri(song.albumId).toString());
|
||||||
remoteControlClient
|
remoteControlClient
|
||||||
.editMetadata(false)
|
.editMetadata(false)
|
||||||
|
|
@ -243,10 +247,10 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
|
||||||
}
|
}
|
||||||
|
|
||||||
private void killEverythingAndReleaseResources() {
|
private void killEverythingAndReleaseResources() {
|
||||||
savePosition();
|
|
||||||
saveQueues();
|
|
||||||
stopPlaying();
|
stopPlaying();
|
||||||
notificationHelper.killNotification();
|
notificationHelper.killNotification();
|
||||||
|
savePosition();
|
||||||
|
saveQueues();
|
||||||
stopSelf();
|
stopSelf();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -312,18 +316,20 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
|
||||||
case AudioManager.AUDIOFOCUS_GAIN:
|
case AudioManager.AUDIOFOCUS_GAIN:
|
||||||
// resume playback
|
// resume playback
|
||||||
registerEverything();
|
registerEverything();
|
||||||
|
if (!isPlayerPrepared()) {
|
||||||
|
setUpMediaPlayerIfNeeded();
|
||||||
|
}
|
||||||
player.setVolume(1.0f, 1.0f);
|
player.setVolume(1.0f, 1.0f);
|
||||||
if (wasPlayingBeforeFocusLoss) {
|
if (wasPlayingBeforeFocusLoss) {
|
||||||
resumePlaying();
|
resumePlaying();
|
||||||
updateRemoteControlClient(getPlayingQueue().get(getPosition()));
|
|
||||||
}
|
}
|
||||||
updateRemoteControlClient(getPlayingQueue().get(getPosition()));
|
updateRemoteControlClient();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AudioManager.AUDIOFOCUS_LOSS:
|
case AudioManager.AUDIOFOCUS_LOSS:
|
||||||
// Lost focus for an unbounded amount of time: stop playback and release media player
|
// Lost focus for an unbounded amount of time: stop playback and release media player
|
||||||
//TODO maybe also release player
|
//TODO maybe also release player (stopPlaying()) but the the current position in the song is 0 again
|
||||||
wasPlayingBeforeFocusLoss = isPlaying();
|
wasPlayingBeforeFocusLoss = false;
|
||||||
pausePlaying();
|
pausePlaying();
|
||||||
unregisterEverything();
|
unregisterEverything();
|
||||||
break;
|
break;
|
||||||
|
|
@ -339,6 +345,9 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
|
||||||
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
|
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
|
||||||
// Lost focus for a short time, but it's ok to keep playing
|
// Lost focus for a short time, but it's ok to keep playing
|
||||||
// at an attenuated level
|
// at an attenuated level
|
||||||
|
if (!isPlayerPrepared()) {
|
||||||
|
setUpMediaPlayerIfNeeded();
|
||||||
|
}
|
||||||
player.setVolume(0.2f, 0.2f);
|
player.setVolume(0.2f, 0.2f);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -370,7 +379,7 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
|
||||||
player.setDataSource(getApplicationContext(), trackUri);
|
player.setDataSource(getApplicationContext(), trackUri);
|
||||||
currentSongId = getPlayingQueue().get(getPosition()).id;
|
currentSongId = getPlayingQueue().get(getPosition()).id;
|
||||||
updateNotification();
|
updateNotification();
|
||||||
updateRemoteControlClient(getPlayingQueue().get(getPosition()));
|
updateRemoteControlClient();
|
||||||
player.prepareAsync();
|
player.prepareAsync();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e("MUSIC SERVICE", "Error setting data source", e);
|
Log.e("MUSIC SERVICE", "Error setting data source", e);
|
||||||
|
|
@ -531,23 +540,36 @@ public class MusicService extends Service implements MediaPlayer.OnPreparedListe
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveState() {
|
public void saveState() {
|
||||||
saveQueues();
|
saveQueuesAsync();
|
||||||
savePosition();
|
savePosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveQueues() {
|
public void saveQueuesAsync() {
|
||||||
new Thread(new Runnable() {
|
if (isSavingQueues) {
|
||||||
@Override
|
saveQueuesAgain = true;
|
||||||
public void run() {
|
} else {
|
||||||
try {
|
new Thread(new Runnable() {
|
||||||
InternalStorageUtil.writeObject(MusicService.this, AppKeys.IS_PLAYING_QUEUE, playingQueue);
|
@Override
|
||||||
InternalStorageUtil.writeObject(MusicService.this, AppKeys.IS_ORIGINAL_PLAYING_QUEUE, originalPlayingQueue);
|
public void run() {
|
||||||
Log.i(TAG, "saved current queue state");
|
isSavingQueues = true;
|
||||||
} catch (IOException e) {
|
do {
|
||||||
Log.e(TAG, "error while saving music service queue state", e);
|
saveQueuesAgain = false;
|
||||||
|
saveQueues();
|
||||||
|
} while (saveQueuesAgain);
|
||||||
|
isSavingQueues = false;
|
||||||
}
|
}
|
||||||
}
|
}).start();
|
||||||
}).start();
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveQueues() {
|
||||||
|
try {
|
||||||
|
InternalStorageUtil.writeObject(MusicService.this, AppKeys.IS_PLAYING_QUEUE, playingQueue);
|
||||||
|
InternalStorageUtil.writeObject(MusicService.this, AppKeys.IS_ORIGINAL_PLAYING_QUEUE, originalPlayingQueue);
|
||||||
|
Log.i(TAG, "saved current queue state");
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e(TAG, "error while saving music service queue state", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void savePosition() {
|
public void savePosition() {
|
||||||
|
|
|
||||||
|
|
@ -269,7 +269,6 @@ public class MusicControllerActivity extends AbsFabActivity implements OnMusicRe
|
||||||
if (fromUser) {
|
if (fromUser) {
|
||||||
getApp().getMusicPlayerRemote().seekTo(progress);
|
getApp().getMusicPlayerRemote().seekTo(progress);
|
||||||
}
|
}
|
||||||
currentSongProgress.setText(MusicUtil.getReadableDurationString(progress));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -365,8 +364,16 @@ public class MusicControllerActivity extends AbsFabActivity implements OnMusicRe
|
||||||
return;
|
return;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
progressSlider.setMax(total);
|
final int finalTotal = total;
|
||||||
progressSlider.setProgress(currentPosition);
|
final int finalCurrentPosition = currentPosition;
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
progressSlider.setMax(finalTotal);
|
||||||
|
progressSlider.setProgress(finalCurrentPosition);
|
||||||
|
currentSongProgress.setText(MusicUtil.getReadableDurationString(finalCurrentPosition));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue