From 5c381defa102141e70d52ab6ed2f628f9adb8f02 Mon Sep 17 00:00:00 2001 From: IacobIonut01 Date: Mon, 30 Sep 2024 13:04:50 +0300 Subject: [PATCH] Allow Video Playback Without Pausing Other Audio Add new setting to disable audio focus in video player, allowing other apps to play in background while playing videos in the Gallery. Fixes [Enhancement] Allow Video Playback Without Pausing Other Audio #480 Signed-off-by: IacobIonut01 --- app/build.gradle.kts | 2 +- .../kotlin/com/dot/gallery/core/Settings.kt | 6 ++++++ .../mediaview/components/video/VideoPlayer.kt | 4 +++- .../presentation/settings/SettingsScreen.kt | 21 ++++++++++++++++++- app/src/main/res/values/strings.xml | 2 ++ 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index e714e716b..9b15385d3 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -22,7 +22,7 @@ android { applicationId = "com.dot.gallery" minSdk = 30 targetSdk = 35 - versionCode = 30109 + versionCode = 30115 versionName = "3.0.1" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/kotlin/com/dot/gallery/core/Settings.kt b/app/src/main/kotlin/com/dot/gallery/core/Settings.kt index 314f1fa32..dc42b26ec 100644 --- a/app/src/main/kotlin/com/dot/gallery/core/Settings.kt +++ b/app/src/main/kotlin/com/dot/gallery/core/Settings.kt @@ -261,6 +261,12 @@ object Settings { @Composable fun rememberAutoHideNavBar() = rememberPreference(key = AUTO_HIDE_NAVIGATIONBAR, defaultValue = true) + + private val AUDIO_FOCUS = booleanPreferencesKey("audio_focus") + + @Composable + fun rememberAudioFocus() = + rememberPreference(key = AUDIO_FOCUS, defaultValue = true) } } diff --git a/app/src/main/kotlin/com/dot/gallery/feature_node/presentation/mediaview/components/video/VideoPlayer.kt b/app/src/main/kotlin/com/dot/gallery/feature_node/presentation/mediaview/components/video/VideoPlayer.kt index 6833ffb28..083eada2c 100644 --- a/app/src/main/kotlin/com/dot/gallery/feature_node/presentation/mediaview/components/video/VideoPlayer.kt +++ b/app/src/main/kotlin/com/dot/gallery/feature_node/presentation/mediaview/components/video/VideoPlayer.kt @@ -37,6 +37,7 @@ import androidx.media3.common.util.UnstableApi import androidx.media3.exoplayer.ExoPlayer import com.dot.gallery.core.Constants.Animation.enterAnimation import com.dot.gallery.core.Constants.Animation.exitAnimation +import com.dot.gallery.core.Settings.Misc.rememberAudioFocus import com.dot.gallery.core.presentation.components.util.swipe import com.dot.gallery.feature_node.domain.model.Media import io.sanghun.compose.video.RepeatMode @@ -113,6 +114,7 @@ fun VideoPlayer( } if (showPlayer) { + val audioFocus by rememberAudioFocus() SanghunComposeVideoVideoPlayer( mediaItems = listOf( VideoPlayerMediaItem.StorageMediaItem( @@ -124,7 +126,7 @@ fun VideoPlayer( autoPlay = playWhenReady, usePlayerController = false, enablePip = false, - handleAudioFocus = true, + handleAudioFocus = audioFocus, repeatMode = RepeatMode.ONE, playerInstance = { exoPlayer = this diff --git a/app/src/main/kotlin/com/dot/gallery/feature_node/presentation/settings/SettingsScreen.kt b/app/src/main/kotlin/com/dot/gallery/feature_node/presentation/settings/SettingsScreen.kt index 312bd508d..27be35693 100644 --- a/app/src/main/kotlin/com/dot/gallery/feature_node/presentation/settings/SettingsScreen.kt +++ b/app/src/main/kotlin/com/dot/gallery/feature_node/presentation/settings/SettingsScreen.kt @@ -59,6 +59,7 @@ import androidx.compose.ui.window.DialogProperties import com.dot.gallery.R import com.dot.gallery.core.Position import com.dot.gallery.core.Settings +import com.dot.gallery.core.Settings.Misc.rememberAudioFocus import com.dot.gallery.core.Settings.Misc.rememberAutoHideNavBar import com.dot.gallery.core.Settings.Misc.rememberAutoHideSearchBar import com.dot.gallery.core.Settings.Misc.rememberForcedLastScreen @@ -380,7 +381,7 @@ fun rememberSettingsList( title = context.getString(R.string.set_default_screen), summary = summary, onClick = { showLaunchScreenDialog.value = true }, - screenPosition = Position.Bottom + screenPosition = Position.Middle ) } @@ -408,6 +409,23 @@ fun rememberSettingsList( ) } + var audioFocus by rememberAudioFocus() + val audioFocusPref = remember(audioFocus) { + SettingsEntity.SwitchPreference( + title = context.getString(R.string.take_audio_focus_title), + summary = context.getString(R.string.take_audio_focus_summary), + isChecked = audioFocus, + onCheck = { + scope.launch { + audioFocus = it + delay(50) + context.restartApplication() + } + }, + screenPosition = Position.Bottom + ) + } + return remember( arrayOf( forceTheme, @@ -444,6 +462,7 @@ fun rememberSettingsList( add(allowBlurPref) add(hideTimelineOnAlbumPref) add(forcedLastScreenPref) + add(audioFocusPref) /** ********************* **/ /** ********************* **/ /** Navigation Section Start **/ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index da3444666..b36a54146 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -261,6 +261,8 @@ Update file name Support the project Click to copy + Take Audio Focus + Enabling this will take audio focus when playing media. Any media playing in background will pause while the video is playing. (Restart app required) %s item %s items