Skip to content

Commit

Permalink
feat(player): return position and duration intents
Browse files Browse the repository at this point in the history
mostly for aniyomi but it can work for other players
  • Loading branch information
abdallahmehiz committed Jun 19, 2024
1 parent da79b2b commit 6c1995f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
android:launchMode="singleTask"
android:supportsPictureInPicture="true"
android:resizeableActivity="true"
android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize|keyboardHidden|keyboard"
android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize|keyboardHidden|keyboard|uiMode"
android:theme="@style/Theme.MpvKt">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
Expand Down
26 changes: 25 additions & 1 deletion app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ class PlayerActivity : AppCompatActivity() {
}
}

private fun setupIntents(intent: Intent) {
val title = intent.getStringExtra("title")
if (title?.isNotBlank() == true) viewModel.fileName = intent.getStringExtra("title") ?: ""
player.timePos = intent.getIntExtra("position", 0) / 1000
}

private fun parsePathFromIntent(intent: Intent): String? {
val filepath: String? = when (intent.action) {
Intent.ACTION_VIEW -> intent.data?.let { resolveUri(it) }
Expand Down Expand Up @@ -226,18 +232,36 @@ class PlayerActivity : AppCompatActivity() {
viewModel.loadChapters()
viewModel.loadTracks()
viewModel.getDecoder()
setupIntents(intent)
}

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

MPVLib.mpvEventId.MPV_EVENT_END_FILE -> {
onDestroy()
endPlayback(EndPlaybackReason.PlaybackCompleted)
}
}
}

override fun finish() {
endPlayback(EndPlaybackReason.ExternalAction)
}

private fun endPlayback(reason: EndPlaybackReason) {
if (!intent.getBooleanExtra("return_result", false)) {
super.finish()
return
}
val returnIntent = Intent()
returnIntent.putExtra("end_by", reason.value)
player.timePos?.let { returnIntent.putExtra("position", it * 1000) }
player.duration?.let { returnIntent.putExtra("duration", it * 1000) }
setResult(RESULT_OK, returnIntent)
super.finish()
}

internal fun efEvent(err: String?) {

}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerEnums.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ enum class Sheets {
;
}

enum class EndPlaybackReason(val value: String) {
ExternalAction("external_action"),
PlaybackCompleted("playback_completion"),
Error("error"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.MaterialTheme
Expand Down Expand Up @@ -94,9 +93,8 @@ fun VideoTimer(
)
}

fun Chapter.toSegment(): Segment {
return Segment(this.title ?: "", this.time.toFloat())
}
// Seeker doesn't like the first chapter's time being bigger than 0
fun Chapter.toSegment() = Segment(title ?: time.toString(), if(index != 0) time.toFloat() else 0f)

@Preview
@Composable
Expand Down

0 comments on commit 6c1995f

Please sign in to comment.