-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run TTS engine service without starting the app. (#553)
- Loading branch information
1 parent
4fbad6a
commit 7ae73e7
Showing
6 changed files
with
134 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...id/SherpaOnnxTtsEngine/app/src/main/java/com/k2fsa/sherpa/onnx/tts/engine/TtsViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.k2fsa.sherpa.onnx.tts.engine | ||
|
||
import android.app.Application | ||
import android.os.FileUtils.ProgressListener | ||
import android.speech.tts.TextToSpeech | ||
import android.speech.tts.TextToSpeech.OnInitListener | ||
import android.speech.tts.UtteranceProgressListener | ||
import android.util.Log | ||
import androidx.lifecycle.ViewModel | ||
import java.util.Locale | ||
|
||
class TtsApp : Application() { | ||
companion object { | ||
lateinit var instance: TtsApp | ||
} | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
instance = this | ||
} | ||
|
||
} | ||
|
||
class TtsViewModel : ViewModel() { | ||
|
||
// https://developer.android.com/reference/kotlin/android/speech/tts/TextToSpeech.OnInitListener | ||
private val onInitListener = object : OnInitListener { | ||
override fun onInit(status: Int) { | ||
when (status) { | ||
TextToSpeech.SUCCESS -> Log.i(TAG, "Init tts succeded") | ||
TextToSpeech.ERROR -> Log.i(TAG, "Init tts failed") | ||
else -> Log.i(TAG, "Unknown status $status") | ||
} | ||
} | ||
} | ||
|
||
// https://developer.android.com/reference/kotlin/android/speech/tts/UtteranceProgressListener | ||
private val utteranceProgressListener = object : UtteranceProgressListener() { | ||
override fun onStart(utteranceId: String?) { | ||
Log.i(TAG, "onStart: $utteranceId") | ||
} | ||
|
||
override fun onStop(utteranceId: String?, interrupted: Boolean) { | ||
Log.i(TAG, "onStop: $utteranceId, $interrupted") | ||
super.onStop(utteranceId, interrupted) | ||
} | ||
|
||
override fun onError(utteranceId: String?, errorCode: Int) { | ||
Log.i(TAG, "onError: $utteranceId, $errorCode") | ||
super.onError(utteranceId, errorCode) | ||
} | ||
|
||
override fun onDone(utteranceId: String?) { | ||
Log.i(TAG, "onDone: $utteranceId") | ||
} | ||
|
||
@Deprecated("Deprecated in Java") | ||
override fun onError(utteranceId: String?) { | ||
Log.i(TAG, "onError: $utteranceId") | ||
} | ||
} | ||
|
||
val tts = TextToSpeech(TtsApp.instance, onInitListener, "com.k2fsa.sherpa.onnx.tts.engine") | ||
|
||
init { | ||
tts.setLanguage(Locale(TtsEngine.lang!!)) | ||
tts.setOnUtteranceProgressListener(utteranceProgressListener) | ||
} | ||
|
||
override fun onCleared() { | ||
super.onCleared() | ||
tts.shutdown() | ||
} | ||
} |