mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Saved the playback position when the player is paused
This commit is contained in:
parent
e4d09f3bc0
commit
40866a2855
7 changed files with 101 additions and 13 deletions
|
|
@ -3,6 +3,7 @@ package com.cappielloantonio.play.database;
|
|||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.room.AutoMigration;
|
||||
import androidx.room.Database;
|
||||
import androidx.room.Room;
|
||||
import androidx.room.RoomDatabase;
|
||||
|
|
@ -20,9 +21,9 @@ import com.cappielloantonio.play.model.Server;
|
|||
|
||||
@SuppressLint("RestrictedApi")
|
||||
@Database(
|
||||
version = 29,
|
||||
entities = {Queue.class, Server.class, RecentSearch.class, Download.class, Playlist.class}
|
||||
// autoMigrations = { @AutoMigration(from = 23, to = 24) }
|
||||
version = 30,
|
||||
entities = {Queue.class, Server.class, RecentSearch.class, Download.class, Playlist.class},
|
||||
autoMigrations = { @AutoMigration(from = 29, to = 30) }
|
||||
)
|
||||
public abstract class AppDatabase extends RoomDatabase {
|
||||
private static final String TAG = "AppDatabase";
|
||||
|
|
|
|||
|
|
@ -37,6 +37,12 @@ public interface QueueDao {
|
|||
@Query("UPDATE queue SET last_play=:timestamp WHERE id=:id")
|
||||
void setLastPlay(String id, long timestamp);
|
||||
|
||||
@Query("UPDATE queue SET playing_changed=:timestamp WHERE id=:id")
|
||||
void setPlayingChanged(String id, long timestamp);
|
||||
|
||||
@Query("SELECT track_order FROM queue ORDER BY last_play DESC LIMIT 1")
|
||||
int getLastPlay();
|
||||
|
||||
@Query("SELECT playing_changed FROM queue ORDER BY last_play DESC LIMIT 1")
|
||||
long getLastPlayedTimestamp();
|
||||
}
|
||||
|
|
@ -34,10 +34,13 @@ public class Queue {
|
|||
@ColumnInfo(name = "duration")
|
||||
private long duration;
|
||||
|
||||
@ColumnInfo(name = "last_play")
|
||||
@ColumnInfo(name = "last_play",defaultValue = "0")
|
||||
private long lastPlay;
|
||||
|
||||
public Queue(int trackOrder, String songID, String title, String albumId, String albumName, String artistId, String artistName, String primary, long duration, long lastPlay) {
|
||||
@ColumnInfo(name = "playing_changed", defaultValue = "0")
|
||||
private long playingChanged;
|
||||
|
||||
public Queue(int trackOrder, String songID, String title, String albumId, String albumName, String artistId, String artistName, String primary, long duration, long lastPlay, long playingChanged) {
|
||||
this.trackOrder = trackOrder;
|
||||
this.songID = songID;
|
||||
this.title = title;
|
||||
|
|
@ -48,6 +51,7 @@ public class Queue {
|
|||
this.primary = primary;
|
||||
this.duration = duration;
|
||||
this.lastPlay = lastPlay;
|
||||
this.playingChanged = playingChanged;
|
||||
}
|
||||
|
||||
public int getTrackOrder() {
|
||||
|
|
@ -129,4 +133,12 @@ public class Queue {
|
|||
public void setLastPlay(long lastPlay) {
|
||||
this.lastPlay = lastPlay;
|
||||
}
|
||||
|
||||
public long getPlayingChanged() {
|
||||
return playingChanged;
|
||||
}
|
||||
|
||||
public void setPlayingChanged(long playingChanged) {
|
||||
this.playingChanged = playingChanged;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -128,8 +128,14 @@ public class QueueRepository {
|
|||
return count;
|
||||
}
|
||||
|
||||
public void setTimestamp(String id) {
|
||||
SetTimestampThreadSafe timestamp = new SetTimestampThreadSafe(queueDao, id);
|
||||
public void setLastPlayedTimestamp(String id) {
|
||||
SetLastPlayedTimestampThreadSafe timestamp = new SetLastPlayedTimestampThreadSafe(queueDao, id);
|
||||
Thread thread = new Thread(timestamp);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public void setPlayingChangedTimestamp(String id, long ms) {
|
||||
SetPlayingChangedTimestampThreadSafe timestamp = new SetPlayingChangedTimestampThreadSafe(queueDao, id, ms);
|
||||
Thread thread = new Thread(timestamp);
|
||||
thread.start();
|
||||
}
|
||||
|
|
@ -151,6 +157,23 @@ public class QueueRepository {
|
|||
return index;
|
||||
}
|
||||
|
||||
public long getLastPlayedSongTimestamp() {
|
||||
long timestamp = 0;
|
||||
|
||||
GetLastPlayedSongTimestampThreadSafe getLastPlayedSongTimestampThreadSafe = new GetLastPlayedSongTimestampThreadSafe(queueDao);
|
||||
Thread thread = new Thread(getLastPlayedSongTimestampThreadSafe);
|
||||
thread.start();
|
||||
|
||||
try {
|
||||
thread.join();
|
||||
timestamp = getLastPlayedSongTimestampThreadSafe.getTimestamp();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
private static class GetSongsThreadSafe implements Runnable {
|
||||
private final QueueDao queueDao;
|
||||
private List<Song> songs;
|
||||
|
|
@ -230,11 +253,11 @@ public class QueueRepository {
|
|||
}
|
||||
}
|
||||
|
||||
private static class SetTimestampThreadSafe implements Runnable {
|
||||
private static class SetLastPlayedTimestampThreadSafe implements Runnable {
|
||||
private final QueueDao queueDao;
|
||||
private final String songId;
|
||||
|
||||
public SetTimestampThreadSafe(QueueDao queueDao, String songId) {
|
||||
public SetLastPlayedTimestampThreadSafe(QueueDao queueDao, String songId) {
|
||||
this.queueDao = queueDao;
|
||||
this.songId = songId;
|
||||
}
|
||||
|
|
@ -245,6 +268,23 @@ public class QueueRepository {
|
|||
}
|
||||
}
|
||||
|
||||
private static class SetPlayingChangedTimestampThreadSafe implements Runnable {
|
||||
private final QueueDao queueDao;
|
||||
private final String songId;
|
||||
private final long ms;
|
||||
|
||||
public SetPlayingChangedTimestampThreadSafe(QueueDao queueDao, String songId, long ms) {
|
||||
this.queueDao = queueDao;
|
||||
this.songId = songId;
|
||||
this.ms = ms;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
queueDao.setPlayingChanged(songId, ms);
|
||||
}
|
||||
}
|
||||
|
||||
private static class GetLastPlayedSongThreadSafe implements Runnable {
|
||||
private final QueueDao queueDao;
|
||||
private int index;
|
||||
|
|
@ -262,4 +302,22 @@ public class QueueRepository {
|
|||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
private static class GetLastPlayedSongTimestampThreadSafe implements Runnable {
|
||||
private final QueueDao queueDao;
|
||||
private long timestamp;
|
||||
|
||||
public GetLastPlayedSongTimestampThreadSafe(QueueDao queueDao) {
|
||||
this.queueDao = queueDao;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
timestamp = queueDao.getLastPlayedTimestamp();
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ public class MediaManager {
|
|||
if (mediaBrowserListenableFuture.isDone()) {
|
||||
mediaBrowserListenableFuture.get().clearMediaItems();
|
||||
mediaBrowserListenableFuture.get().setMediaItems(MappingUtil.mapMediaItems(context, songs, true));
|
||||
mediaBrowserListenableFuture.get().seekTo(getQueueRepository().getLastPlayedSongIndex(), 0);
|
||||
mediaBrowserListenableFuture.get().seekTo(getQueueRepository().getLastPlayedSongIndex(), getQueueRepository().getLastPlayedSongTimestamp());
|
||||
mediaBrowserListenableFuture.get().prepare();
|
||||
}
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
|
|
@ -268,7 +268,11 @@ public class MediaManager {
|
|||
}
|
||||
|
||||
public static void setLastPlayedTimestamp(MediaItem mediaItem) {
|
||||
if (mediaItem != null) getQueueRepository().setTimestamp(mediaItem.mediaId);
|
||||
if (mediaItem != null) getQueueRepository().setLastPlayedTimestamp(mediaItem.mediaId);
|
||||
}
|
||||
|
||||
public static void setPlayingChangedTimestamp(MediaItem mediaItem, long ms) {
|
||||
if (mediaItem != null) getQueueRepository().setPlayingChangedTimestamp(mediaItem.mediaId, ms);
|
||||
}
|
||||
|
||||
private static QueueRepository getQueueRepository() {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package com.cappielloantonio.play.service;
|
|||
import android.annotation.SuppressLint;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.media3.common.AudioAttributes;
|
||||
|
|
@ -18,7 +19,6 @@ import androidx.media3.session.MediaSession;
|
|||
|
||||
import com.cappielloantonio.play.ui.activity.MainActivity;
|
||||
import com.cappielloantonio.play.util.DownloadUtil;
|
||||
import com.google.android.gms.cast.framework.CastContext;
|
||||
|
||||
public class MediaService extends MediaLibraryService {
|
||||
private static final String TAG = "MediaService";
|
||||
|
|
@ -98,6 +98,13 @@ public class MediaService extends MediaLibraryService {
|
|||
public void onMediaItemTransition(@Nullable MediaItem mediaItem, int reason) {
|
||||
MediaManager.setLastPlayedTimestamp(mediaItem);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onIsPlayingChanged(boolean isPlaying) {
|
||||
if(isPlaying) {
|
||||
MediaManager.setPlayingChangedTimestamp(player.getCurrentMediaItem(), player.getCurrentPosition());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -101,7 +101,7 @@ public class MappingUtil {
|
|||
}
|
||||
|
||||
public static Queue mapSongToQueue(Song song, int trackOrder) {
|
||||
return new Queue(trackOrder, song.getId(), song.getTitle(), song.getAlbumId(), song.getAlbumName(), song.getArtistId(), song.getArtistName(), song.getPrimary(), song.getDuration(), 0);
|
||||
return new Queue(trackOrder, song.getId(), song.getTitle(), song.getAlbumId(), song.getAlbumName(), song.getArtistId(), song.getArtistName(), song.getPrimary(), song.getDuration(), 0, 0);
|
||||
}
|
||||
|
||||
public static List<Queue> mapSongsToQueue(List<Song> songs) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue