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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item android:drawable="@drawable/ic_play_circle" android:state_checked="true" android:color="@color/titleTextColor"/>
|
||||
<item android:drawable="@drawable/ic_pause_circle" android:state_checked="false" android:color="@color/titleTextColor" />
|
||||
<item android:drawable="@drawable/ic_play" android:state_checked="true" android:color="@color/titleTextColor"/>
|
||||
<item android:drawable="@drawable/ic_pause" android:state_checked="false" android:color="@color/titleTextColor" />
|
||||
</selector>
|
||||
9
app/src/main/res/drawable/ic_pause.xml
Normal file
9
app/src/main/res/drawable/ic_pause.xml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@color/titleTextColor"
|
||||
android:pathData="M6,19h4L10,5L6,5v14zM14,5v14h4L18,5h-4z"/>
|
||||
</vector>
|
||||
|
|
@ -5,5 +5,5 @@
|
|||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="@color/titleTextColor"
|
||||
android:pathData="M8,6.82v10.36c0,0.79 0.87,1.27 1.54,0.84l8.14,-5.18c0.62,-0.39 0.62,-1.29 0,-1.69L9.54,5.98C8.87,5.55 8,6.03 8,6.82z"/>
|
||||
android:pathData="M8,5v14l11,-7z"/>
|
||||
</vector>
|
||||
|
|
|
|||
|
|
@ -5,12 +5,12 @@
|
|||
android:layout_height="match_parent"
|
||||
android:layout_margin="24dp"
|
||||
android:backgroundTint="@color/cardColor"
|
||||
app:cardCornerRadius="4dp">
|
||||
app:cardCornerRadius="4dp"
|
||||
android:foreground="?android:attr/selectableItemBackground">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/now_playing_song_cover_image_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:foreground="?android:attr/selectableItemBackground"/>
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
|
@ -7,33 +7,41 @@
|
|||
android:elevation="2dp"
|
||||
android:background="@color/almostCardColor">
|
||||
|
||||
<ToggleButton
|
||||
android:id="@+id/player_header_button"
|
||||
android:layout_width="38dp"
|
||||
android:layout_height="38dp"
|
||||
android:layout_marginStart="20dp"
|
||||
android:background="@drawable/button_play_pause_selector"
|
||||
android:checked="true"
|
||||
android:text=""
|
||||
android:textOff=""
|
||||
android:textOn=""
|
||||
<androidx.cardview.widget.CardView
|
||||
xmlns:card_view="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/player_header_song_cover_card_view"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="12dp"
|
||||
android:layout_gravity="center"
|
||||
android:backgroundTint="@color/cardColor"
|
||||
card_view:cardCornerRadius="4dp"
|
||||
card_view:cardElevation="2dp"
|
||||
card_view:cardPreventCornerOverlap="false"
|
||||
card_view:cardUseCompatPadding="true"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/player_header_song_cover_image"
|
||||
android:layout_width="42dp"
|
||||
android:layout_height="42dp" />
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/player_header_song_title_label"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:maxLines="1"
|
||||
android:ellipsize="end"
|
||||
android:layout_marginTop="10dp"
|
||||
android:paddingStart="12dp"
|
||||
android:paddingEnd="24dp"
|
||||
android:text="@string/label_placeholder"
|
||||
android:paddingStart="8dp"
|
||||
android:textColor="@color/titleTextColor"
|
||||
android:textSize="14sp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/player_header_button"
|
||||
app:layout_constraintEnd_toStartOf="@+id/player_header_button"
|
||||
app:layout_constraintStart_toEndOf="@+id/player_header_song_cover_card_view"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
|
|
@ -41,13 +49,26 @@
|
|||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:fontFamily="@font/open_sans_font_family"
|
||||
android:paddingStart="12dp"
|
||||
android:paddingEnd="24dp"
|
||||
android:text="@string/label_placeholder"
|
||||
android:paddingStart="8dp"
|
||||
android:textColor="@color/subtitleTextColor"
|
||||
android:textSize="12sp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/player_header_button"
|
||||
app:layout_constraintEnd_toStartOf="@+id/player_header_button"
|
||||
app:layout_constraintStart_toEndOf="@+id/player_header_song_cover_card_view"
|
||||
app:layout_constraintTop_toBottomOf="@+id/player_header_song_title_label" />
|
||||
|
||||
<ToggleButton
|
||||
android:id="@+id/player_header_button"
|
||||
android:layout_width="32dp"
|
||||
android:layout_height="32dp"
|
||||
android:layout_marginStart="12dp"
|
||||
android:layout_marginEnd="12dp"
|
||||
android:background="@drawable/button_play_pause_selector"
|
||||
android:checked="true"
|
||||
android:text=""
|
||||
android:textOff=""
|
||||
android:textOn=""
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
Loading…
Add table
Add a link
Reference in a new issue