Skip to content

Commit fcebe7b

Browse files
committed
feat(player): Optimize the display of subtitles in the player's picture-in-picture mode
1 parent f658ae1 commit fcebe7b

File tree

6 files changed

+38
-18
lines changed

6 files changed

+38
-18
lines changed

composeApp/src/commonMain/kotlin/com/jankinwu/fntv/client/data/store/PlayingSettingsStore.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,17 @@ object PlayingSettingsStore {
6868
var playerWindowWidthCompensation: Float
6969
get() = settings.getFloat(scopedKey("player_window_width_compensation"), -40f)
7070
set(value) = settings.set(scopedKey("player_window_width_compensation"), value)
71+
72+
data class PlayerScreenSize(val width: Float, val height: Float)
73+
74+
fun saveLastPlayerScreenSize(width: Float, height: Float) {
75+
settings[scopedKey("last_player_screen_width")] = width
76+
settings[scopedKey("last_player_screen_height")] = height
77+
}
78+
79+
fun getLastPlayerScreenSize(): PlayerScreenSize? {
80+
val width = settings.getFloatOrNull(scopedKey("last_player_screen_width")) ?: return null
81+
val height = settings.getFloatOrNull(scopedKey("last_player_screen_height")) ?: return null
82+
return PlayerScreenSize(width, height)
83+
}
7184
}

composeApp/src/commonMain/kotlin/com/jankinwu/fntv/client/ui/component/player/SubtitleOverlay.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ fun SubtitleOverlay(
4141
maxWidth: Dp,
4242
maxHeight: Dp,
4343
currentPosition: Long,
44-
settings: SubtitleSettings = SubtitleSettings()
44+
settings: SubtitleSettings = SubtitleSettings(),
45+
fontScaleRatio: Float = 1.0f
4546
) {
4647
val screenWidth = maxWidth.value
4748
val screenHeight = maxHeight.value
@@ -303,16 +304,16 @@ fun SubtitleOverlay(
303304
text = cue.text,
304305
style = TextStyle(
305306
color = Color.White,
306-
fontSize = 40.sp * settings.fontScale,
307+
fontSize = 40.sp * settings.fontScale * fontScaleRatio,
307308
fontWeight = FontWeight.Bold,
308309
shadow = Shadow(
309310
color = Color.Black,
310-
offset = Offset(2f, 2f),
311-
blurRadius = 4f
311+
offset = Offset(2f * fontScaleRatio, 2f * fontScaleRatio),
312+
blurRadius = 4f * fontScaleRatio
312313
)
313314
),
314315
textAlign = TextAlign.Center,
315-
modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp)
316+
modifier = Modifier.padding(horizontal = 8.dp * fontScaleRatio, vertical = 4.dp * fontScaleRatio)
316317
)
317318
}
318319
}

composeApp/src/commonMain/kotlin/com/jankinwu/fntv/client/ui/screen/PlayerScreen.kt

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ fun PlayerOverlay(
317317
// Window Aspect Ratio State
318318
var windowAspectRatio by remember { mutableStateOf(PlayingSettingsStore.playerWindowAspectRatio) }
319319

320-
val playerViewModel: PlayerViewModel = koinInject()
320+
val playerViewModel: PlayerViewModel = koinViewModel()
321321
val subtitleSettingsFromVm by playerViewModel.subtitleSettings.collectAsState()
322322

323323
var subtitleSettings by remember {
@@ -363,7 +363,6 @@ fun PlayerOverlay(
363363
// val scope = rememberCoroutineScope()
364364
val mediaPViewModel: MediaPViewModel = koinViewModel()
365365
val tagViewModel: TagViewModel = koinViewModel()
366-
// val playerViewModel: PlayerViewModel = koinViewModel()
367366
val playPlayViewModel: PlayPlayViewModel = koinViewModel()
368367
val episodeListViewModel: EpisodeListViewModel = koinViewModel()
369368
val episodeListState by episodeListViewModel.uiState.collectAsState()
@@ -388,15 +387,6 @@ fun PlayerOverlay(
388387
isSettingsMenuHovered = false
389388
isSubtitleControlHovered = false
390389
playerManager.requestKeyFocus()
391-
392-
// Reset subtitle settings when episode changes
393-
val settings = SubtitleSettings(
394-
fontScale = 1.0f,
395-
verticalPosition = 0.1f,
396-
offsetSeconds = 0.0f
397-
)
398-
subtitleSettings = settings
399-
playerViewModel.updateSubtitleSettings(settings)
400390
}
401391

402392
LaunchedEffect(playingInfoCache?.parentGuid) {
@@ -1095,6 +1085,9 @@ fun PlayerOverlay(
10951085
true
10961086
)
10971087
) {
1088+
LaunchedEffect(maxWidth, maxHeight) {
1089+
PlayingSettingsStore.saveLastPlayerScreenSize(maxWidth.value, maxHeight.value)
1090+
}
10981091
// 视频层 - 从标题栏下方开始显示
10991092
key(surfaceRecreateKey) {
11001093
MediampPlayerSurface(

composeApp/src/commonMain/kotlin/com/jankinwu/fntv/client/viewmodel/BaseViewModel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.jankinwu.fntv.client.viewmodel
22

33
import androidx.lifecycle.ViewModel
44
import kotlinx.coroutines.flow.MutableStateFlow
5+
import org.koin.core.module.dsl.singleOf
56
import org.koin.core.module.dsl.viewModelOf
67
import org.koin.dsl.module
78

@@ -75,5 +76,5 @@ val viewModelModule = module {
7576
viewModelOf (::ProxySettingViewModel)
7677
viewModelOf (::UpdateViewModel)
7778
viewModelOf (::MediaPViewModel)
78-
viewModelOf (::PlayerViewModel)
79+
singleOf (::PlayerViewModel)
7980
}

composeApp/src/commonMain/kotlin/com/jankinwu/fntv/client/viewmodel/PlayerViewModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class PlayerViewModel : ViewModel() {
2222
}
2323

2424
fun updatePlayingInfo(info: PlayingInfoCache?) {
25+
// 当切换视频(guid不同)或清除播放信息时,重置字幕设置
26+
if (info?.itemGuid != _playingInfoCache.value?.itemGuid) {
27+
_subtitleSettings.value = SubtitleSettings()
28+
}
2529
_playingInfoCache.value = info
2630
}
2731

composeApp/src/jvmMain/kotlin/com/jankinwu/fntv/client/ui/window/PipPlayerWindow.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,21 @@ fun PipPlayerWindow(
283283
BoxWithConstraints(
284284
modifier = Modifier.fillMaxSize()
285285
) {
286+
val playerScreenSize = remember { PlayingSettingsStore.getLastPlayerScreenSize() }
287+
val fontScaleRatio = if (playerScreenSize != null && playerScreenSize.width > 0) {
288+
maxWidth.value / playerScreenSize.width
289+
} else {
290+
maxWidth.value / 1280f
291+
}
292+
286293
SubtitleOverlay(
287294
subtitleCues = subtitleCues,
288295
currentRenderTime = currentRenderTime - (subtitleSettings.offsetSeconds * 1000).toLong(),
289296
maxWidth = maxWidth,
290297
maxHeight = maxHeight,
291298
currentPosition = mediaPlayer.getCurrentPositionMillis() - (subtitleSettings.offsetSeconds * 1000).toLong(),
292-
settings = subtitleSettings
299+
settings = subtitleSettings,
300+
fontScaleRatio = fontScaleRatio
293301
)
294302
}
295303
}

0 commit comments

Comments
 (0)