5

I'm trying to build an voice controlled application which can perform some tasks depending on the commands.
I wanted to add Google Now features also to it so that if the user asks some questions like about weather info, news, about celebrities etc then I can get results from Google Now.

Is there any way to integrate Google now functionality in my app?

Kara
  • 6,115
  • 16
  • 50
  • 57
Shashi
  • 51
  • 1
  • 2

2 Answers2

2

Check out the Voice Reorganization in Android

You can implement it as below :

Write the below code on click event of button which is responsible for firing off the voice intent.

/**
 * Instruct the app to listen for user speech input
 */
private void listenToSpeech() {
    //start the speech recognition intent passing required data
    Intent listenIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //indicate package
    listenIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
    //message to display while listening
    listenIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say a word!");
    //set speech model
    listenIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    //specify number of results to retrieve
    listenIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
    //start listening
    startActivityForResult(listenIntent, VR_REQUEST);
}

When the intent calls back, we display the transcribed voice.

/**
 * onActivityResults handles:
 *  - retrieving results of speech recognition listening
 *  - retrieving result of TTS data check
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //check speech recognition result
    if (requestCode == VR_REQUEST && resultCode == RESULT_OK)
    {
        //store the returned word list as an ArrayList
        ArrayList<String> suggestedWords = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        //set the retrieved list to display in the ListView using an ArrayAdapter
        wordList.setAdapter(new ArrayAdapter<String> (this, R.layout.word, suggestedWords));

     //to open the result in browser 
     Intent intent = new Intent(Intent.ACTION_VIEW, 
     Uri.parse("https://www.google.co.in/?gws_rd=cr#q="+suggestedWords));
startActivity(intent);
    }
    //tss code here
    //call superclass method
    super.onActivityResult(requestCode, resultCode, data);
}
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Thank you GrIsHu for your suggestion. According to this logic I can see I'll be able to get the result in the browser, but could you please explain how to use TTS for the most accurate result. For example if I ask How long is Eiffel tower, I'll get the result in the browser but how can I select the result on which I use TTS for voice response, so that the result can be audible. – Shashi Nov 22 '13 at 09:48
  • What are trying to say i am not getting you? – GrIsHu Nov 22 '13 at 10:03
  • The way you click on google search voice icon on Android device and ask any question, most of the times it displays the result on the screen as well as tells the answer through audio(using text to speech). I want to know how to do that, I'm basically not interested in visual results, rather I need the results to be spoken by the device in the similar way it happens in Google Now. Let me know if I'm still not clear enough. – Shashi Nov 22 '13 at 10:24
  • The functionality which you want is not possible using the TTS. Because TTS only translates the things which you speak it does not provide and result. For more details check http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/ – GrIsHu Nov 22 '13 at 10:29
  • As far as I know TTS translates input text to Speech. I want to translate result obtained from google search to speech, so that user can hear it. For example if I ask the app using voice recognition "what is the height of Eiffel tower", it should respond by saying "Height of Eiffel tower is 324m" or something similar. I want the most accurate search result to be converted to speech using TTS. I don't want any results from TTS. Or do you know any other better way to achieve this? – Shashi Nov 22 '13 at 10:51
  • I follow the example and I wasn't able to understand what should be the value of VR_REQUEST; or what actually represents. Can you explain me a bit? thank you – Ispas Claudiu Jul 23 '14 at 09:37
0

So far the closest thing I have found to your requirements is

RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE

This basically launches the Google Now screen on top of your app and responds back with the voice feedback as Google Now does.

I haven't found any way yet to listen in background and get the speech result or text result which can be converted into speech by TTS engine.

Amit
  • 3,422
  • 3
  • 28
  • 39