一個簡單有效的庫,為Android應用程序提供文本到語音功能。
使用Gradle將庫添加到您的項目中:
allprojects {
repositories {
maven { url ' https://jitpack.io ' }
}
}dependencies {
implementation ' com.github.aiyu-ayaan:tts-engine:Tag '
}使用庫簡單而簡單。這是如何在代碼中使用它的示例:
TextToSpeechHelper
.getInstance(activity)
.registerLifecycle(owner)
.speak(message)
.highlight()
.onHighlight { pair ->
Log .d( TAG , " speak: ${pair.first} - ${pair.second} " )
}
.onDone {
Log .d( TAG , " speak: done " )
}
.onError {
Log .d( TAG , " speak: $it " )
} activity是將用於初始化TTS發動機的活動。
owner是生命週期所有者,將用於註冊TTS發動機生命週期觀察者並可以處理生命週期事件。
message是將要說的文字。
highlight()是一種可選方法,它將突出文本中的口語。
onHighlight()是一種可選方法,當說單詞時將被調用。
onDone()是一種說文本時將調用的方法。
onError()是發生錯誤時將調用的方法。
MainActivity.kt
class MainActivity : ComponentActivity () {
override fun onCreate ( savedInstanceState : Bundle ? ) {
super .onCreate(savedInstanceState)
setContent {
TTSLibraryTheme {
val viewModel = MainViewModel ()
val resource = stringResource(id = R .string.des)
val text = remember { resource }
// screen
Surface (
modifier = Modifier .fillMaxSize(),
color = MaterialTheme .colorScheme.background
) {
Screen (
state = text,
viewModel = viewModel,
) {
speak(
this ,
this as LifecycleOwner ,
text,
viewModel
)
}
}
}
}
}
}
@Composable
fun Screen (
modifier : Modifier = Modifier ,
viewModel : MainViewModel ,
state : String ,
onButtonClick : () -> Unit = {},
) {
Column (
modifier = modifier
.fillMaxSize()
.padding( 16 .dp),
verticalArrangement = Arrangement . Center ,
horizontalAlignment = Alignment . CenterHorizontally
) {
val s = viewModel.getState().value
TTSText (
textAlign = TextAlign . Center ,
textHighlightBuilder = TextHighlightBuilder (
text = state,
s
),
)
Spacer (modifier = Modifier .padding( 16 .dp))
Button (onClick = onButtonClick) {
Text (text = stringResource(id = R .string.speak))
}
}
}
private fun speak (
activity : Activity ,
owner : LifecycleOwner ,
message : String ,
viewModel : MainViewModel
) {
TextToSpeechHelper
.getInstance(activity)
.registerLifecycle(owner)
.speak(message)
.highlight()
.onHighlight { pair ->
viewModel.updateState(pair)
}
.onDone {
Log .d( TAG , " speak: done " )
}
.onError {
Log .d( TAG , " speak: $it " )
}
}
MainViewModel.kt
class MainViewModel : ViewModel () {
private val state = mutableStateOf( Pair ( 0 , 0 ))
fun updateState ( pair : Pair < Int , Int >) {
state.value = pair
}
fun getState (): State < Pair < Int , Int >> = state
} class MainActivity : AppCompatActivity () {
override fun onCreate ( savedInstanceState : Bundle ? ) {
super .onCreate(savedInstanceState)
setContentView( R .layout.activity_main)
val textView = findViewById< TextView >( R .id.text_view)
val button = findViewById< Button >( R .id.button)
val message = getString( R .string.des)
button.setOnClickListener {
speak(
this ,
this as LifecycleOwner ,
message,
textView
)
}
}
private fun speak (
activity : Activity ,
owner : LifecycleOwner ,
message : String ,
textView : TextView
) {
TextToSpeechHelper
.getInstance(activity)
.registerLifecycle(owner)
.speak(message)
.highlight()
.onHighlight { pair ->
textView.highlightText(pair.first, pair.second)
}
.onDone {
Log .d( TAG , " speak: done " )
}
.onError {
Log .d( TAG , " speak: $it " )
}
}
}TextView.highlightText()是一種擴展方法,它突出了文本中的口語。 Method 1: registerLifecycle(owner: LifecycleOwner)記錄給定所有者的生命週期。
Method 2: initTTS()初始化文本到語音引擎。
Method 3: speak(message: String)從給定消息生成語音。
Method 4: highlight()突出顯示文本視圖中的文本。
Method 5: removeHighlight()從文本視圖中的文本中刪除了亮點。
Method 6: destroy(action: (() -> Unit))破壞文本對語音引擎並刪除所有聽眾。
Method 7: onStart(onStartListener: () -> Unit)設置onstart偵聽器。
Method 8: onDone(onCompleteListener: () -> Unit)設置了ondone偵聽器。
Method 9: onError(onErrorListener: (String) -> Unit)設置了OnError偵聽器。
Method 10: onHighlight(onHighlightListener: (Pair<Int, Int>) -> Unit)設置了on -ighlight偵聽器。
Method 11: setCustomActionForDestroy(action: () -> Unit)設置了在破壞文本到語音發動機時要執行的自定義操作。
Method 12: setLanguage(locale: Locale)設置文本到語音引擎的語言。
Method 13: setPitchAndSpeed(pitch: Float, speed: Float)設置了文本到語音引擎產生的語音的音調和速度。
Method 14: resetPitchAndSpeed()文本到語音引擎產生的語音的音調和速度重置為默認值。
如果您想為圖書館做出貢獻,請提供存儲庫並創建拉動請求。
MIT License
Copyright (c) 2023 Ayaan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`