مكتبة بسيطة وفعالة توفر وظائف النص إلى كلام لتطبيقات 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() يسلط الضوء على النص في TextView.
Method 5: removeHighlight() يزيل تسليط الضوء من النص في TextView.
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) يعين مستمع OnHighlight.
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.
`