mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Fix music sync with info/cover/notification
This commit is contained in:
parent
dfc10c76ca
commit
c0088ff224
10 changed files with 132 additions and 51 deletions
|
|
@ -82,7 +82,7 @@ public class PlayerSongQueueAdapter extends RecyclerView.Adapter<PlayerSongQueue
|
|||
SongRepository songRepository = new SongRepository(App.getInstance());
|
||||
songRepository.increasePlayCount(songs.get(getBindingAdapterPosition()));
|
||||
|
||||
playerBottomSheetFragment.scrollPager(songs.get(getBindingAdapterPosition()), getAdapterPosition(), true);
|
||||
playerBottomSheetFragment.scrollPager(songs.get(getBindingAdapterPosition()), getBindingAdapterPosition(), false);
|
||||
MusicPlayerRemote.openQueue(songs, getBindingAdapterPosition(), true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -118,7 +118,7 @@ public class MusicPlayerRemote {
|
|||
|
||||
public static void playNextSong() {
|
||||
if (musicService != null) {
|
||||
musicService.playNextSong(true);
|
||||
musicService.playNextSong();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -170,6 +170,14 @@ public class MusicPlayerRemote {
|
|||
return false;
|
||||
}
|
||||
|
||||
public static Song getCurrentSong() {
|
||||
if (musicService != null) {
|
||||
return musicService.getCurrentSong();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int getPosition() {
|
||||
if (musicService != null) {
|
||||
return musicService.getPosition();
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import android.preference.PreferenceManager;
|
|||
import android.support.v4.media.MediaMetadataCompat;
|
||||
import android.support.v4.media.session.MediaSessionCompat;
|
||||
import android.support.v4.media.session.PlaybackStateCompat;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
|
@ -56,6 +57,7 @@ import static com.google.android.exoplayer2.Player.PLAY_WHEN_READY_CHANGE_REASON
|
|||
|
||||
public class MusicService extends Service implements Playback.PlaybackCallbacks {
|
||||
public static final String PACKAGE_NAME = "com.antoniocappiello.play";
|
||||
private static final String TAG = "MusicService";
|
||||
|
||||
public static final String ACTION_TOGGLE = PACKAGE_NAME + ".toggle";
|
||||
public static final String ACTION_PLAY = PACKAGE_NAME + ".play";
|
||||
|
|
@ -206,7 +208,7 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
|
|||
|
||||
@Override
|
||||
public void onSkipToNext() {
|
||||
playNextSong(true);
|
||||
playNextSong();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -269,7 +271,7 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
|
|||
back(true);
|
||||
break;
|
||||
case ACTION_SKIP:
|
||||
playNextSong(true);
|
||||
playNextSong();
|
||||
break;
|
||||
case ACTION_STOP:
|
||||
case ACTION_QUIT:
|
||||
|
|
@ -343,7 +345,6 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
|
|||
private synchronized void restoreQueuesAndPositionIfNecessary() {
|
||||
if (!queuesRestored && playingQueue.isEmpty()) {
|
||||
QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
||||
|
||||
List<Song> restoredQueue = queueRepository.getSongs();
|
||||
|
||||
int restoredPosition = PreferenceManager.getDefaultSharedPreferences(this).getInt(PreferenceUtil.POSITION, -1);
|
||||
|
|
@ -400,8 +401,8 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
|
|||
return position;
|
||||
}
|
||||
|
||||
public void playNextSong(boolean force) {
|
||||
playSongAt(getNextPosition(force));
|
||||
public void playNextSong() {
|
||||
playSongAt(getNextPosition());
|
||||
}
|
||||
|
||||
private void openTrackAndPrepareNextAt(int position) {
|
||||
|
|
@ -432,7 +433,7 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
|
|||
|
||||
private void prepareNextImpl() {
|
||||
synchronized (this) {
|
||||
nextPosition = getNextPosition(false);
|
||||
nextPosition = getNextPosition();
|
||||
playback.queueDataSource(getSongAt(nextPosition));
|
||||
}
|
||||
}
|
||||
|
|
@ -500,8 +501,14 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
|
|||
}
|
||||
}
|
||||
|
||||
public int getNextPosition(boolean force) {
|
||||
return getPosition() + 1;
|
||||
public int getNextPosition() {
|
||||
int position = getPosition() + 1;
|
||||
|
||||
if (isLastTrack()) {
|
||||
position -= 1;
|
||||
}
|
||||
|
||||
return position;
|
||||
}
|
||||
|
||||
private boolean isLastTrack() {
|
||||
|
|
@ -804,9 +811,16 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
|
|||
break;
|
||||
|
||||
case TRACK_CHANGED:
|
||||
service.position = service.nextPosition;
|
||||
service.prepareNextImpl();
|
||||
service.notifyChange(META_CHANGED);
|
||||
if (service.isLastTrack()) {
|
||||
service.pause();
|
||||
service.seek(0);
|
||||
service.notifyChange(STATE_CHANGED);
|
||||
} else {
|
||||
service.position = service.nextPosition;
|
||||
service.prepareNextImpl();
|
||||
service.notifyChange(META_CHANGED);
|
||||
service.notifyChange(QUEUE_CHANGED);
|
||||
}
|
||||
break;
|
||||
|
||||
case TRACK_ENDED:
|
||||
|
|
@ -814,13 +828,14 @@ public class MusicService extends Service implements Playback.PlaybackCallbacks
|
|||
if (service.pendingQuit && service.isLastTrack()) {
|
||||
service.notifyChange(STATE_CHANGED);
|
||||
service.seek(0);
|
||||
|
||||
if (service.pendingQuit) {
|
||||
service.pendingQuit = false;
|
||||
service.quit();
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
service.playNextSong(false);
|
||||
service.playNextSong();
|
||||
}
|
||||
|
||||
sendEmptyMessage(RELEASE_WAKELOCK);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
package com.cappielloantonio.play.ui.fragment;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
|
@ -18,12 +21,14 @@ import com.cappielloantonio.play.R;
|
|||
import com.cappielloantonio.play.adapter.PlayerNowPlayingSongAdapter;
|
||||
import com.cappielloantonio.play.adapter.PlayerSongQueueAdapter;
|
||||
import com.cappielloantonio.play.databinding.FragmentPlayerBottomSheetBinding;
|
||||
import com.cappielloantonio.play.glide.CustomGlideRequest;
|
||||
import com.cappielloantonio.play.helper.MusicPlayerRemote;
|
||||
import com.cappielloantonio.play.helper.MusicProgressViewUpdateHelper;
|
||||
import com.cappielloantonio.play.interfaces.MusicServiceEventListener;
|
||||
import com.cappielloantonio.play.model.Song;
|
||||
import com.cappielloantonio.play.ui.activities.MainActivity;
|
||||
import com.cappielloantonio.play.util.MusicUtil;
|
||||
import com.cappielloantonio.play.util.PreferenceUtil;
|
||||
import com.cappielloantonio.play.viewmodel.PlayerBottomSheetViewModel;
|
||||
|
||||
public class PlayerBottomSheetFragment extends Fragment implements MusicServiceEventListener, MusicProgressViewUpdateHelper.Callback {
|
||||
|
|
@ -105,9 +110,19 @@ public class PlayerBottomSheetFragment extends Fragment implements MusicServiceE
|
|||
super.onPageSelected(position);
|
||||
|
||||
Song song = playerNowPlayingSongAdapter.getItem(position);
|
||||
if (song != null && song != playerBottomSheetViewModel.getSong()) setSongInfo(song);
|
||||
if (song != null && song != playerBottomSheetViewModel.getSong()) {
|
||||
|
||||
if (MusicPlayerRemote.isPlaying()) {
|
||||
MusicPlayerRemote.playSongAt(position);
|
||||
} else {
|
||||
MusicPlayerRemote.setPosition(position);
|
||||
MusicPlayerRemote.pauseSong();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
setViewPageDelayed(PreferenceManager.getDefaultSharedPreferences(requireContext()).getInt(PreferenceUtil.POSITION, -1));
|
||||
}
|
||||
|
||||
private void initQueueRecyclerView() {
|
||||
|
|
@ -155,6 +170,19 @@ public class PlayerBottomSheetFragment extends Fragment implements MusicServiceE
|
|||
});
|
||||
}
|
||||
|
||||
private void setViewPageDelayed(int position) {
|
||||
/*bind.playerBodyLayout.playerSongCoverViewPager.post(() -> {
|
||||
int restoredPosition = PreferenceManager.getDefaultSharedPreferences(requireContext()).getInt(PreferenceUtil.POSITION, -1);
|
||||
bind.playerBodyLayout.playerSongCoverViewPager.setCurrentItem(restoredPosition, true);
|
||||
});*/
|
||||
|
||||
final Handler handler = new Handler();
|
||||
final Runnable r = () -> {
|
||||
bind.playerBodyLayout.playerSongCoverViewPager.setCurrentItem(position, false);
|
||||
};
|
||||
handler.postDelayed(r, 100);
|
||||
}
|
||||
|
||||
private void setSongInfo(Song song) {
|
||||
playerBottomSheetViewModel.setNowPlayingSong(song);
|
||||
|
||||
|
|
@ -164,6 +192,11 @@ public class PlayerBottomSheetFragment extends Fragment implements MusicServiceE
|
|||
bind.playerHeaderLayout.playerHeaderSongTitleLabel.setText(song.getTitle());
|
||||
bind.playerHeaderLayout.playerHeaderSongArtistLabel.setText(song.getArtistName());
|
||||
|
||||
CustomGlideRequest.Builder
|
||||
.from(requireContext(), song.getPrimary(), song.getBlurHash(), CustomGlideRequest.PRIMARY, CustomGlideRequest.TOP_QUALITY)
|
||||
.build()
|
||||
.into(bind.playerHeaderLayout.playerHeaderSongCoverImage);
|
||||
|
||||
bind.playerBodyLayout.buttonFavorite.setChecked(song.isFavorite());
|
||||
}
|
||||
|
||||
|
|
@ -176,9 +209,6 @@ public class PlayerBottomSheetFragment extends Fragment implements MusicServiceE
|
|||
}
|
||||
|
||||
private void setUpMusicControllers() {
|
||||
// setUpPrevNext();
|
||||
// setUpRepeatButton();
|
||||
// setUpShuffleButton();
|
||||
initSeekBar();
|
||||
}
|
||||
|
||||
|
|
@ -197,6 +227,7 @@ public class PlayerBottomSheetFragment extends Fragment implements MusicServiceE
|
|||
|
||||
@Override
|
||||
public void onServiceConnected() {
|
||||
setSongInfo(MusicPlayerRemote.getCurrentSong());
|
||||
updatePlayPauseState();
|
||||
}
|
||||
|
||||
|
|
@ -212,7 +243,8 @@ public class PlayerBottomSheetFragment extends Fragment implements MusicServiceE
|
|||
|
||||
@Override
|
||||
public void onPlayMetadataChanged() {
|
||||
|
||||
setViewPageDelayed(MusicPlayerRemote.getPosition());
|
||||
setSongInfo(MusicPlayerRemote.getCurrentSong());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -38,11 +38,7 @@ public class PlayerBottomSheetViewModel extends AndroidViewModel {
|
|||
}
|
||||
|
||||
public void setFavorite() {
|
||||
if(song.isFavorite())
|
||||
song.setFavorite(false);
|
||||
else
|
||||
song.setFavorite(true);
|
||||
|
||||
song.setFavorite(!song.isFavorite());
|
||||
songRepository.setFavoriteStatus(song);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue