From be223883c230e0f946944da1660204aaf27201e1 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Tue, 17 Nov 2020 12:06:49 +0100 Subject: [PATCH 1/2] Fix falsely starting player service when previewing video Signed-off-by: tobiasKaminsky --- src/main/java/com/nextcloud/client/media/PlayerService.kt | 1 - .../com/owncloud/android/ui/preview/PreviewMediaFragment.java | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/nextcloud/client/media/PlayerService.kt b/src/main/java/com/nextcloud/client/media/PlayerService.kt index d4a4246c1e85..61993746a4b7 100644 --- a/src/main/java/com/nextcloud/client/media/PlayerService.kt +++ b/src/main/java/com/nextcloud/client/media/PlayerService.kt @@ -35,7 +35,6 @@ import com.owncloud.android.datamodel.OCFile import com.owncloud.android.ui.notifications.NotificationUtils import com.owncloud.android.utils.ThemeUtils import dagger.android.AndroidInjection -import java.lang.IllegalArgumentException import javax.inject.Inject class PlayerService : Service() { diff --git a/src/main/java/com/owncloud/android/ui/preview/PreviewMediaFragment.java b/src/main/java/com/owncloud/android/ui/preview/PreviewMediaFragment.java index bd57c3728862..ca0f3c1a8ae7 100644 --- a/src/main/java/com/owncloud/android/ui/preview/PreviewMediaFragment.java +++ b/src/main/java/com/owncloud/android/ui/preview/PreviewMediaFragment.java @@ -324,7 +324,9 @@ public void onStart() { mMultiListContainer.setVisibility(View.GONE); mPreviewContainer.setVisibility(View.VISIBLE); } else if (MimeTypeUtil.isVideo(file)) { - stopAudio(); + if (mMediaPlayerServiceConnection.isConnected() && mMediaPlayerServiceConnection.isPlaying()) { + stopAudio(); + } playVideo(); } } From 343561a013c3fdbe84fc71f2ab3db786e54dfbbf Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Fri, 20 Nov 2020 09:39:11 +0100 Subject: [PATCH 2/2] Add toggle function Signed-off-by: tobiasKaminsky --- .../nextcloud/client/media/PlayerService.kt | 41 +++++++++++++++++-- .../ui/preview/PreviewMediaFragment.java | 7 +++- src/main/res/values/strings.xml | 2 + 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/nextcloud/client/media/PlayerService.kt b/src/main/java/com/nextcloud/client/media/PlayerService.kt index 61993746a4b7..e708d8b09d60 100644 --- a/src/main/java/com/nextcloud/client/media/PlayerService.kt +++ b/src/main/java/com/nextcloud/client/media/PlayerService.kt @@ -35,6 +35,7 @@ import com.owncloud.android.datamodel.OCFile import com.owncloud.android.ui.notifications.NotificationUtils import com.owncloud.android.utils.ThemeUtils import dagger.android.AndroidInjection +import java.util.Locale import javax.inject.Inject class PlayerService : Service() { @@ -46,6 +47,7 @@ class PlayerService : Service() { const val EXTRA_START_POSITION_MS = "START_POSITION_MS" const val ACTION_PLAY = "PLAY" const val ACTION_STOP = "STOP" + const val ACTION_TOGGLE = "TOGGLE" const val ACTION_STOP_FILE = "STOP_FILE" } @@ -73,7 +75,7 @@ class PlayerService : Service() { } override fun onStop() { - stopForeground(true) + stopServiceAndRemoveNotification(null) } override fun onError(error: PlayerError) { @@ -96,10 +98,20 @@ class PlayerService : Service() { player = Player(applicationContext, clientFactory, playerListener, audioManager) notificationBuilder = NotificationCompat.Builder(this) notificationBuilder.color = ThemeUtils.primaryColor(this) + val stop = Intent(this, PlayerService::class.java) stop.action = ACTION_STOP val pendingStop = PendingIntent.getService(this, 0, stop, 0) - notificationBuilder.addAction(0, "STOP", pendingStop) + notificationBuilder.addAction(0, getString(R.string.player_stop).toUpperCase(Locale.getDefault()), pendingStop) + + val toggle = Intent(this, PlayerService::class.java) + toggle.action = ACTION_TOGGLE + val pendingToggle = PendingIntent.getService(this, 0, toggle, 0) + notificationBuilder.addAction( + 0, + getString(R.string.player_toggle).toUpperCase(Locale.getDefault()), + pendingToggle + ) } override fun onBind(intent: Intent?): IBinder? { @@ -111,10 +123,19 @@ class PlayerService : Service() { ACTION_PLAY -> onActionPlay(intent) ACTION_STOP -> onActionStop() ACTION_STOP_FILE -> onActionStopFile(intent.extras) + ACTION_TOGGLE -> onActionToggle() } return START_NOT_STICKY } + private fun onActionToggle() { + if (player.isPlaying) { + player.pause() + } else { + player.start() + } + } + private fun onActionPlay(intent: Intent) { val user: User = intent.getParcelableExtra(EXTRA_USER) as User val file: OCFile = intent.getParcelableExtra(EXTRA_FILE) as OCFile @@ -125,12 +146,13 @@ class PlayerService : Service() { } private fun onActionStop() { - player.stop() + stopServiceAndRemoveNotification(null) } private fun onActionStopFile(args: Bundle?) { val file: OCFile = args?.getParcelable(EXTRA_FILE) ?: throw IllegalArgumentException("Missing file argument") - player.stop(file) + + stopServiceAndRemoveNotification(file) } private fun startForeground(currentFile: OCFile) { @@ -148,4 +170,15 @@ class PlayerService : Service() { startForeground(R.string.media_notif_ticker, notificationBuilder.build()) } + + private fun stopServiceAndRemoveNotification(file: OCFile?) { + if (file == null) { + player.stop() + } else { + player.stop(file) + } + + stopSelf() + stopForeground(true) + } } diff --git a/src/main/java/com/owncloud/android/ui/preview/PreviewMediaFragment.java b/src/main/java/com/owncloud/android/ui/preview/PreviewMediaFragment.java index ca0f3c1a8ae7..ab3ff9be0ab1 100644 --- a/src/main/java/com/owncloud/android/ui/preview/PreviewMediaFragment.java +++ b/src/main/java/com/owncloud/android/ui/preview/PreviewMediaFragment.java @@ -317,14 +317,17 @@ public void onStart() { Log_OC.v(TAG, "onStart"); OCFile file = getFile(); if (file != null) { + // bind to any existing player + mMediaPlayerServiceConnection.bind(); + if (MimeTypeUtil.isAudio(file)) { mMediaController.setMediaPlayer(mMediaPlayerServiceConnection); - mMediaPlayerServiceConnection.bind(); mMediaPlayerServiceConnection.start(user, file, mAutoplay, mSavedPlaybackPosition); mMultiListContainer.setVisibility(View.GONE); mPreviewContainer.setVisibility(View.VISIBLE); } else if (MimeTypeUtil.isVideo(file)) { - if (mMediaPlayerServiceConnection.isConnected() && mMediaPlayerServiceConnection.isPlaying()) { + if (mMediaPlayerServiceConnection.isConnected()) { + // always stop player stopAudio(); } playVideo(); diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 8025da513514..4f5f6e28d827 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -945,4 +945,6 @@ 4 hours This week Media + stop + toggle