Skip to content

Commit

Permalink
feat: good stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
abdallahmehiz committed Jun 13, 2024
1 parent 915839a commit d072cd0
Show file tree
Hide file tree
Showing 12 changed files with 483 additions and 189 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ dependencies {
implementation(libs.androidx.compose.constraintlayout)
implementation(libs.androidx.material3.icons.extended)
implementation(libs.androidx.documentfile)
implementation(libs.androidx.compose.animation.graphics)
implementation(libs.material)

implementation(libs.aniyomi.mpv.lib)
implementation(libs.aniyomi.ffmpeg.kit)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
android:supportsPictureInPicture="true"
android:resizeableActivity="true"
android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize|keyboardHidden|keyboard"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
android:theme="@style/Theme.MpvKt">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

Expand Down
29 changes: 18 additions & 11 deletions app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.core.view.WindowCompat
import androidx.documentfile.provider.DocumentFile
import `is`.xyz.mpv.MPVLib
import `is`.xyz.mpv.Utils
import kotlinx.coroutines.flow.update
import live.mehiz.mpvkt.databinding.PlayerLayoutBinding
import live.mehiz.mpvkt.preferences.AdvancedPreferences
import live.mehiz.mpvkt.preferences.AudioPreferences
Expand Down Expand Up @@ -91,7 +92,7 @@ class PlayerActivity : AppCompatActivity() {
)

val statisticsPage = advancedPreferences.enabledStatisticsPage.get()
if(statisticsPage != 0) {
if (statisticsPage != 0) {
MPVLib.command(arrayOf("script-binding", "stats/display-stats-toggle"))
MPVLib.command(
arrayOf("script-binding", "stats/display-page-$statisticsPage"),
Expand Down Expand Up @@ -191,14 +192,9 @@ class PlayerActivity : AppCompatActivity() {
// a bunch of observers
internal fun onObserverEvent(property: String, value: Long) {
when (property) {
"time-pos" -> {
viewModel.updatePlayBackPos(value.toFloat())
viewModel.updateChapter(value)
}

"demuxer-cache-time" -> {
viewModel.updateReadAhead(value = value)
}
"time-pos" -> viewModel.updatePlayBackPos(value.toFloat())
"duration" -> viewModel.duration.update { value.toFloat() }
"demuxer-cache-time" -> viewModel.updateReadAhead(value = value)
}
}

Expand All @@ -207,7 +203,14 @@ class PlayerActivity : AppCompatActivity() {
}

internal fun onObserverEvent(property: String, value: Boolean) {

when (property) {
"paused-for-cache" -> {
viewModel.isLoading.update { value }
}
"seeking" -> {
viewModel.isLoading.update { value }
}
}
}

internal fun onObserverEvent(property: String, value: String) {
Expand All @@ -219,12 +222,16 @@ class PlayerActivity : AppCompatActivity() {
MPVLib.mpvEventId.MPV_EVENT_FILE_LOADED -> {
setOrientation()
viewModel.changeVideoAspect(playerPreferences.videoAspect.get())
viewModel.duration = player.duration!!.toFloat()
viewModel.duration.value = player.duration!!.toFloat()
viewModel.loadChapters()
viewModel.loadTracks()
viewModel.getDecoder()
}

MPVLib.mpvEventId.MPV_EVENT_SEEK -> {
viewModel.isLoading.update { true }
}

MPVLib.mpvEventId.MPV_EVENT_END_FILE -> {
onDestroy()
}
Expand Down
71 changes: 41 additions & 30 deletions app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import android.net.Uri
import android.util.DisplayMetrics
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import `is`.xyz.mpv.MPVLib
import `is`.xyz.mpv.MPVView
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import live.mehiz.mpvkt.R
import live.mehiz.mpvkt.preferences.PlayerPreferences
import org.koin.java.KoinJavaComponent.inject
Expand All @@ -22,6 +24,10 @@ class PlayerViewModel(
private val _currentDecoder = MutableStateFlow(getDecoderFromValue(MPVLib.getPropertyString("hwdec")))
val currentDecoder = _currentDecoder.asStateFlow()

var fileName = ""

val isLoading = MutableStateFlow(true)

private val _subtitleTracks = MutableStateFlow<List<Track>>(emptyList())
val subtitleTracks = _subtitleTracks.asStateFlow()
private val _selectedSubtitles = MutableStateFlow(listOf(-1, -1))
Expand All @@ -39,7 +45,7 @@ class PlayerViewModel(
private val _pos = MutableStateFlow(0f)
val pos = _pos.asStateFlow()

var duration: Float = 0f
val duration = MutableStateFlow(0f)

private val _readAhead = MutableStateFlow(0f)
val readAhead = _readAhead.asStateFlow()
Expand All @@ -66,18 +72,18 @@ class PlayerViewModel(
Decoder.HW -> Decoder.SW.value
Decoder.SW -> Decoder.HWPlus.value
Decoder.Auto -> Decoder.SW.value
}
},
)
val newDecoder = activity.player.hwdecActive
if(newDecoder != currentDecoder.value.value) {
if (newDecoder != currentDecoder.value.value) {
_currentDecoder.update { getDecoderFromValue(newDecoder) }
}
}

fun updateDecoder(decoder: Decoder) {
MPVLib.setPropertyString("hwdec", decoder.value)
val newDecoder = activity.player.hwdecActive
if(newDecoder != currentDecoder.value.value) {
if (newDecoder != currentDecoder.value.value) {
_currentDecoder.update { getDecoderFromValue(newDecoder) }
}
}
Expand All @@ -99,25 +105,28 @@ class PlayerViewModel(
}

fun loadTracks() {
val tracksCount = MPVLib.getPropertyInt("track-list/count")!!
val possibleTrackTypes = listOf("video", "audio", "sub")
val vidTracks = mutableListOf<Track>()
val subTracks = mutableListOf<Track>()
val audioTracks = mutableListOf(Track(-1, activity.getString(R.string.player_sheets_tracks_off), null))
for (i in 0..<tracksCount) {
val type = getTrackType(i) ?: continue
if (!possibleTrackTypes.contains(type)) continue
when (type) {
"audio" -> audioTracks.add(Track(getTrackMPVId(i), getTrackTitle(i), getTrackLanguage(i)))
"video" -> vidTracks.add(Track(getTrackMPVId(i), getTrackTitle(i), getTrackLanguage(i)))
"sub" -> subTracks.add(Track(getTrackMPVId(i), getTrackTitle(i), getTrackLanguage(i)))
else -> throw IllegalStateException()
viewModelScope.launch {
fileName = MPVLib.getPropertyString("media-title")
val tracksCount = MPVLib.getPropertyInt("track-list/count")!!
val possibleTrackTypes = listOf("video", "audio", "sub")
val vidTracks = mutableListOf<Track>()
val subTracks = mutableListOf<Track>()
val audioTracks = mutableListOf(Track(-1, activity.getString(R.string.player_sheets_tracks_off), null))
for (i in 0..<tracksCount) {
val type = getTrackType(i) ?: continue
if (!possibleTrackTypes.contains(type)) continue
when (type) {
"sub" -> subTracks.add(Track(getTrackMPVId(i), getTrackTitle(i), getTrackLanguage(i)))
"audio" -> audioTracks.add(Track(getTrackMPVId(i), getTrackTitle(i), getTrackLanguage(i)))
"video" -> vidTracks.add(Track(getTrackMPVId(i), getTrackTitle(i), getTrackLanguage(i)))
else -> throw IllegalStateException()
}
}
_subtitleTracks.update { subTracks }
_selectedSubtitles.update { listOf(activity.player.sid, activity.player.secondarySid) }
_audioTracks.update { audioTracks }
_selectedAudio.update { activity.player.aid }
}
_subtitleTracks.update { subTracks }
_selectedSubtitles.update { listOf(activity.player.sid, activity.player.secondarySid) }
_audioTracks.update { audioTracks }
_selectedAudio.update { activity.player.aid }
}


Expand Down Expand Up @@ -193,7 +202,7 @@ class PlayerViewModel(
}

fun updateReadAhead(value: Long) {
_readAhead.value = pos.value + value.toFloat()
_readAhead.update { value.toFloat() }
}

fun pauseUnpause() {
Expand All @@ -208,15 +217,15 @@ class PlayerViewModel(

fun unpause() {
activity.player.paused = false
_paused.value = false
_paused.update { false }
}

fun showControls() {
_controlsShown.value = true
_controlsShown.update { true }
}

fun hideControls() {
_controlsShown.value = false
_controlsShown.update { false }
activity.windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
}

Expand All @@ -227,33 +236,35 @@ class PlayerViewModel(
}

fun toggleSeekBar() {
_seekBarShown.value = !seekBarShown.value
_seekBarShown.update { !seekBarShown.value }
}

fun hideSeekBar() {
_seekBarShown.value = false
_seekBarShown.update { false }
}

fun showSeekBar() {
_seekBarShown.value = true
_seekBarShown.update { true }
}

fun lockControls() {
_areControlsLocked.value = true
_areControlsLocked.update { true }
}

fun unlockControls() {
_areControlsLocked.value = false
_areControlsLocked.update { false }
}

fun seekBy(offset: Int) {
activity.player.timePos = activity.player.timePos?.plus(offset)
isLoading.update { true }
}

fun seekTo(position: Int) {
if (position < 0) return
if (position > (activity.player.duration ?: 0)) return
activity.player.timePos = position
isLoading.update { true }
}

fun changeBrightnessWithDrag(
Expand Down
Loading

0 comments on commit d072cd0

Please sign in to comment.