Custom STT dialog

This commit is contained in:
adrienmalin 2018-12-06 17:33:23 +01:00
parent 1efae35fea
commit b965302c52
8 changed files with 205 additions and 287 deletions

View File

@ -65,7 +65,7 @@
<PersistentState>
<option name="values">
<map>
<entry key="url" value="jar:file:/C:/Program%20Files/Android/Android%20Studio/plugins/android/lib/android.jar!/images/material_design_icons/social/ic_share_black_24dp.xml" />
<entry key="url" value="jar:file:/C:/Program%20Files/Android/Android%20Studio/plugins/android/lib/android.jar!/images/material_design_icons/hardware/ic_keyboard_voice_black_24dp.xml" />
</map>
</option>
</PersistentState>
@ -76,7 +76,7 @@
<option name="values">
<map>
<entry key="color" value="ffffff" />
<entry key="outputName" value="ic_share" />
<entry key="outputName" value="ic_stt" />
<entry key="sourceFile" value="C:\Users\adima" />
</map>
</option>

View File

@ -4,7 +4,6 @@ import android.arch.lifecycle.ViewModelProviders
import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Bundle
import android.speech.RecognitionListener
import android.speech.RecognizerIntent
import android.speech.SpeechRecognizer
import android.speech.tts.TextToSpeech
@ -13,7 +12,6 @@ import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.support.v7.app.AppCompatDelegate
import android.text.method.LinkMovementMethod
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.ImageView
@ -27,140 +25,23 @@ class MatchActivity : AppCompatActivity() {
var textScore: android.widget.TextView? = null
var textService: android.widget.TextView? = null
var buttons: Array<Button> = emptyArray()
var imageViews: Array<ImageView?> = emptyArray()
var imageViews: Array<ImageView> = emptyArray()
var tts: TextToSpeech? = null
var stt: SpeechRecognizer? = null
var sttIntent: Intent? = null
inner class WaitForTtsInit : TextToSpeech.OnInitListener {
override fun onInit(status: Int) {
updateUI()
matchModel?.apply{
if (sttEnabled) {
speakText(
getString(
R.string.STT_hint,
players[0].name,
players[1].name
),
TextToSpeech.QUEUE_ADD
)
}
}
}
}
inner class WaitForTtsSpeak : UtteranceProgressListener() {
inner class SttAfterTts : UtteranceProgressListener() {
override fun onDone(id: String) {
launchStt()
SttDialog().show( supportFragmentManager, "SttDialog")
}
override fun onStart(id: String) {}
override fun onError(id: String) {}
}
inner class SttListener : RecognitionListener {
val LOG_TAG: String = "SttListener"
override fun onBeginningOfSpeech() {
Log.i(LOG_TAG, "onBeginningOfSpeech")
}
override fun onBufferReceived(buffer: ByteArray?) {
Log.i(LOG_TAG, "onBufferReceived: $buffer");
}
override fun onEndOfSpeech() {
Log.i(LOG_TAG, "onEndOfSpeech")
}
override fun onError(errorCode: Int) {
val errorMessage: String = when(errorCode) {
SpeechRecognizer.ERROR_AUDIO -> "Audio recording error"
SpeechRecognizer.ERROR_CLIENT -> "Client side error"
SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS -> "Insufficient permissions"
SpeechRecognizer.ERROR_NETWORK -> "Network error"
SpeechRecognizer.ERROR_NETWORK_TIMEOUT -> "Network timeout"
SpeechRecognizer.ERROR_NO_MATCH -> "No match"
SpeechRecognizer.ERROR_RECOGNIZER_BUSY -> "RecognitionService busy"
SpeechRecognizer.ERROR_SERVER -> "error from server"
SpeechRecognizer.ERROR_SPEECH_TIMEOUT -> "No speech input"
else -> "Didn't understand, please try again."
}
Log.d(LOG_TAG, "FAILED $errorMessage")
launchStt()
}
override fun onEvent(arg0: Int, arg1: Bundle?) {
Log.i(LOG_TAG, "onEvent")
}
override fun onPartialResults(data: Bundle?) {
//Log.i(LOG_TAG, "onPartialResults")
}
override fun onReadyForSpeech(arg0: Bundle?) {
Log.i(LOG_TAG, "onReadyForSpeech")
}
override fun onResults(data: Bundle) {
Log.i(LOG_TAG, "onResults");
val results:ArrayList<String> = data.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
var understood = false
matchModel?.apply {
for (result in results) {
for (player in players) {
if (player.pattern?.matcher(result)?.find() == true) {
understood = true
updateScore(player)
updateUI()
break
}
}
if (understood) break
}
if (!understood) {
if (ttsEnabled) {
speakText(getString(R.string.not_understood))
}
else {
showText(R.string.not_understood)
}
}
}
launchStt()
}
override fun onRmsChanged(rmsdB: Float) {
//Log.i(LOG_TAG, "onRmsChanged: $rmsdB")
}
}
fun showText(text: String, duration: Int = Snackbar.LENGTH_SHORT) {
Snackbar.make(
findViewById(R.id.coordinatorLayout),
text,
duration
).show()
}
fun showText(textId: Int, duration: Int = Snackbar.LENGTH_SHORT) {
Snackbar.make(
findViewById(R.id.coordinatorLayout),
textId,
duration
).show()
}
fun speakText(text: String, queueMode: Int = TextToSpeech.QUEUE_FLUSH) {
//stt?.stopListening()
tts?.speak(
text,
queueMode,
hashMapOf(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID to "TTS")
)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
@ -201,53 +82,14 @@ class MatchActivity : AppCompatActivity() {
}
if (ttsEnabled) {
tts = TextToSpeech(this@MatchActivity, WaitForTtsInit())
}
if (sttEnabled) {
stt = SpeechRecognizer.createSpeechRecognizer(this@MatchActivity).apply {
setRecognitionListener(SttListener())
}
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, this@MatchActivity.packageName)
}
if (ttsEnabled) {
tts?.setOnUtteranceProgressListener(WaitForTtsSpeak())
} else {
matchModel?.apply {
showText(
getString(
R.string.STT_hint,
players[0].name,
players[1].name
),
Snackbar.LENGTH_LONG
)
}
launchStt()
}
} else {
showText(R.string.button_hint)
if (sttEnabled)
tts?.setOnUtteranceProgressListener(SttAfterTts())
}
}
}
updateUI()
}
fun launchStt() {
matchModel?.apply {
if (sttEnabled and !matchFinished) {
try {
stt?.startListening(sttIntent)
} catch (e: ActivityNotFoundException) {
sttEnabled = false
showText(R.string.STT_unavailable)
}
}
}
}
override fun onBackPressed() {
if (matchModel?.pointId == 0)
super.onBackPressed()
@ -316,6 +158,9 @@ class MatchActivity : AppCompatActivity() {
if (matchPoint)
scoreSpeech += getString(R.string.match_point)
speakText(scoreSpeech)
} else {
if (sttEnabled)
SttDialog().show(supportFragmentManager, "SttDialog")
}
}
}
@ -334,9 +179,27 @@ class MatchActivity : AppCompatActivity() {
}
}
override fun onStop() {
super.onStop()
tts?.shutdown()
stt?.destroy()
fun showText(text: String, duration: Int = Snackbar.LENGTH_SHORT) {
Snackbar.make(
findViewById(R.id.coordinatorLayout),
text,
duration
).show()
}
fun showText(textId: Int, duration: Int = Snackbar.LENGTH_SHORT) {
Snackbar.make(
findViewById(R.id.coordinatorLayout),
textId,
duration
).show()
}
fun speakText(text: String, queueMode: Int = TextToSpeech.QUEUE_FLUSH) {
tts?.speak(
text,
queueMode,
hashMapOf(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID to "TTS")
)
}
}

View File

@ -0,0 +1,143 @@
package adrienmalin.pingpoints
import android.app.AlertDialog
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.speech.RecognitionListener
import android.speech.RecognizerIntent
import android.speech.SpeechRecognizer
import android.support.v4.app.DialogFragment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import java.util.*
class SttDialog : DialogFragment() {
var partialResultsTextView: TextView? = null
var matchActivity: MatchActivity? = null
var stt: SpeechRecognizer? = null
var sttIntent: Intent? = null
inner class SttListener : RecognitionListener {
val LOG_TAG: String = "SttListener"
override fun onBeginningOfSpeech() {
Log.i(LOG_TAG, "onBeginningOfSpeech")
}
override fun onBufferReceived(buffer: ByteArray?) {
Log.i(LOG_TAG, "onBufferReceived: $buffer");
}
override fun onEndOfSpeech() {
Log.i(LOG_TAG, "onEndOfSpeech")
}
override fun onError(errorCode: Int) {
val errorMessage: String = when (errorCode) {
SpeechRecognizer.ERROR_AUDIO -> "Audio recording error"
SpeechRecognizer.ERROR_CLIENT -> "Client side error"
SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS -> "Insufficient permissions"
SpeechRecognizer.ERROR_NETWORK -> "Network error"
SpeechRecognizer.ERROR_NETWORK_TIMEOUT -> "Network timeout"
SpeechRecognizer.ERROR_NO_MATCH -> "No match"
SpeechRecognizer.ERROR_RECOGNIZER_BUSY -> "RecognitionService busy"
SpeechRecognizer.ERROR_SERVER -> "error from server"
SpeechRecognizer.ERROR_SPEECH_TIMEOUT -> "No speech input"
else -> "Didn't understand, please try again."
}
Log.d(LOG_TAG, "FAILED $errorMessage")
stt?.startListening(sttIntent)
}
override fun onEvent(arg0: Int, arg1: Bundle?) {
Log.i(LOG_TAG, "onEvent")
}
override fun onPartialResults(data: Bundle) {
//Log.i(LOG_TAG, "onPartialResults")
partialResultsTextView?.text = data.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)[0]
}
override fun onReadyForSpeech(arg0: Bundle?) {
Log.i(LOG_TAG, "onReadyForSpeech")
}
override fun onResults(data: Bundle) {
Log.i(LOG_TAG, "onResults");
val results: ArrayList<String> = data.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
var understood = false
matchActivity?.apply {
matchModel?.apply {
for (result in results) {
for (player in players) {
if (player.pattern?.matcher(result)?.find() == true) {
understood = true
dismiss()
updateScore(player)
updateUI()
break
}
}
if (understood) break
}
if (!understood) {
partialResultsTextView?.text = getString(R.string.not_understood)
stt?.startListening(sttIntent)
}
}
}
}
override fun onRmsChanged(rmsdB: Float) {
//Log.i(LOG_TAG, "onRmsChanged: $rmsdB")
}
}
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)
setView(view)
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)
}
}
}
}
}.create()!!
override fun onStop() {
super.onStop()
stt?.destroy()
}
}

View File

@ -1,102 +0,0 @@
package adrienmalin.pingpoints
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Activities that contain this fragment must implement the
* [SttFragment.OnFragmentInteractionListener] interface
* to handle interaction events.
* Use the [SttFragment.newInstance] factory method to
* create an instance of this fragment.
*
*/
class SttFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
private var listener: OnFragmentInteractionListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_stt, container, false)
}
// TODO: Rename method, update argument and hook method into UI event
fun onButtonPressed(uri: Uri) {
listener?.onFragmentInteraction(uri)
}
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnFragmentInteractionListener) {
listener = context
} else {
throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
}
}
override fun onDetach() {
super.onDetach()
listener = null
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
*
*
* See the Android Training lesson [Communicating with Other Fragments]
* (http://developer.android.com/training/basics/fragments/communicating.html)
* for more information.
*/
interface OnFragmentInteractionListener {
// TODO: Update argument type and name
fun onFragmentInteraction(uri: Uri)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment SttFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
SttFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SttDialog">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
app:srcCompat="@drawable/ic_stt"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_margin="8dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/STT_hint" android:id="@+id/sttHintTextView" android:layout_margin="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/partialResultTextView"
android:layout_margin="8dp" android:gravity="center"/>
</LinearLayout>
</FrameLayout>

View File

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SttFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"/>
</FrameLayout>

View File

@ -32,7 +32,7 @@
<string name="share_subject">Match Ping Points : %s contre %s</string>
<string name="share_message">"%s contre %s:\n%s a gagné par %d à %d\nPing Points est disponible sur Google Play\n "</string>
<string name="match_point">Balle de match</string>
<string name="STT_hint">Dîtes : \"Point pour %s\" ou \"Point pour %s\"</string>
<string name="STT_hint">Dîtes : \"Point pour %s\"\nou \"Point pour %s\"</string>
<string name="pattern">(?i:Point pour %s)</string>
<string name="not_understood">Pouvez-vous répéter ?</string>
<string name="STT_disabled">Reconnaissance vocale désactivée.</string>

View File

@ -37,7 +37,7 @@
<string name="share_subject">Ping Points Match: %s vs. %s</string>
<string name="share_message">%s vs. %s:\n%s won by %d to %d\nGet Ping Points on Google Play</string>
<string name="match_point">Match point</string>
<string name="STT_hint">Say: \"Point for %s\" or \"Point for %s\"</string>
<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>