Skip to content

Commit

Permalink
Allow Video Playback Without Pausing Other Audio
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
IacobIonut01 committed Sep 30, 2024
1 parent cbe7bbe commit 5c381de
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/kotlin/com/dot/gallery/core/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,6 +114,7 @@ fun VideoPlayer(
}

if (showPlayer) {
val audioFocus by rememberAudioFocus()
SanghunComposeVideoVideoPlayer(
mediaItems = listOf(
VideoPlayerMediaItem.StorageMediaItem(
Expand All @@ -124,7 +126,7 @@ fun VideoPlayer(
autoPlay = playWhenReady,
usePlayerController = false,
enablePip = false,
handleAudioFocus = true,
handleAudioFocus = audioFocus,
repeatMode = RepeatMode.ONE,
playerInstance = {
exoPlayer = this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -444,6 +462,7 @@ fun rememberSettingsList(
add(allowBlurPref)
add(hideTimelineOnAlbumPref)
add(forcedLastScreenPref)
add(audioFocusPref)
/** ********************* **/
/** ********************* **/
/** Navigation Section Start **/
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@
<string name="update_file_name">Update file name</string>
<string name="support_the_project">Support the project</string>
<string name="click_to_copy">Click to copy</string>
<string name="take_audio_focus_title">Take Audio Focus</string>
<string name="take_audio_focus_summary">Enabling this will take audio focus when playing media. Any media playing in background will pause while the video is playing. (Restart app required)</string>
<plurals name="item_count">
<item quantity="one">%s item</item>
<item quantity="other">%s items</item>
Expand Down

0 comments on commit 5c381de

Please sign in to comment.