BArtWell
BArtWell

Reputation: 4044

Is there a way to voice activation by my own phrase?

I want to create a simple application which will make some tasks by voice commands. I want to start listening for commands with my own phrase like a "Hello device". Is it possible with Android speech recognition API? How to implement activation by my own phrase?

I was searching about this before asking, but couldn't find information about activation. I know about pocket-sphinx, but I need to implement it with Google APIs.

Upvotes: 2

Views: 1317

Answers (3)

Xay
Xay

Reputation: 11

import android.speech.RecognizerIntent;

import android.content.Intent;

import java.util.ArrayList;

import java.util.List;

Step 1: Put this into a method that will start the speech recognizer, you can name anything meaningful like void startSpeech() or something.

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);

Step 2: This will require us to override the onActivityResult() method to work with the above method.

Then start to implement the onActivityResult() method below:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{

String wordStr = null; 

    String[] words = null;
    String firstWord = null;
    String secondWord = null;

    ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

    if(requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK)

    {
        wordStr = matches.get(0);
        words = wordStr.split(" ");
        firstWord = words[0];
        secondWord = words[1];
    }

    if (firstWord.equals("open"))
    {

      // DO SOMETHING HERE
    }

}

Upvotes: 1

Nikolay Shmyrev
Nikolay Shmyrev

Reputation: 25220

CMUSphinx is a real solution for this problem, continuous speech recognition takes too much resources and will drain your battery in an hour while keyword spotting mode is fast enough to detect just a keyphrase.

You might be interested that Google introduced new API in v21 for the similar task:

http://developer.android.com/reference/android/service/voice/AlwaysOnHotwordDetector.html

You could use that, but it will quite seriously restrict your userbase.

Upvotes: 3

Adam Stelmaszczyk
Adam Stelmaszczyk

Reputation: 19837

Send RecognizerIntent.

Here you have a tutorial how to implement voice recognition.

I want to start listening for commands with my own phrase like a "Hello device". Is it possible with Android speech recognition API?

You cannot record a phrase, but you can listen to everything and then ask the recognition engine for words it heard.

Relevant code fragment from the tutorial:

// Populate the wordsList with the String values the recognition engine thought it heard
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

So, you can check if it heard your plain English command, like "Hello device" and if yes, do something.

Upvotes: 1

Related Questions