Cleaned up MusicService and fixed ISE and NPEs there. Replaced thread with handler for updating the progress bar in MusicControllerActivity for simplicity and performance.
This commit is contained in:
parent
3bdecbebe4
commit
049bf90620
3 changed files with 110 additions and 57 deletions
|
|
@ -16,7 +16,7 @@ import com.kabouzeid.gramophone.util.PreferenceUtils;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
public final class MultiPlayer implements MediaPlayer.OnErrorListener,
|
public class MultiPlayer implements MediaPlayer.OnErrorListener,
|
||||||
MediaPlayer.OnCompletionListener {
|
MediaPlayer.OnCompletionListener {
|
||||||
public static final String TAG = MultiPlayer.class.getSimpleName();
|
public static final String TAG = MultiPlayer.class.getSimpleName();
|
||||||
|
|
||||||
|
|
@ -234,8 +234,7 @@ public final class MultiPlayer implements MediaPlayer.OnErrorListener,
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean onError(final MediaPlayer mp, final int what, final int extra) {
|
public boolean onError(final MediaPlayer mp, final int what, final int extra) {
|
||||||
Toast.makeText(mService.get().getApplicationContext(), mService.get().getResources().getString(R.string.unplayable_file), Toast.LENGTH_SHORT).show();
|
Toast.makeText(mService.get(), mService.get().getResources().getString(R.string.unplayable_file), Toast.LENGTH_SHORT).show();
|
||||||
mService.get().playNextSong(true);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -308,43 +308,53 @@ public class MusicService extends Service {
|
||||||
playSongAt(getNextPosition(force));
|
playSongAt(getNextPosition(force));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void openTrackAndPrepareNextAt(int position) {
|
private boolean openTrackAndPrepareNextAt(int position) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
setPosition(position);
|
setPosition(position);
|
||||||
openCurrent();
|
boolean prepared = openCurrent();
|
||||||
prepareNext();
|
prepareNext();
|
||||||
notifyChange(META_CHANGED);
|
notifyChange(META_CHANGED);
|
||||||
|
return prepared;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void openCurrent() {
|
private boolean openCurrent() {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
|
try {
|
||||||
player.setDataSource(getTrackUri(getCurrentSong()));
|
player.setDataSource(getTrackUri(getCurrentSong()));
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepareNext() {
|
private boolean prepareNext() {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
nextPosition = getNextPosition(false);
|
try {
|
||||||
|
int nextPosition = getNextPosition(false);
|
||||||
player.setNextDataSource(getTrackUri(getSongAt(nextPosition)));
|
player.setNextDataSource(getTrackUri(getSongAt(nextPosition)));
|
||||||
|
this.nextPosition = nextPosition;
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void closeAudioEffectSession() {
|
private void closeAudioEffectSession() {
|
||||||
if (player != null) {
|
|
||||||
final Intent audioEffectsIntent = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION);
|
final Intent audioEffectsIntent = new Intent(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION);
|
||||||
audioEffectsIntent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, player.getAudioSessionId());
|
audioEffectsIntent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, player.getAudioSessionId());
|
||||||
audioEffectsIntent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
|
audioEffectsIntent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
|
||||||
sendBroadcast(audioEffectsIntent);
|
sendBroadcast(audioEffectsIntent);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private boolean requestFocus() {
|
private boolean requestFocus() {
|
||||||
return (getAudioManager().requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
|
return (getAudioManager().requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateRemoteControlClient() {
|
private void updateRemoteControlClient() {
|
||||||
final Song song = playingQueue.get(getPosition());
|
final Song song = getCurrentSong();
|
||||||
remoteControlClient
|
remoteControlClient
|
||||||
.editMetadata(false)
|
.editMetadata(false)
|
||||||
.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, song.artistName)
|
.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, song.artistName)
|
||||||
|
|
@ -580,8 +590,11 @@ public class MusicService extends Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void playSongAtImpl(int position) {
|
private void playSongAtImpl(int position) {
|
||||||
openTrackAndPrepareNextAt(position);
|
if (openTrackAndPrepareNextAt(position)) {
|
||||||
play(false);
|
play(false);
|
||||||
|
} else {
|
||||||
|
Toast.makeText(this, getResources().getString(R.string.unplayable_file), Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pause(boolean forceNoFading) {
|
public void pause(boolean forceNoFading) {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,10 @@ import android.graphics.Color;
|
||||||
import android.graphics.PorterDuff;
|
import android.graphics.PorterDuff;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.HandlerThread;
|
||||||
|
import android.os.Looper;
|
||||||
|
import android.os.Message;
|
||||||
import android.support.v7.graphics.Palette;
|
import android.support.v7.graphics.Palette;
|
||||||
import android.support.v7.widget.CardView;
|
import android.support.v7.widget.CardView;
|
||||||
import android.support.v7.widget.Toolbar;
|
import android.support.v7.widget.Toolbar;
|
||||||
|
|
@ -53,11 +57,13 @@ import com.nostra13.universalimageloader.core.assist.FailReason;
|
||||||
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
|
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
public class MusicControllerActivity extends AbsFabActivity {
|
public class MusicControllerActivity extends AbsFabActivity {
|
||||||
|
|
||||||
public static final String TAG = MusicControllerActivity.class.getSimpleName();
|
public static final String TAG = MusicControllerActivity.class.getSimpleName();
|
||||||
private static final int COLOR_TRANSITION_TIME = 400;
|
private static final int COLOR_TRANSITION_TIME = 400;
|
||||||
|
private static final int UPDATE_PROGRESS_VIEWS = 1;
|
||||||
|
|
||||||
private Song song;
|
private Song song;
|
||||||
private SquareIfPlaceImageView albumArt;
|
private SquareIfPlaceImageView albumArt;
|
||||||
|
|
@ -77,7 +83,8 @@ public class MusicControllerActivity extends AbsFabActivity {
|
||||||
private Toolbar toolbar;
|
private Toolbar toolbar;
|
||||||
private int lastFooterColor = -1;
|
private int lastFooterColor = -1;
|
||||||
private int lastTextColor = -2;
|
private int lastTextColor = -2;
|
||||||
private Thread progressViewsUpdateThread;
|
private Handler progressViewsUpdateHandler;
|
||||||
|
private HandlerThread handlerThread;
|
||||||
|
|
||||||
private boolean opaqueStatusBar;
|
private boolean opaqueStatusBar;
|
||||||
private boolean opaqueToolBar;
|
private boolean opaqueToolBar;
|
||||||
|
|
@ -92,7 +99,7 @@ public class MusicControllerActivity extends AbsFabActivity {
|
||||||
|
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
initAppeareanceVars();
|
initAppearanceVarsFromSharedPrefs();
|
||||||
|
|
||||||
setContentView(alternativeProgressSlider ? R.layout.activity_music_controller_alternative_progress_slider : R.layout.activity_music_controller);
|
setContentView(alternativeProgressSlider ? R.layout.activity_music_controller_alternative_progress_slider : R.layout.activity_music_controller);
|
||||||
|
|
||||||
|
|
@ -101,10 +108,14 @@ public class MusicControllerActivity extends AbsFabActivity {
|
||||||
adjustTitleBoxSize();
|
adjustTitleBoxSize();
|
||||||
setUpPlaybackControllerCard();
|
setUpPlaybackControllerCard();
|
||||||
setUpMusicControllers();
|
setUpMusicControllers();
|
||||||
|
setUpAlbumArtViews();
|
||||||
|
setUpToolbar();
|
||||||
|
animateFabCircularRevealOnEnterTransitionEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setUpAlbumArtViews() {
|
||||||
albumArtBackground.setAlpha(0.7f);
|
albumArtBackground.setAlpha(0.7f);
|
||||||
albumArt.forceSquare(forceSquareAlbumArt);
|
albumArt.forceSquare(forceSquareAlbumArt);
|
||||||
|
|
||||||
if (opaqueStatusBar) {
|
if (opaqueStatusBar) {
|
||||||
if (opaqueToolBar) {
|
if (opaqueToolBar) {
|
||||||
alignAlbumArtToToolbar();
|
alignAlbumArtToToolbar();
|
||||||
|
|
@ -114,11 +125,16 @@ public class MusicControllerActivity extends AbsFabActivity {
|
||||||
} else {
|
} else {
|
||||||
alignAlbumArtToTop();
|
alignAlbumArtToTop();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setUpToolbar() {
|
||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
|
//noinspection ConstantConditions
|
||||||
getSupportActionBar().setTitle(null);
|
getSupportActionBar().setTitle(null);
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void animateFabCircularRevealOnEnterTransitionEnd() {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||||
getWindow().getEnterTransition().addListener(new SmallTransitionListener() {
|
getWindow().getEnterTransition().addListener(new SmallTransitionListener() {
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -143,7 +159,31 @@ public class MusicControllerActivity extends AbsFabActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initAppeareanceVars() {
|
private void startHandler() {
|
||||||
|
handlerThread = new HandlerThread("MusicProgressViewUpdateHandler",
|
||||||
|
android.os.Process.THREAD_PRIORITY_BACKGROUND);
|
||||||
|
handlerThread.start();
|
||||||
|
progressViewsUpdateHandler = new MusicProgressViewsUpdateHandler(this, handlerThread.getLooper());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void stopHandler() {
|
||||||
|
progressViewsUpdateHandler.removeCallbacksAndMessages(null);
|
||||||
|
if (Build.VERSION.SDK_INT >= 18) {
|
||||||
|
handlerThread.quitSafely();
|
||||||
|
} else {
|
||||||
|
handlerThread.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startUpdatingProgressViews() {
|
||||||
|
progressViewsUpdateHandler.sendEmptyMessage(UPDATE_PROGRESS_VIEWS);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void stopUpdatingProgressViews() {
|
||||||
|
progressViewsUpdateHandler.removeMessages(UPDATE_PROGRESS_VIEWS);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initAppearanceVarsFromSharedPrefs() {
|
||||||
opaqueStatusBar = PreferenceUtils.getInstance(this).opaqueStatusbarNowPlaying();
|
opaqueStatusBar = PreferenceUtils.getInstance(this).opaqueStatusbarNowPlaying();
|
||||||
opaqueToolBar = opaqueStatusBar && PreferenceUtils.getInstance(this).opaqueToolbarNowPlaying();
|
opaqueToolBar = opaqueStatusBar && PreferenceUtils.getInstance(this).opaqueToolbarNowPlaying();
|
||||||
forceSquareAlbumArt = PreferenceUtils.getInstance(this).forceAlbumArtSquared();
|
forceSquareAlbumArt = PreferenceUtils.getInstance(this).forceAlbumArtSquared();
|
||||||
|
|
@ -320,8 +360,16 @@ public class MusicControllerActivity extends AbsFabActivity {
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
updateControllerState();
|
updateControllerState();
|
||||||
startProgressViewsUpdateThread();
|
|
||||||
updateCurrentSong();
|
updateCurrentSong();
|
||||||
|
startHandler();
|
||||||
|
startUpdatingProgressViews();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
stopUpdatingProgressViews();
|
||||||
|
stopHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateCurrentSong() {
|
private void updateCurrentSong() {
|
||||||
|
|
@ -440,28 +488,11 @@ public class MusicControllerActivity extends AbsFabActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startProgressViewsUpdateThread() {
|
private void updateProgressViews() {
|
||||||
if (progressViewsUpdateThread != null) progressViewsUpdateThread.interrupt();
|
final int totalMillis = MusicPlayerRemote.getSongDurationMillis();
|
||||||
progressViewsUpdateThread = new Thread(new Runnable() {
|
final int progressMillis = MusicPlayerRemote.getSongProgressMillis();
|
||||||
int totalMillis = 0;
|
|
||||||
int progressMillis = 0;
|
|
||||||
|
|
||||||
@Override
|
runOnUiThread(new Runnable() {
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
while (!Thread.currentThread().isInterrupted()) {
|
|
||||||
totalMillis = MusicPlayerRemote.getSongDurationMillis();
|
|
||||||
progressMillis = MusicPlayerRemote.getSongProgressMillis();
|
|
||||||
|
|
||||||
runOnUiThread(updateProgressViews);
|
|
||||||
|
|
||||||
Thread.sleep(100);
|
|
||||||
}
|
|
||||||
} catch (InterruptedException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Runnable updateProgressViews = new Runnable() {
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
progressSlider.setMax(totalMillis);
|
progressSlider.setMax(totalMillis);
|
||||||
|
|
@ -469,9 +500,7 @@ public class MusicControllerActivity extends AbsFabActivity {
|
||||||
currentSongProgress.setText(MusicUtil.getReadableDurationString(progressMillis));
|
currentSongProgress.setText(MusicUtil.getReadableDurationString(progressMillis));
|
||||||
totalSongDuration.setText(MusicUtil.getReadableDurationString(totalMillis));
|
totalSongDuration.setText(MusicUtil.getReadableDurationString(totalMillis));
|
||||||
}
|
}
|
||||||
};
|
|
||||||
});
|
});
|
||||||
progressViewsUpdateThread.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void updateControllerState() {
|
protected void updateControllerState() {
|
||||||
|
|
@ -498,12 +527,6 @@ public class MusicControllerActivity extends AbsFabActivity {
|
||||||
updateShuffleState();
|
updateShuffleState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPause() {
|
|
||||||
super.onPause();
|
|
||||||
if (progressViewsUpdateThread != null) progressViewsUpdateThread.interrupt();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
getMenuInflater().inflate(R.menu.menu_music_playing, menu);
|
getMenuInflater().inflate(R.menu.menu_music_playing, menu);
|
||||||
|
|
@ -573,4 +596,22 @@ public class MusicControllerActivity extends AbsFabActivity {
|
||||||
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) findViewById(R.id.album_art_frame).getLayoutParams();
|
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) findViewById(R.id.album_art_frame).getLayoutParams();
|
||||||
params.addRule(RelativeLayout.BELOW, R.id.status_bar);
|
params.addRule(RelativeLayout.BELOW, R.id.status_bar);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class MusicProgressViewsUpdateHandler extends Handler {
|
||||||
|
private WeakReference<MusicControllerActivity> activityReference;
|
||||||
|
|
||||||
|
public MusicProgressViewsUpdateHandler(final MusicControllerActivity activity, final Looper looper) {
|
||||||
|
super(looper);
|
||||||
|
activityReference = new WeakReference<>(activity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleMessage(Message msg) {
|
||||||
|
super.handleMessage(msg);
|
||||||
|
if (msg.what == UPDATE_PROGRESS_VIEWS) {
|
||||||
|
activityReference.get().updateProgressViews();
|
||||||
|
sendEmptyMessageDelayed(UPDATE_PROGRESS_VIEWS, 100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue