I haven't been able to find a solution to my issue, and I am sure I am doing something dumb I just can't figure out. I am working through a tutorial for Text To Speech right now, and I can't get the ovveride fun OnDestroy() to work. I get an error saying that it overrides nothing. Not sure what I am doing wrong. The code works fine if I remove that part of the code, but everything I have read online stresses the importance of including a shutdown to tts in the OnDestroy method. Any help is much appreciated.
package com.example.texttospeechdemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.speech.tts.TextToSpeech
import android.util.Log
import android.widget.Toast
import com.example.texttospeechdemo.databinding.ActivityMainBinding
import java.util.*
class MainActivity : AppCompatActivity(), TextToSpeech.OnInitListener {
private var tts: TextToSpeech? = null
private var binding: ActivityMainBinding? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding?.root)
tts = TextToSpeech(this, this)
binding?.btnSpeak?.setOnClickListener{view ->
if(binding?.etTextToSpeak?.text!!.isEmpty()){
Toast.makeText(this@MainActivity,
"Enter a text to speak",
Toast.LENGTH_SHORT
).show()
}else{
speakOut(binding?.etTextToSpeak?.text!!.toString())
}
}
}
override fun onInit(status: Int) {
if(status == TextToSpeech.SUCCESS) {
val result = tts!!.setLanguage(Locale.US)
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED
) {
Log.i("Error", "Language not found or isn't supported")
}
}else{
Log.i("Error", "Initialization Failed")
}
}
private fun speakOut(text: String){
tts!!.speak(text, TextToSpeech.QUEUE_FLUSH, null, "")
}
override fun OnDestroy(){
if(tts != null){
tts?.stop()
tts?.shutdown()
}
super.onDestroy()
binding = null
}
}