This commit is contained in:
adrienmalin 2018-12-06 23:57:45 +01:00
parent 0bfb13f678
commit ff57c29ce1
3 changed files with 31 additions and 35 deletions

View File

@ -131,7 +131,7 @@ class MatchActivity : AppCompatActivity() {
if (matchFinished) {
val (loser, winner) = players.sortedBy { it.score }
if (sttEnabled) {
if (ttsEnabled) {
speakText(
getString(
R.string.victory_speech,

View File

@ -36,7 +36,8 @@ class SttDialog : DialogFragment() {
}
override fun onPartialResults(data: Bundle) {
partialResultsTextView?.text = data.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)[0]
if (!data.isEmpty)
partialResultsTextView?.text = data.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)[0]
}
override fun onResults(data: Bundle) {
@ -77,37 +78,35 @@ class SttDialog : DialogFragment() {
}
override fun onCreateDialog(savedInstanceState: Bundle?) = AlertDialog.Builder(activity).apply {
val view: View = (context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(
R.layout.dialog_stt,
null
)
partialResultsTextView = view.findViewById(R.id.partialResultTextView)
icStt = view.findViewById(R.id.icStt)
(context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(R.layout.dialog_stt, null). apply {
partialResultsTextView = findViewById(R.id.partialResultTextView)
icStt = findViewById(R.id.icStt)
setView(view)
setView(this)
matchActivity = (activity as MatchActivity).apply {
matchModel?.apply {
view.findViewById<TextView>(R.id.sttHintTextView).text = getString(
R.string.STT_hint,
players[0].name,
players[1].name
)
sttIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().displayLanguage)
putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10)
putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, packageName)
putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true)
}
stt = SpeechRecognizer.createSpeechRecognizer(activity).apply {
setRecognitionListener(SttListener())
try {
startListening(sttIntent)
} catch (e: ActivityNotFoundException) {
sttEnabled = false
dismiss()
showText(R.string.STT_unavailable)
matchActivity = (activity as MatchActivity).apply {
matchModel?.apply {
findViewById<TextView>(R.id.sttHintTextView).text = getString(
R.string.STT_hint,
players[0].name,
players[1].name
)
sttIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().displayLanguage)
putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10)
putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, packageName)
putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true)
}
stt = SpeechRecognizer.createSpeechRecognizer(activity).apply {
setRecognitionListener(SttListener())
try {
startListening(sttIntent)
} catch (e: ActivityNotFoundException) {
sttEnabled = false
dismiss()
showText(R.string.STT_unavailable)
}
}
}
}

View File

@ -40,9 +40,6 @@
<string name="STT_hint">Say: \"Point for %s\"\nor \"Point for %s\"</string>
<string name="pattern">(?i:Point for %s)</string>
<string name="not_understood">Can you repeat, please?</string>
<string name="score" translatable="false">%d - %d</string>
<string name="score" translatable="false">%2d - %2d</string>
<string name="STT_disabled">Voice recognition disabled.</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>