Skip to content

Commit

Permalink
only save playback time in MediaSessionCompatCallback#onStop than p…
Browse files Browse the repository at this point in the history
…olling from slider updates
  • Loading branch information
VishnuSanal committed Sep 7, 2023
1 parent 7512962 commit a2ba405
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,6 @@ void buildTransportControls() {
MediaControllerCompat.getMediaController(MainActivity.this)
.getTransportControls()
.seekTo((int) value);

long position = (long) slider.getValue();

if (id != -1)
if (position != slider.getValueTo())
viewModel.insert(new SaveItem(id, position));
else viewModel.delete(new SaveItem(id, position));
});

rewindIV.setOnClickListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
Expand Down Expand Up @@ -253,6 +254,8 @@ public void onStop() {

mediaSession.setActive(false);

savePosition(mediaPlayer.getCurrentPosition(), mediaPlayer.getDuration());

mediaPlayer.release();
updateHandler.removeCallbacks(updateRunnable);

Expand Down Expand Up @@ -323,6 +326,25 @@ public void onSetRepeatMode(int repeatMode) {
setSessionToken(mediaSession.getSessionToken());
}

private void savePosition(int currentPosition, int duration) {

Log.e("vishnu", "savePosition: " + currentPosition + " / " + duration);

long id = audio.getId();

if (id == -1) return;

AsyncTask.execute(
() -> {
SaveItemRepository saveItemRepository =
new SaveItemRepository(getApplication());

if (currentPosition != duration)
saveItemRepository.insertSaveItem(new SaveItem(id, currentPosition));
else saveItemRepository.deleteSaveItem(new SaveItem(id, currentPosition));
});
}

@Override
public void onDestroy() {
unregisterReceiver(becomingNoisyReceiver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ class SaveItemRepository(application: Application?) {
return saveItemDao.getSaveItem(id)
}

suspend fun insertSaveItem(saveItem: SaveItem?) {
fun insertSaveItem(saveItem: SaveItem?) {
saveItemDao.insert(saveItem)
}

suspend fun updateSaveItem(saveItem: SaveItem?) {
saveItemDao.update(saveItem)
}

suspend fun deleteSaveItem(saveItem: SaveItem?) {
fun deleteSaveItem(saveItem: SaveItem?) {
saveItemDao.delete(saveItem)
}

Expand Down

0 comments on commit a2ba405

Please sign in to comment.