feat: adjustable subtitle position

close #426
This commit is contained in:
edde746
2026-02-09 21:22:19 +01:00
parent 4d50c778ad
commit 2de92e660d
25 changed files with 129 additions and 23 deletions
@@ -858,7 +858,8 @@ class ExoPlayerCore(private val activity: Activity) : Player.Listener {
borderSize: Float,
borderColor: String,
bgColor: String,
bgOpacity: Int
bgOpacity: Int,
subtitlePosition: Int = 100
) {
activity.runOnUiThread {
// 1. Non-ASS subtitles: CaptionStyleCompat on SubtitleView
@@ -885,6 +886,25 @@ class ExoPlayerCore(private val activity: Activity) : Player.Listener {
val fraction = fontSize / 720f
subtitleView?.setFractionalTextSize(fraction)
// Subtitle position: adjust gravity and bottom padding
val clampedPosition = subtitlePosition.coerceIn(0, 100)
val gravity = when {
clampedPosition <= 33 -> Gravity.TOP
clampedPosition <= 66 -> Gravity.CENTER
else -> Gravity.BOTTOM
}
(subtitleView?.layoutParams as? FrameLayout.LayoutParams)?.let { params ->
params.gravity = gravity or Gravity.CENTER_HORIZONTAL
subtitleView?.layoutParams = params
}
// Fine-grained positioning within bottom region via bottom padding fraction
if (clampedPosition > 66) {
val bottomFraction = (100 - clampedPosition) / 100f
subtitleView?.setBottomPaddingFraction(bottomFraction)
} else {
subtitleView?.setBottomPaddingFraction(0f)
}
// 2. ASS subtitles: font scale via libass
// MPV default sub-font-size is 38
val defaultSize = 38f
@@ -895,7 +915,7 @@ class ExoPlayerCore(private val activity: Activity) : Player.Listener {
Log.w(TAG, "Failed to set ASS font scale: ${e.message}")
}
Log.d(TAG, "setSubtitleStyle: fontSize=$fontSize, textColor=$textColor, borderSize=$borderSize, bgOpacity=$bgOpacity, assScale=$scale")
Log.d(TAG, "setSubtitleStyle: fontSize=$fontSize, textColor=$textColor, borderSize=$borderSize, bgOpacity=$bgOpacity, position=$subtitlePosition, assScale=$scale")
}
}
@@ -419,6 +419,7 @@ class ExoPlayerPlugin : FlutterPlugin, MethodChannel.MethodCallHandler,
val borderColor = call.argument<String>("borderColor") ?: "#000000"
val bgColor = call.argument<String>("bgColor") ?: "#000000"
val bgOpacity = call.argument<Number>("bgOpacity")?.toInt() ?: 0
val subtitlePosition = call.argument<Number>("subtitlePosition")?.toInt() ?: 100
if (usingMpvFallback) {
// MPV fallback handles styling via setProperty, no-op here
@@ -426,7 +427,7 @@ class ExoPlayerPlugin : FlutterPlugin, MethodChannel.MethodCallHandler,
return
}
playerCore?.setSubtitleStyle(fontSize, textColor, borderSize, borderColor, bgColor, bgOpacity)
playerCore?.setSubtitleStyle(fontSize, textColor, borderSize, borderColor, bgColor, bgOpacity, subtitlePosition)
result.success(null)
}