Skip to content

Commit

Permalink
feat: preferred subtitles and audio tracks languages
Browse files Browse the repository at this point in the history
  • Loading branch information
abdallahmehiz committed Jun 4, 2024
1 parent 10ecf37 commit f1ed68c
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 7 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/live/mehiz/mpvkt/di/PreferencesModule.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package live.mehiz.mpvkt.di

import live.mehiz.mpvkt.preferences.AppearancePreferences
import live.mehiz.mpvkt.preferences.AudioPreferences
import live.mehiz.mpvkt.preferences.DecoderPreferences
import live.mehiz.mpvkt.preferences.PlayerPreferences
import live.mehiz.mpvkt.preferences.SubtitlesPreferences
import live.mehiz.mpvkt.preferences.preference.AndroidPreferenceStore
import live.mehiz.mpvkt.preferences.preference.PreferenceStore
import org.koin.android.ext.koin.androidContext
Expand All @@ -15,4 +17,6 @@ val PreferencesModule = module {
singleOf(::AppearancePreferences)
singleOf(::PlayerPreferences)
singleOf(::DecoderPreferences)
singleOf(::SubtitlesPreferences)
singleOf(::AudioPreferences)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package live.mehiz.mpvkt.preferences

import live.mehiz.mpvkt.preferences.preference.PreferenceStore

class AudioPreferences(preferenceStore: PreferenceStore) {
val preferredLanguages = preferenceStore.getString("audio_preferred_languages")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package live.mehiz.mpvkt.preferences

import live.mehiz.mpvkt.preferences.preference.PreferenceStore

class SubtitlesPreferences(preferenceStore: PreferenceStore) {
val preferredLanguages = preferenceStore.getString("sub_preferred_languages")
}
19 changes: 14 additions & 5 deletions app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import androidx.core.view.WindowCompat
import `is`.xyz.mpv.MPVLib
import `is`.xyz.mpv.Utils
import live.mehiz.mpvkt.databinding.PlayerLayoutBinding
import live.mehiz.mpvkt.preferences.AudioPreferences
import live.mehiz.mpvkt.preferences.DecoderPreferences
import live.mehiz.mpvkt.preferences.PlayerPreferences
import live.mehiz.mpvkt.preferences.SubtitlesPreferences
import live.mehiz.mpvkt.ui.player.controls.PlayerControls
import live.mehiz.mpvkt.ui.theme.MpvKtTheme
import org.koin.android.ext.android.inject
Expand All @@ -30,6 +32,8 @@ class PlayerActivity : AppCompatActivity() {
val audioManager by lazy { getSystemService(Context.AUDIO_SERVICE) as AudioManager }
private val playerPreferences by inject<PlayerPreferences>()
private val decoderPreferences by inject<DecoderPreferences>()
private val audioPreferences by inject<AudioPreferences>()
private val subtitlesPreferences by inject<SubtitlesPreferences>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -38,6 +42,8 @@ class PlayerActivity : AppCompatActivity() {
setContentView(binding.root)

setupMPV()
setupAudio()
setupSubtitles()
val uri = parsePathFromIntent(intent)
val videoUri = if (uri?.startsWith("content://") == true) {
openContentFd(Uri.parse(uri))
Expand Down Expand Up @@ -88,14 +94,17 @@ class PlayerActivity : AppCompatActivity() {
}
if(decoderPreferences.useYUV420P.get()) MPVLib.setPropertyString("vf", "format=yuv420p")

MPVLib.command(arrayOf("script-binding", "stats/display-stats-toggle"))
MPVLib.command(
arrayOf("script-binding", "stats/display-page-1"),
)

player.addObserver(PlayerObserver(this))
}

private fun setupAudio() {
MPVLib.setPropertyString("alang", audioPreferences.preferredLanguages.get())
}

private fun setupSubtitles() {
MPVLib.setPropertyString("slang", subtitlesPreferences.preferredLanguages.get())
}

private fun parsePathFromIntent(intent: Intent): String? {
val filepath: String? = when (intent.action) {
Intent.ACTION_VIEW -> intent.data?.let { resolveUri(it) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package live.mehiz.mpvkt.ui.preferences

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import live.mehiz.mpvkt.R
import live.mehiz.mpvkt.preferences.AudioPreferences
import me.zhanghai.compose.preference.ProvidePreferenceLocals
import me.zhanghai.compose.preference.textFieldPreference
import org.koin.compose.koinInject

object AudioPreferencesScreen : Screen {
@OptIn(ExperimentalMaterial3Api::class)
@Composable
override fun Content() {
val navigator = LocalNavigator.currentOrThrow
val preferences = koinInject<AudioPreferences>()
Scaffold(
topBar = {
TopAppBar(
title = {
Text(stringResource(R.string.pref_audio))
},
navigationIcon = {
IconButton(onClick = { navigator.pop() }) {
Icon(Icons.AutoMirrored.Default.ArrowBack, null)
}
}
)
},
) { padding ->
ProvidePreferenceLocals {
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(padding)
) {
textFieldPreference(
preferences.preferredLanguages.key(),
defaultValue = preferences.preferredLanguages.defaultValue(),
textToValue = { it },
title = { Text(stringResource(R.string.pref_preferred_languages)) },
summary = { if(it.isNotBlank()) Text(it) },
textField = { value, onValueChange, _ ->
Column {
Text(stringResource(`is`.xyz.mpv.R.string.pref_default_audio_language_message))
TextField(
value,
onValueChange,
modifier = Modifier.fillMaxWidth()
)
}
},
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.ArrowBack
import androidx.compose.material.icons.outlined.Audiotrack
import androidx.compose.material.icons.outlined.Memory
import androidx.compose.material.icons.outlined.Palette
import androidx.compose.material.icons.outlined.PlayCircle
import androidx.compose.material.icons.outlined.Subtitles
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
Expand Down Expand Up @@ -58,13 +60,25 @@ object PreferencesScreen : Screen {
key = "player",
title = { Text(text = stringResource(id = R.string.pref_player)) },
icon = { Icon(Icons.Outlined.PlayCircle, null) },
onClick = { navigator.push(PlayerPreferencesScreen) }
onClick = { navigator.push(PlayerPreferencesScreen) },
)
preference(
key = "decoder",
title = { Text(text = stringResource(id = R.string.pref_decoder)) },
icon = { Icon(Icons.Outlined.Memory, null) },
onClick = { navigator.push(DecoderPreferencesScreen) }
onClick = { navigator.push(DecoderPreferencesScreen) },
)
preference(
key = "subtitles",
title = { Text(text = stringResource(id = R.string.pref_subtitles)) },
icon = { Icon(Icons.Outlined.Subtitles, null) },
onClick = { navigator.push(SubtitlesPreferencesScreen) },
)
preference(
key = "audio",
title = { Text(text = stringResource(id = R.string.pref_audio)) },
icon = { Icon(Icons.Outlined.Audiotrack, null) },
onClick = { navigator.push(AudioPreferencesScreen) },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package live.mehiz.mpvkt.ui.preferences

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import live.mehiz.mpvkt.R
import live.mehiz.mpvkt.preferences.SubtitlesPreferences
import me.zhanghai.compose.preference.ProvidePreferenceLocals
import me.zhanghai.compose.preference.textFieldPreference
import org.koin.compose.koinInject

object SubtitlesPreferencesScreen : Screen {
@OptIn(ExperimentalMaterial3Api::class)
@Composable
override fun Content() {
val navigator = LocalNavigator.currentOrThrow
val preferences = koinInject<SubtitlesPreferences>()

Scaffold(
topBar = {
TopAppBar(
title = {
Text(stringResource(R.string.pref_preferred_languages))
},
navigationIcon = {
IconButton(onClick = { navigator.pop() }) {
Icon(Icons.AutoMirrored.Outlined.ArrowBack, null)
}
},
)
},
) { padding ->
ProvidePreferenceLocals {
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(padding),
) {
textFieldPreference(
preferences.preferredLanguages.key(),
defaultValue = preferences.preferredLanguages.defaultValue(),
textToValue = { it },
title = { Text(stringResource(R.string.pref_preferred_languages)) },
summary = { if(it.isNotBlank()) Text(it) },
textField = { value, onValueChange, _ ->
Column {
Text(stringResource(`is`.xyz.mpv.R.string.pref_default_subtitle_language_message))
TextField(
value,
onValueChange,
modifier = Modifier.fillMaxWidth()
)
}
},
)
}
}
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
<string name="pref_decoder_yuv420p_title">Use YUV420P pixel format</string>
<string name="pref_decoder_yuv420p_summary">May fix black screens on some video codecs, can also improve performance at the cost of quality.</string>

<string name="pref_subtitles">Subtitles</string>
<string name="pref_preferred_languages">Preferred languages</string>

<string name="pref_audio">Audio</string>

<string name="player_sheets_add_ext_sub">Add external subtitles</string>
<string name="player_sheets_add_ext_audio">Add external audio tracks</string>
<string name="player_sheets_tracks_off">Off</string>
Expand Down

0 comments on commit f1ed68c

Please sign in to comment.