Skip to content

Commit

Permalink
mini tts fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Jul 9, 2023
1 parent 10f323b commit ace55e5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ class ReadActivityViewModel : ViewModel() {
private fun startTTSThread() = ioSafe {
if (ttsThreadMutex.isLocked) return@ioSafe
ttsThreadMutex.withLock {
ttsSession.register(context)
ttsSession.register()

val dIndex = desiredTTSIndex ?: desiredIndex
var innerIndex = 0
Expand Down Expand Up @@ -728,14 +728,27 @@ class ReadActivityViewModel : ViewModel() {

val line = lines[innerIndex]
val nextLine = lines.getOrNull(innerIndex + 1)

// set keys
setKey(
EPUB_CURRENT_POSITION_SCROLL_CHAR,
book.title(),
line.startChar
)
setKey(EPUB_CURRENT_POSITION, book.title(), line.index)

// post visual
_ttsLine.postValue(line)

// wait for next line
val waitFor = ttsSession.speak(line, nextLine)
ttsSession.waitForOr(waitFor, {
currentTTSStatus != TTSHelper.TTSStatus.IsRunning || pendingTTSSkip != 0
}) {
notify()
}

// wait for pause
var isPauseDuration = 0
while (currentTTSStatus == TTSHelper.TTSStatus.IsPaused) {
isPauseDuration++
Expand Down Expand Up @@ -776,7 +789,7 @@ class ReadActivityViewModel : ViewModel() {
TTSHelper.TTSStatus.IsStopped,
context
)
ttsSession.unregister(context)
ttsSession.unregister()
_ttsLine.postValue(null)
}
}
Expand Down Expand Up @@ -896,7 +909,8 @@ class ReadActivityViewModel : ViewModel() {
}

override fun onCleared() {
ttsSession.unregister(context)
ttsSession.release()

super.onCleared()
}

Expand Down
16 changes: 12 additions & 4 deletions app/src/main/java/com/lagradost/quicknovel/TTSHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,25 @@ class TTSSession(val context: Context, event: (TTSHelper.TTSActionType) -> Boole
}


fun register(context: Context?) {
if (context == null || isRegisterd) return
fun register() {
if (isRegisterd) return
isRegisterd = true
context.registerReceiver(myNoisyAudioStreamReceiver, intentFilter)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.requestAudioFocus(focusRequest)
}
}

fun unregister(context: Context?) {
if (context == null || !isRegisterd) return
fun release() {
tts?.stop()
tts?.shutdown()
tts = null

unregister()
}

fun unregister() {
if (!isRegisterd) return
isRegisterd = false
context.unregisterReceiver(myNoisyAudioStreamReceiver)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,30 @@ package com.lagradost.quicknovel.services
import android.app.IntentService
import android.content.Intent
import com.lagradost.quicknovel.ReadActivity
import com.lagradost.quicknovel.ReadActivity2
import com.lagradost.quicknovel.TTSActionType
import com.lagradost.quicknovel.TTSHelper

class TTSPauseService : IntentService("TTSPauseService") {
override fun onHandleIntent(intent: Intent?) {
if (intent != null) {
val id = intent.getIntExtra("id", -1)

when (id) {
TTSHelper.TTSActionType.Pause.ordinal -> {
TTSHelper.TTSActionType.Pause
}
TTSHelper.TTSActionType.Resume.ordinal -> {
TTSHelper.TTSActionType.Resume
}
TTSHelper.TTSActionType.Stop.ordinal -> {
TTSHelper.TTSActionType.Stop
}
else -> null
}?.let { action ->
ReadActivity2.readActivity?.parseAction(action)
}
/*
when (id) {
TTSActionType.Pause.ordinal -> {
ReadActivity.readActivity.isTTSPaused = true
Expand All @@ -19,7 +37,7 @@ class TTSPauseService : IntentService("TTSPauseService") {
TTSActionType.Stop.ordinal -> {
ReadActivity.readActivity.stopTTS()
}
}
}*/
}
}
}

0 comments on commit ace55e5

Please sign in to comment.