Last active
May 16, 2018 23:41
-
-
Save nic0lette/36a0e1a2d6592c232612fea115b68149 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2017 Google Inc. All rights reserved. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example.speechrecognizer.sample | |
import android.Manifest | |
import android.content.Intent | |
import android.content.pm.PackageManager | |
import android.os.Bundle | |
import android.speech.RecognitionListener | |
import android.speech.RecognizerIntent.ACTION_RECOGNIZE_SPEECH | |
import android.speech.RecognizerIntent.EXTRA_LANGUAGE_MODEL | |
import android.speech.RecognizerIntent.LANGUAGE_MODEL_FREE_FORM | |
import android.speech.SpeechRecognizer | |
import android.support.v4.app.ActivityCompat | |
import android.support.v4.content.ContextCompat | |
import android.support.v7.app.AppCompatActivity | |
import android.widget.Toast | |
import kotlinx.android.synthetic.main.activity_main.* | |
/** | |
* NOTE: This activity requires the permission | |
* <uses-permission android:name="android.permission.RECORD_AUDIO"/> | |
* to added to the app's AndroidManifest.xml file. | |
*/ | |
class MainActivity : AppCompatActivity() { | |
private lateinit var speechRecognizer: SpeechRecognizer | |
private val listener = object : RecognitionListener { | |
override fun onReadyForSpeech(params: Bundle?) = Unit | |
override fun onRmsChanged(rmsdB: Float) = Unit | |
override fun onBufferReceived(buffer: ByteArray?) = Unit | |
override fun onPartialResults(partialResults: Bundle?) = Unit | |
override fun onEvent(eventType: Int, params: Bundle?) = Unit | |
override fun onBeginningOfSpeech() = Unit | |
override fun onEndOfSpeech() = Unit | |
override fun onError(error: Int) = Unit | |
/** | |
* This is where the results of the speech to text are received. | |
* | |
* Each entry in the string array extra [SpeechRecognizer.RESULTS_RECOGNITION] | |
* contains a possible interpretation of the speech. In a simple application | |
* such as this one, it's probably okay to just use the first response. In a more complex | |
* app, it may be useful to examine other results. | |
* | |
* [textView] is (unsurprisingly ;) just an [android.widget.TextView]. | |
*/ | |
override fun onResults(results: Bundle?) { | |
val data = results?.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION) ?: return | |
textView.text = data[0] | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this).apply { | |
setRecognitionListener(listener) | |
} | |
/** | |
* [listenButton] is just an [android.widget.Button]. | |
*/ | |
listenButton.setOnClickListener { | |
tryToListen() | |
} | |
} | |
/** | |
* A very simple handler for receiving the results from our permission request. | |
*/ | |
override fun onRequestPermissionsResult(requestCode: Int, | |
permissions: Array<String>, grantResults: IntArray) { | |
if (grantResults.isEmpty()) { | |
// Request was cancelled, so exit. | |
return | |
} | |
when (requestCode) { | |
RECORD_AUDIO_PERM_CHECK -> { | |
// If request is cancelled, the result arrays are empty. | |
when (grantResults[0]) { | |
PackageManager.PERMISSION_GRANTED -> beginListening() | |
else -> permissionDenied() | |
} | |
} | |
} | |
} | |
/** | |
* This method checks if permission was granted to record audio. If it was, then | |
* it immediately begins listening to speech. If it wasn't, it prompts the user to | |
* grant permission. | |
* | |
* See also: https://developer.android.com/training/permissions/requesting | |
*/ | |
private fun tryToListen() { | |
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) | |
!= PackageManager.PERMISSION_GRANTED) { | |
// To keep the sample short, don't worry about showing rational. | |
ActivityCompat.requestPermissions(this, | |
arrayOf(Manifest.permission.RECORD_AUDIO), | |
RECORD_AUDIO_PERM_CHECK) | |
} else { | |
// Permission has already been granted | |
beginListening() | |
} | |
} | |
/** | |
* This uses a simple intent to ask the [SpeechRecognizer] service to listen for speech | |
* and return it to us as an array of strings. | |
* | |
*/ | |
private fun beginListening() { | |
val intent = Intent(ACTION_RECOGNIZE_SPEECH).apply { | |
putExtra(EXTRA_LANGUAGE_MODEL, LANGUAGE_MODEL_FREE_FORM) | |
} | |
speechRecognizer.startListening(intent) | |
} | |
/** | |
* If permission is not granted, just show a toast. In a real application, a better | |
* response is probably needed. ;) | |
*/ | |
private fun permissionDenied() { | |
Toast.makeText(this, "RECORD_AUDIO permission denied", Toast.LENGTH_SHORT).show() | |
} | |
} | |
/** Request code for [MainActivity.tryToListen]. */ | |
private const val RECORD_AUDIO_PERM_CHECK = 4919 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment