SttDialog

This commit is contained in:
adrienmalin 2018-12-07 15:47:20 +01:00
parent 1454713881
commit 2edc43fd95
2 changed files with 11 additions and 22 deletions

View File

@ -51,15 +51,8 @@ class MatchActivity : AppCompatActivity() {
val player1Name = getStringExtra("player1Name") val player1Name = getStringExtra("player1Name")
val player2Name = getStringExtra("player2Name") val player2Name = getStringExtra("player2Name")
players = listOf( players = listOf(
Player( Player(player1Name, 0, Pattern.compile(getString(R.string.pattern, player1Name))),
getStringExtra("player1Name"), Player(player2Name, 0, Pattern.compile(getString(R.string.pattern, player2Name)))
0,
Pattern.compile(getString(R.string.pattern, player1Name))
), Player(
player2Name,
0,
Pattern.compile(getString(R.string.pattern, player2Name))
)
) )
serviceSide = getIntExtra("starterId", 0) serviceSide = getIntExtra("starterId", 0)
relaunchSide = when(serviceSide) { relaunchSide = when(serviceSide) {

View File

@ -24,6 +24,7 @@ class SttDialog : DialogFragment() {
var sttIntent: Intent? = null var sttIntent: Intent? = null
inner class SttListener : RecognitionListener { inner class SttListener : RecognitionListener {
val ERROR_NOT_UNDERSTOOD = 1
var minRms: Float = 0f var minRms: Float = 0f
var maxRms: Float = 0f var maxRms: Float = 0f
@ -42,25 +43,20 @@ class SttDialog : DialogFragment() {
} }
override fun onResults(data: Bundle) { override fun onResults(data: Bundle) {
var understood = false
data.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)?.let { results -> data.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)?.let { results ->
matchActivity?.apply { matchActivity?.apply {
matchModel?.apply { matchModel?.apply {
for (result in results) { for (result in results) {
for (player in players) { for (player in players) {
if (player.pattern?.matcher(result)?.find() == true) { if (player.pattern?.matcher(result)?.find() == true) {
understood = true
dismiss() dismiss()
updateScore(player) updateScore(player)
updateUI() updateUI()
break return
} }
} }
if (understood) break
}
if (!understood) {
onError(0)
} }
onError(ERROR_NOT_UNDERSTOOD)
} }
} }
} }
@ -79,15 +75,15 @@ class SttDialog : DialogFragment() {
} }
override fun onCreateDialog(savedInstanceState: Bundle?) = AlertDialog.Builder(activity).apply { override fun onCreateDialog(savedInstanceState: Bundle?) = AlertDialog.Builder(activity).apply {
(context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(R.layout.dialog_stt, null). apply { (context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(R.layout.dialog_stt, null).let { view ->
partialResultsTextView = findViewById(R.id.partialResultTextView) partialResultsTextView = view.findViewById(R.id.partialResultTextView)
icStt = findViewById(R.id.icStt) icStt = view.findViewById(R.id.icStt)
setView(this) setView(view)
matchActivity = (activity as MatchActivity).apply { matchActivity = (activity as MatchActivity).apply {
matchModel?.apply { matchModel?.apply {
findViewById<TextView>(R.id.sttHintTextView).text = getString( view.findViewById<TextView>(R.id.sttHintTextView).text = getString(
R.string.STT_hint, R.string.STT_hint,
players[0].name, players[0].name,
players[1].name players[1].name
@ -96,8 +92,8 @@ class SttDialog : DialogFragment() {
putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM) putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().displayLanguage) putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().displayLanguage)
putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10) putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10)
putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, packageName)
putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true) putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true)
putExtra(RecognizerIntent.EXTRA_PREFER_OFFLINE, true)
} }
stt = SpeechRecognizer.createSpeechRecognizer(activity).apply { stt = SpeechRecognizer.createSpeechRecognizer(activity).apply {
setRecognitionListener(SttListener()) setRecognitionListener(SttListener())