Yokan / rebuild
Course 1 8624561

ExternalPlayer.android.kt

composeApp/src/androidMain/kotlin/eu/friedcutlet/yokan/services/media/player/ExternalPlayer.android.kt

implementation39 lines
1package eu.friedcutlet.yokan.services.media.player23import android.content.Intent4import android.net.Uri5import eu.friedcutlet.yokan.AndroidAppContext67/** Installed apps that open video streams; ids are package names. */8actual fun externalPlayerChoices(): List<ExternalPlayerChoice> {9    val context = AndroidAppContext.appContext ?: return emptyList()10    val probe = Intent(Intent.ACTION_VIEW).setDataAndType(Uri.parse("https://example.invalid/video.mp4"), "video/*")11    val packageManager = context.packageManager12    return packageManager.queryIntentActivities(probe, 0)13        .map { ExternalPlayerChoice(it.activityInfo.packageName, it.loadLabel(packageManager).toString()) }14        .filter { it.id != context.packageName }15        .distinctBy { it.id }16        .sortedBy { it.label.lowercase() }17}1819actual fun launchExternalPlayer(url: String, headers: Map<String, String>, playerId: String): Boolean {20    val context = AndroidAppContext.appContext ?: return false21    if (url.isBlank()) return false22    return runCatching {23        val intent = Intent(Intent.ACTION_VIEW).apply {24            // MIME hint biases the chooser toward video players (mpv-android,25            // VLC, MX Player, Just Player) instead of browsers.26            setDataAndType(Uri.parse(url), "video/*")27            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)28            if (playerId.isNotBlank()) setPackage(playerId)29            if (headers.isNotEmpty()) {30                // mpv-android / VLC honor android.intent.extra.headers as a31                // string array of alternating name/value pairs.32                val flat = headers.entries.flatMap { listOf(it.key, it.value) }.toTypedArray()33                putExtra("headers", flat)34            }35        }36        context.startActivity(intent)37        true38    }.getOrDefault(false)39}40

Search chapters, reference and source filenames.