Custom STT dialog
This commit is contained in:
		| @ -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") | ||||
|         ) | ||||
|     } | ||||
| } | ||||
|  | ||||
							
								
								
									
										143
									
								
								app/src/main/java/adrienmalin/pingpoints/SttDialog.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										143
									
								
								app/src/main/java/adrienmalin/pingpoints/SttDialog.kt
									
									
									
									
									
										Normal 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() | ||||
|     } | ||||
|  | ||||
| } | ||||
| @ -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) | ||||
|                 } | ||||
|             } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user