This sample demonstrates how to recognize speech and intents with Java using the Speech SDK for Android.
Note: this sample is not for the Speech Devices SDK and the Roobo device. If you are looking for further information on these, visit the Speech Devices SDK page.
- A subscription key for the Speech service. See Try the speech service for free.
- A PC (Windows, Linux, Mac) capable to run Android Studio.
- Version 3.1 of Android Studio.
- An ARM-based Android device (API 23: Android 6.0 Marshmallow or higher) enabled for development with a working microphone.
- By building this sample you will download the Microsoft Cognitive Services Speech SDK. By downloading you acknowledge its license, see Speech SDK license agreement.
- Download the sample code to your development PC.
- Open this folder as a project in Android Studio.
- Edit the
MainActivity
source:- Replace the string
YourSubscriptionKey
with your own subscription key. - Replace the string
YourServiceRegion
with the service region of your subscription. For example, replace withwestus
if you are using the 30-day free trial subscription. - If you'd like to use intent recognition powered by the Language Understanding service (LUIS), also do the following edits:
YourLanguageUnderstandingSubscriptionKey
: replace with your Language Understanding service subscription key (endpoint key).YourLanguageUnderstandingServiceRegion
: replace with the region associated with your Language Understanding service subscription.- Replace the string
YourLanguageUnderstandingAppId
with the ID of a Language Understanding service application that you want to recognize intents with. YourLanguageUnderstandingIntentName1
,YourLanguageUnderstandingIntentName2
,YourLanguageUnderstandingIntentName3
: replace with names of intents that your Language Understanding service application recognizes.
- Replace the string
- Press Ctrl+F9, or select Build > Make Project.
- Connect your Android device to your development PC.
- Press Shift+F10, or select Run > Run 'app'.
- In the deployment target windows that comes up, pick your Android device.
- On your Android device, use the buttons in the user interface to pick the different sample scenarios.
Please note that the Speech SDK requires two permissions, i.e., the INTERNET as well as the RECORD_AUDIO permission. As for Android API levels up to 22, requesting the permissions is handled at installation time but from API level 23 users are able to grant/revoke permissions at any time.
Note that this sample targets minSdkVersion 23 or later, so you need to remove the following code if you want to retarget to minSdkVersion 22 or earlier:
// Initialize SpeechSDK and request required permissions.
try {
// a unique number within the application to allow
// correlating permission request responses with the request.
int permissionRequestId = 5;
// Request permissions needed for speech recognition
ActivityCompat.requestPermissions(MainActivity.this, new String[]{RECORD_AUDIO, INTERNET}, permissionRequestId);
}
catch (Exception ex) {
Log.e("SpeechSDK", "could not init sdk, " + ex.toString());
recognizedTextView.setText("Could not initialize: " + ex.toString());
}