@@ -7,10 +7,9 @@ import com.jankinwu.fntv.client.data.model.response.SubtitleStream
77import com.jankinwu.fntv.client.data.store.AccountDataCache
88import io.ktor.client.HttpClient
99import io.ktor.client.request.get
10- import io.ktor.client.request.header
1110import io.ktor.client.statement.bodyAsText
12- import kotlinx.coroutines.delay
1311import kotlinx.coroutines.Dispatchers
12+ import kotlinx.coroutines.delay
1413import kotlinx.coroutines.sync.Mutex
1514import kotlinx.coroutines.sync.withLock
1615import kotlinx.coroutines.withContext
@@ -103,9 +102,11 @@ class HlsSubtitleUtil(
103102 }
104103 }
105104
106- suspend fun initialize () {
105+ suspend fun initialize (startPositionMs : Long ) {
106+ mutex.withLock {
107+ if (isInitialized) return
108+ }
107109 withContext(Dispatchers .IO ) {
108- if (isInitialized) return @withContext
109110 try {
110111 baseUrl = playLink.substringBeforeLast(" /" )
111112 val fullPlayLink = constructFullUrl(playLink)
@@ -127,17 +128,21 @@ class HlsSubtitleUtil(
127128 val playlistContent = fetchWithAuth(subtitlePlaylistUrl)
128129
129130 // 4. Parse segments with duration
130- parseSegments(playlistContent)
131-
132- isInitialized = true
131+ mutex.withLock {
132+ parseSegments(playlistContent)
133+ isInitialized = true
134+ }
133135 logger.i { " Initialized HLS subtitle repository with ${segments.size} segments" }
136+
137+ // 5. Immediately update for the current position
138+ update(startPositionMs)
134139 } catch (e: Exception ) {
135140 logger.e(e) { " Failed to initialize HlsSubtitleRepository" }
136141 }
137142 }
138143 }
139144
140- suspend fun reload () {
145+ suspend fun reload (startPositionMs : Long = 0L ) {
141146 mutex.withLock {
142147 segments.clear()
143148 cues.clear()
@@ -146,7 +151,7 @@ class HlsSubtitleUtil(
146151 lastUpdateCheckTime = 0L
147152 lastProcessedPositionSec = - 1.0
148153 }
149- initialize()
154+ initialize(startPositionMs )
150155 }
151156
152157 suspend fun update (currentPositionMs : Long ) = withContext(Dispatchers .IO ) {
@@ -335,10 +340,10 @@ class HlsSubtitleUtil(
335340
336341 private suspend fun fetchWithAuth (url : String ): String {
337342 return client.get(url) {
338- if (AccountDataCache .cookieState.isNotBlank()) {
339- header(" cookie" , AccountDataCache .cookieState)
340- header(" Authorization" , AccountDataCache .authorization)
341- }
343+ // if (AccountDataCache.cookieState.isNotBlank()) {
344+ // header("cookie", AccountDataCache.cookieState)
345+ // header("Authorization", AccountDataCache.authorization)
346+ // }
342347 }.bodyAsText()
343348 }
344349
0 commit comments