Discuz! BBS

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 193|回复: 0

Android tts programming

[复制链接]

393

主题

556

帖子

3294

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3294
发表于 2025-9-18 13:27:02 | 显示全部楼层 |阅读模式
These are results for android tts programming
Search instead for andoid tts programming
AI Overview
To implement Text-to-Speech (TTS) programming on Android, you need to create a TextToSpeech object, initialize it, and implement TextToSpeech.OnInitListener to ensure the engine is ready before use. Once initialized, you can call the speak() method with your desired text. For advanced control, you can set up callbacks with UtteranceProgressListener to monitor the speech process and trigger actions when it starts or finishes.
1. Initialize the TextToSpeech Object

    Create an instance:
    In your Activity's onCreate() method, create a TextToSpeech object, providing the application context and an instance of TextToSpeech.OnInitListener.

Implement OnInitListener:
Your Activity should implement this interface and override the onInit(int status) method.
Check initialization status:
Inside onInit(), check if the status is TextToSpeech.SUCCESS to confirm the engine is ready.

  1. // Example Kotlin code for initializing TTS
  2. class MainActivity : AppCompatActivity(), TextToSpeech.OnInitListener {

  3.     private lateinit var textToSpeech: TextToSpeech

  4.     override fun onCreate(savedInstanceState: Bundle?) {
  5.         super.onCreate(savedInstanceState)
  6.         setContentView(R.layout.activity_main)

  7.         textToSpeech = TextToSpeech(this, this) // 'this' is the activity, which implements OnInitListener
  8.     }

  9.     override fun onInit(status: Int) {
  10.         if (status == TextToSpeech.SUCCESS) {
  11.             // TTS engine is ready. You can now set the language and speak.
  12.             val result = textToSpeech.setLanguage(Locale.US) // Example: Set language to US English
  13.             if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
  14.                 Log.e("TTS", "Language not supported or missing data")
  15.             } else {
  16.                 // Language set successfully, enable speaking features if needed
  17.             }
  18.         } else {
  19.             Log.e("TTS", "Initialization failed")
  20.         }
  21.     }
  22. }
复制代码


2. Speak Text

    Call speak():
    Once the onInit() method confirms success and you've set a language, you can use the speak() method.

Provide parameters:
The speak() method takes three main parameters:

    text: The string of text to be spoken.

queueMode: A strategy for handling queues of speech requests. Common options are TextToSpeech.QUEUE_FLUSH (clears the queue before speaking) or TextToSpeech.QUEUE_ADD (adds to the end of the queue).
utteranceId: An optional string ID for the specific utterance, used for tracking.
  1. // Example of speaking text
  2. fun speakOut(text: String) {
  3.     if (textToSpeech.isSpeaking) {
  4.         textToSpeech.stop()
  5.     }
  6.     textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, "utteranceId1")
  7. }
复制代码
3. Handle Callbacks (Advanced)

    Set UtteranceProgressListener:
    To get notified when speaking starts, stops, or encounters an error, set an UtteranceProgressListener.
    Implement methods:
    Implement onStart(), onDone(), and onError() within your listener. These methods receive the utteranceId to help identify the specific speech request.
  1. // Example of setting an UtteranceProgressListener
  2. textToSpeech.setOnUtteranceProgressListener(object : UtteranceProgressListener() {
  3.     override fun onDone(utteranceId: String?) {
  4.         Log.i("TTS", "Speech completed for: $utteranceId")
  5.         // You can trigger another action here, like showing an ad
  6.     }

  7.     override fun onError(utteranceId: String?) {
  8.         Log.e("TTS", "Error during speech for: $utteranceId")
  9.     }

  10.     override fun onStart(utteranceId: String?) {
  11.         Log.i("TTS", "Speech started for: $utteranceId")
  12.     }
  13. })
复制代码
ref:  https://proandroiddev.com/master ... cda?gi=73c2c30c4b81
https://medium.com/@vedantkingh/ ... voices-fe4e6ead2368




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-10-25 08:46 , Processed in 0.010265 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表