Search code examples
androidspeech-recognitionvoice-recognitionspeech-to-text

bind to recognition service failed


i am testing VoiceRecognition.i have searched and didn't find any feasible answer. here is the code.

import android.content.Intent;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, RecognitionListener {

Button button;
SpeechRecognizer speechRecognizer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button);
    speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
    speechRecognizer.setRecognitionListener(this);
    button.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

private static final int SPEECH_REQUEST_CODE = 0;

// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            this.getPackageName());
    speechRecognizer.startListening(intent);

}
@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
        List<String> results = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        String spokenText = results.get(0);
        System.out.println(spokenText);
        // Do something with spokenText
    }
    super.onActivityResult(requestCode, resultCode, data);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button:
            displaySpeechRecognizer();
            break;
    }

}

@Override
public void onReadyForSpeech(Bundle params) {
    System.out.println("onReadyForSpeech");

}

@Override
public void onBeginningOfSpeech() {
    System.out.println("onBeginningOfSpeech");

}

@Override
public void onRmsChanged(float rmsdB) {
    System.out.println("onRmsChanged");

}

@Override
public void onBufferReceived(byte[] buffer) {
    System.out.println("onBufferReceived");

}

@Override
public void onEndOfSpeech() {
    System.out.println("onEndOfSpeech");

}

@Override
public void onError(int error) {
    System.out.println("onError");

}

@Override
public void onResults(Bundle results) {
    System.out.println("onResults");

}

@Override
public void onPartialResults(Bundle partialResults) {
    System.out.println("onPartialResults");

}

@Override
public void onEvent(int eventType, Bundle params) {
    System.out.println("onEvent");

}

}

and this is the layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="whats up"
    android:id="@+id/button"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="94dp" />

</RelativeLayout>

here is the manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.byteshaft.voicerecognition" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

i am getting this error -> "voicerecognition E/SpeechRecognizer﹕ bind to recognition service failed " also cannot find any working example of it.


Solution

  • I just ran across this same issue and, for me, the problem was that I had a version of Cyanogenmod installed on my phone that didn't have Voice Search installed. Here are my steps to resolve it:

    1. Download the APK file from this thread in xda-developers. That thread has a high-level description of how to install that file but the step-by-step is below.
    2. Using a command prompt (windows) or shell window (linux), run the adb application located in your android sdk/platform-tools folder
    3. Ensure it can talk to your device by running the command adb devicesIf your device is not listed, you'll need to make sure your USB drivers for the phone are correct.
    4. Obtain root access through adb. You probably already have it but run adb root
    5. Go into the phone shell by executing adb shell
    6. Find the /dev listing that corresponds to "/system" using the command cat /proc/mounts. You are looking for something like /dev/block/mtdblock4 /system yaffs2 ro, realtime 0 0.
    7. Using the corresponding /dev entry, re-mount the file system into read/write mode with the command (I'm using the example above but the pattern should be obvious) mount -o rw,remount /dev/block/mtdblock4. Hit enter and exit adb shell with the command exit
    8. Copy the apk from your computer to your phone with the push command: adb push C:\VoiceSearch.apk system/app
    9. Go back in to adb shell using adb shell and re-mount the storage in read-only mode (for safety, you could leave this out if you wanted to) with mount -o ro,remount /dev/block/mtdblock4 and exit the shell.

    That's it. No restart needed. You can test this by going to regular Google search and hitting the microphone button to see if it launches.