diff --git a/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt b/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt index 77d0c01..7041a33 100644 --- a/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt +++ b/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt @@ -48,6 +48,8 @@ class PlayerActivity : AppCompatActivity() { private val subtitlesPreferences: SubtitlesPreferences by inject() private val advancedPreferences: AdvancedPreferences by inject() + private lateinit var fileName: String + override fun onCreate(savedInstanceState: Bundle?) { if (playerPreferences.drawOverDisplayCutout.get()) enableEdgeToEdge() super.onCreate(savedInstanceState) @@ -118,9 +120,6 @@ class PlayerActivity : AppCompatActivity() { } player.playbackSpeed = playerPreferences.defaultSpeed.get().toDouble() MPVLib.setPropertyString("keep-open", "yes") - if (playerPreferences.savePositionOnQuit.get()) { - MPVLib.setPropertyString("save-position-on-quit", "yes") - } player.addObserver(PlayerObserver(this)) } @@ -151,7 +150,7 @@ class PlayerActivity : AppCompatActivity() { } private fun setupIntents(intent: Intent) { - viewModel.fileName = intent.getStringExtra("title") ?: "" + intent.getStringExtra("title")?.ifBlank { viewModel.mediaTitle.update { it } } player.timePos = intent.getIntExtra("position", 0) / 1000 } @@ -240,8 +239,12 @@ class PlayerActivity : AppCompatActivity() { internal fun event(eventId: Int) { when (eventId) { MPVLib.mpvEventId.MPV_EVENT_FILE_LOADED -> { + fileName = intent.data!!.lastPathSegment!!.substringAfterLast('/') + viewModel.mediaTitle.update { + MPVLib.getPropertyString("media-title").ifBlank { fileName } + } CoroutineScope(Dispatchers.IO).launch { - reuseVideoPlaybackState(MPVLib.getPropertyString("media-title")) + loadVideoPlaybackState(fileName) if (intent.hasExtra("position")) setupIntents(intent) } setOrientation() @@ -265,7 +268,7 @@ class PlayerActivity : AppCompatActivity() { private suspend fun saveVideoPlaybackState() { mpvKtDatabase.videoDataDao().upsert( PlaybackStateEntity( - MPVLib.getPropertyString("media-title"), + fileName, if (playerPreferences.savePositionOnQuit.get()) player.timePos ?: 0 else 0, player.sid, player.secondarySid, @@ -274,10 +277,10 @@ class PlayerActivity : AppCompatActivity() { ) } - private suspend fun reuseVideoPlaybackState(mediaTitle: String) { + private suspend fun loadVideoPlaybackState(mediaTitle: String) { val state = mpvKtDatabase.videoDataDao().getVideoDataByTitle(mediaTitle) state?.let { - player.timePos = it.lastPosition + player.timePos = if (playerPreferences.savePositionOnQuit.get()) it.lastPosition else 0 player.sid = it.sid player.secondarySid = it.secondarySid player.aid = it.aid diff --git a/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerViewModel.kt b/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerViewModel.kt index fe9d4fb..dc6d825 100644 --- a/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerViewModel.kt +++ b/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerViewModel.kt @@ -26,7 +26,7 @@ class PlayerViewModel( private val _currentDecoder = MutableStateFlow(getDecoderFromValue(MPVLib.getPropertyString("hwdec"))) val currentDecoder = _currentDecoder.asStateFlow() - var fileName = "" + var mediaTitle = MutableStateFlow("") val isLoading = MutableStateFlow(true) @@ -129,7 +129,6 @@ class PlayerViewModel( fun loadTracks() { viewModelScope.launch { - fileName = MPVLib.getPropertyString("media-title") val tracksCount = MPVLib.getPropertyInt("track-list/count")!! val possibleTrackTypes = listOf("video", "audio", "sub") val vidTracks = mutableListOf() diff --git a/app/src/main/java/live/mehiz/mpvkt/ui/player/controls/TopLeftPlayerControls.kt b/app/src/main/java/live/mehiz/mpvkt/ui/player/controls/TopLeftPlayerControls.kt index 8374ad8..ca9b7e1 100644 --- a/app/src/main/java/live/mehiz/mpvkt/ui/player/controls/TopLeftPlayerControls.kt +++ b/app/src/main/java/live/mehiz/mpvkt/ui/player/controls/TopLeftPlayerControls.kt @@ -7,6 +7,8 @@ import androidx.compose.material.icons.automirrored.filled.ArrowBack import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -29,8 +31,9 @@ fun TopLeftPlayerControls( icon = Icons.AutoMirrored.Default.ArrowBack, onClick = { activity.finish() }, ) + val mediaTitle by viewModel.mediaTitle.collectAsState() Text( - viewModel.fileName, + mediaTitle, maxLines = 1, overflow = TextOverflow.Ellipsis, color = Color.White,