mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Pause (but not reset) music on bottom sheet collapsed
This commit is contained in:
parent
d526b3163f
commit
6aafc7dfac
2 changed files with 40 additions and 11 deletions
|
|
@ -20,6 +20,22 @@ import java.util.concurrent.ExecutionException;
|
|||
public class MediaManager {
|
||||
private static final String TAG = "MediaManager";
|
||||
|
||||
public static void quit(ListenableFuture<MediaBrowser> mediaBrowserListenableFuture) {
|
||||
if (mediaBrowserListenableFuture != null) {
|
||||
mediaBrowserListenableFuture.addListener(() -> {
|
||||
try {
|
||||
if (mediaBrowserListenableFuture.isDone()) {
|
||||
if (mediaBrowserListenableFuture.get().isPlaying()) {
|
||||
mediaBrowserListenableFuture.get().pause();
|
||||
}
|
||||
}
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
}
|
||||
}, MoreExecutors.directExecutor());
|
||||
}
|
||||
}
|
||||
|
||||
public static void check(ListenableFuture<MediaBrowser> mediaBrowserListenableFuture, Context context) {
|
||||
if (mediaBrowserListenableFuture != null) {
|
||||
mediaBrowserListenableFuture.addListener(() -> {
|
||||
|
|
@ -136,7 +152,6 @@ public class MediaManager {
|
|||
mediaBrowserListenableFuture.get().prepare();
|
||||
mediaBrowserListenableFuture.get().seekTo(startIndex, 0);
|
||||
mediaBrowserListenableFuture.get().play();
|
||||
|
||||
enqueueDatabase(songs, true, 0);
|
||||
}
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
|
|
@ -155,7 +170,6 @@ public class MediaManager {
|
|||
mediaBrowserListenableFuture.get().setMediaItem(MappingUtil.mapMediaItem(context, song));
|
||||
mediaBrowserListenableFuture.get().prepare();
|
||||
mediaBrowserListenableFuture.get().play();
|
||||
|
||||
enqueueDatabase(song, true, 0);
|
||||
}
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
|
|
@ -170,15 +184,13 @@ public class MediaManager {
|
|||
mediaBrowserListenableFuture.addListener(() -> {
|
||||
try {
|
||||
if (mediaBrowserListenableFuture.isDone()) {
|
||||
if (playImmediatelyAfter && mediaBrowserListenableFuture.get().getNextMediaItemIndex() != -1) {
|
||||
if (playImmediatelyAfter && mediaBrowserListenableFuture.get().getNextMediaItemIndex() != -1) {
|
||||
enqueueDatabase(songs, false, mediaBrowserListenableFuture.get().getNextMediaItemIndex());
|
||||
mediaBrowserListenableFuture.get().addMediaItems(mediaBrowserListenableFuture.get().getNextMediaItemIndex(), MappingUtil.mapMediaItems(context, songs));
|
||||
} else {
|
||||
enqueueDatabase(songs, false, mediaBrowserListenableFuture.get().getMediaItemCount());
|
||||
mediaBrowserListenableFuture.get().addMediaItems(MappingUtil.mapMediaItems(context, songs));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import android.view.View;
|
|||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.media3.common.Player;
|
||||
import androidx.navigation.NavController;
|
||||
import androidx.navigation.fragment.NavHostFragment;
|
||||
import androidx.navigation.ui.NavigationUI;
|
||||
|
|
@ -29,8 +30,10 @@ import com.cappielloantonio.play.util.PreferenceUtil;
|
|||
import com.cappielloantonio.play.viewmodel.MainViewModel;
|
||||
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
||||
import com.google.android.material.bottomsheet.BottomSheetBehavior;
|
||||
import com.google.common.util.concurrent.MoreExecutors;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class MainActivity extends BaseActivity {
|
||||
private static final String TAG = "MainActivity";
|
||||
|
|
@ -66,7 +69,7 @@ public class MainActivity extends BaseActivity {
|
|||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
initServiceContent();
|
||||
initService();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -149,8 +152,8 @@ public class MainActivity extends BaseActivity {
|
|||
break;
|
||||
case BottomSheetBehavior.STATE_COLLAPSED:
|
||||
if (playerBottomSheetFragment != null) {
|
||||
playerBottomSheetFragment.scrollOnTop();
|
||||
playerBottomSheetFragment.goBackToFirstPage();
|
||||
playerBottomSheetFragment.scrollOnTop();
|
||||
}
|
||||
case BottomSheetBehavior.STATE_SETTLING:
|
||||
if (playerBottomSheetFragment != null) {
|
||||
|
|
@ -210,8 +213,24 @@ public class MainActivity extends BaseActivity {
|
|||
}
|
||||
}
|
||||
|
||||
private void initServiceContent() {
|
||||
@SuppressLint("UnsafeOptInUsageError")
|
||||
private void initService() {
|
||||
MediaManager.check(getMediaBrowserListenableFuture(), this);
|
||||
|
||||
getMediaBrowserListenableFuture().addListener(() -> {
|
||||
try {
|
||||
getMediaBrowserListenableFuture().get().addListener(new Player.Listener() {
|
||||
@Override
|
||||
public void onIsPlayingChanged(boolean isPlaying) {
|
||||
if (isPlaying && bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
|
||||
setBottomSheetInPeek(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, MoreExecutors.directExecutor());
|
||||
}
|
||||
|
||||
private void goToLogin() {
|
||||
|
|
@ -259,9 +278,7 @@ public class MainActivity extends BaseActivity {
|
|||
}
|
||||
|
||||
private void resetMusicSession() {
|
||||
QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
||||
queueRepository.deleteAll();
|
||||
// MusicPlayerRemote.quitPlaying();
|
||||
MediaManager.quit(getMediaBrowserListenableFuture());
|
||||
}
|
||||
|
||||
private void resetViewModel() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue