Skip to content

Commit b39ea37

Browse files
committed
fix(player): Optimize the subtitle switching logic
1 parent 79988d8 commit b39ea37

File tree

1 file changed

+65
-69
lines changed
  • composeApp/src/commonMain/kotlin/com/jankinwu/fntv/client/ui/screen

1 file changed

+65
-69
lines changed

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

Lines changed: 65 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -531,62 +531,62 @@ fun PlayerOverlay(
531531

532532
LaunchedEffect(resetSubtitleState) {
533533
if (resetSubtitleState is UiState.Success) {
534-
val cache = playingInfoCache
535-
val startPos = mediaPlayer.getCurrentPositionMillis()
536-
if (cache != null) {
537-
// Re-fetch play link or use existing one?
538-
// Usually resetSubtitle just changes state on server, we might need to re-request play link or just reuse.
539-
// Assuming we can reuse existing playLink logic but re-evaluate subtitles.
540-
541-
// We need to re-evaluate how to play based on new subtitle selection
542-
val subtitleStream = cache.currentSubtitleStream
543-
val playLink = cache.playLink ?: ""
544-
545-
var extraFiles = MediaExtraFiles()
546-
var actualPlayLink = playLink
547-
var isM3u8 = false
548-
var shouldStartPlayback = true
549-
550-
if (subtitleStream != null) {
551-
extraFiles = getMediaExtraFiles(subtitleStream, playLink)
552-
}
553-
554-
if (playLink.contains(".m3u8")) {
555-
isM3u8 = true
556-
// HLS logic
557-
try {
558-
// Check if it's an internal subtitle
559-
if (subtitleStream != null && subtitleStream.isExternal == 0) {
560-
// Reload HLS subtitle repository to fetch new segments
561-
// hlsSubtitleUtil?.reload() // Removed reload() to avoid re-initialization conflict
562-
// Don't restart playback for internal subtitles
563-
if (cache.previousSubtitle?.isExternal == 0) {
564-
shouldStartPlayback = false
565-
}
566-
}
567-
} catch (e: Exception) {
568-
logger.w("ResetSubtitle: Failed to parse m3u8: ${e.message}")
569-
}
570-
} else if (cache.isUseDirectLink) {
571-
// Direct link logic (usually for external subtitles or non-HLS)
572-
val (link, start) = getDirectPlayLink(
573-
cache.currentVideoStream.mediaGuid,
574-
startPos,
575-
mp4Parser
576-
)
577-
actualPlayLink = link
578-
}
579-
580-
// if (shouldStartPlayback) {
581-
// startPlayback(
582-
// mediaPlayer,
583-
// actualPlayLink,
534+
// val cache = playingInfoCache
535+
// val startPos = mediaPlayer.getCurrentPositionMillis()
536+
// if (cache != null) {
537+
// // Re-fetch play link or use existing one?
538+
// // Usually resetSubtitle just changes state on server, we might need to re-request play link or just reuse.
539+
// // Assuming we can reuse existing playLink logic but re-evaluate subtitles.
540+
//
541+
// // We need to re-evaluate how to play based on new subtitle selection
542+
// val subtitleStream = cache.currentSubtitleStream
543+
// val playLink = cache.playLink ?: ""
544+
//
545+
// var extraFiles = MediaExtraFiles()
546+
// var actualPlayLink = playLink
547+
// var isM3u8 = false
548+
// var shouldStartPlayback = true
549+
//
550+
// if (subtitleStream != null) {
551+
// extraFiles = getMediaExtraFiles(subtitleStream, playLink)
552+
// }
553+
//
554+
// if (playLink.contains(".m3u8")) {
555+
// isM3u8 = true
556+
// // HLS logic
557+
// try {
558+
// // Check if it's an internal subtitle
559+
// if (subtitleStream != null && subtitleStream.isExternal == 0) {
560+
// // Reload HLS subtitle repository to fetch new segments
561+
// // hlsSubtitleUtil?.reload() // Removed reload() to avoid re-initialization conflict
562+
// // Don't restart playback for internal subtitles
563+
// if (cache.previousSubtitle?.isExternal == 0) {
564+
// shouldStartPlayback = false
565+
// }
566+
// }
567+
// } catch (e: Exception) {
568+
// logger.w("ResetSubtitle: Failed to parse m3u8: ${e.message}")
569+
// }
570+
// } else if (cache.isUseDirectLink) {
571+
// // Direct link logic (usually for external subtitles or non-HLS)
572+
// val (link, start) = getDirectPlayLink(
573+
// cache.currentVideoStream.mediaGuid,
584574
// startPos,
585-
// extraFiles,
586-
// isM3u8
575+
// mp4Parser
587576
// )
577+
// actualPlayLink = link
588578
// }
589-
}
579+
//
580+
//// if (shouldStartPlayback) {
581+
//// startPlayback(
582+
//// mediaPlayer,
583+
//// actualPlayLink,
584+
//// startPos,
585+
//// extraFiles,
586+
//// isM3u8
587+
//// )
588+
//// }
589+
// }
590590
mediaPViewModel.clearError()
591591
}
592592
}
@@ -1169,24 +1169,20 @@ fun PlayerOverlay(
11691169
logger.i("切换字幕时调用playRecord失败:缓存为空")
11701170
},
11711171
)
1172-
if (subtitle != null) {
1173-
val request = MediaPRequest(
1174-
req = "media.resetSubtitle",
1175-
reqId = "1234567890ABCDEF",
1176-
playLink = cache.playLink ?: "",
1177-
subtitleIndex = subtitle.index,
1178-
startTimestamp = (mediaPlayer.getCurrentPositionMillis() / 1000).toInt(),
1179-
)
1180-
mediaPViewModel.resetSubtitle(request)
1172+
val subtitleIndex = if (subtitle != null) {
1173+
if (subtitle.isExternal == 1) -1 else subtitle.index
11811174
} else {
1182-
val request = MediaPRequest(
1183-
req = "media.resetSubtitle",
1184-
reqId = "1234567890ABCDEF",
1185-
playLink = cache.playLink ?: "",
1186-
startTimestamp = (mediaPlayer.getCurrentPositionMillis() / 1000).toInt(),
1187-
)
1188-
mediaPViewModel.resetSubtitle(request)
1175+
null
11891176
}
1177+
1178+
val request = MediaPRequest(
1179+
req = "media.resetSubtitle",
1180+
reqId = "1234567890ABCDEF",
1181+
playLink = cache.playLink ?: "",
1182+
subtitleIndex = subtitleIndex,
1183+
startTimestamp = (mediaPlayer.getCurrentPositionMillis() / 1000).toInt(),
1184+
)
1185+
mediaPViewModel.resetSubtitle(request)
11901186
}
11911187
},
11921188
onOpenSubtitleSearch = { showSubtitleSearchDialog = true },

0 commit comments

Comments
 (0)