Java开发者咨询:如何在Android应用中添加自定义Google语音识别按钮及跨平台问题
Hey there! Totally get you're coming from a Java background and new to Android—no worries at all, let's break down your questions step by step to help you build that hands-free app you're aiming for.
1. Can I add a custom button for Google Speech Recognizer that only listens from the bottom of the screen (not half the screen)?
Absolutely! The default RecognizerIntent that pops up the system's built-in voice input UI is what takes up half the screen, but you can bypass that by using the SpeechRecognizer class directly. This lets you build your own UI (like a small button at the bottom) and control the listening process entirely.
Here's a quick overview of how to do this:
- First, request the necessary permissions in your
AndroidManifest.xml:RECORD_AUDIO(for capturing audio) andINTERNET(since Google's speech recognition requires cloud processing). - Initialize an instance of
SpeechRecognizerusingSpeechRecognizer.createSpeechRecognizer(context). - Set a
RecognitionListeneron it—this callback handles events like when listening starts, when results are returned, or if there's an error. - Attach a click listener to your custom bottom button: when clicked, call
startListening()with aRecognizerIntent(you can configure language preferences here), and callstopListening()when you want to end the session. - Once you get the transcribed text from the
onResults()callback, save it to a text file and pass it to Dialogflow (formerly API.AI) for further processing.
Just a quick note: always check if the device supports speech recognition first using SpeechRecognizer.isRecognitionAvailable(context) to avoid unexpected crashes.
2. Are there other ways to achieve this?
Definitely! If you want more control or need advanced features, consider these alternatives:
- Google Cloud Speech-to-Text API: This cloud-based service lets you send audio streams (or files) directly to Google's servers for transcription. Since it's fully customizable, you can build any UI you want, and it supports longer audio clips, more languages, and advanced features like automatic punctuation. It’s also a great option if you plan to expand to iOS later, as it works across platforms.
- Third-party speech recognition SDKs: Tools like Amazon Transcribe or IBM Watson Speech to Text offer similar custom UI integration and powerful transcription capabilities, if you want to explore alternatives to Google’s services.
3. Does Google Speech Recognizer work on iOS?
The native Android SpeechRecognizer class isn’t available on iOS. However, iOS has its own built-in Speech framework (part of Apple’s developer tools) that handles voice input natively.
If you want to stick with Google’s technology across both platforms, the Google Cloud Speech-to-Text API has an official iOS SDK. This lets you integrate the same cloud service into your iOS app, keeping your transcription logic consistent between Android and iOS—perfect for pairing with Dialogflow.
内容的提问来源于stack exchange,提问作者Pawan Kumar

