绑定到识别服务失败。

13

我正在测试语音识别。我已经搜索过了,但是没有找到任何可行的答案。以下是代码。

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");

}

}

这是布局。

<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>

这里是清单

<?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>

我遇到了这个错误 -> "voicerecognition E/SpeechRecognizer﹕ bind to recognition service failed",而且找不到任何可用的示例来解决它。

5个回答

21

从API Level 30开始,需要在AndroidManifest.xml中添加<queries>元素来支持android.speech.RecognitionService

<queries>
    <intent>
        <action android:name="android.speech.RecognitionService" />
    </intent>
</queries>

也许被选中的答案对于提问者来说是正确的,但如果您的识别服务之前正常,突然出现问题,这就是解决方法。 - Giuseppe Garassino

3

如果您在模拟器上运行,则没有语音识别功能。


1

我刚遇到了同样的问题,对我来说,问题在于我手机上安装的Cyanogenmod版本没有安装语音搜索。以下是我的解决步骤:

  1. XDA-Developers中的此讨论串下载APK文件。该讨论串有关于如何安装该文件的高级描述,但步骤如下。
  2. 使用命令提示符(Windows)或Shell窗口(Linux),运行位于Android SDK / platform-tools文件夹中的adb应用程序。
  3. 通过运行命令adb devices确保它可以与您的设备通信。如果您的设备未列出,则需要确保手机的USB驱动程序正确。
  4. 通过adb获得root权限。您可能已经拥有它,但请运行adb root
  5. 通过执行adb shell进入手机shell
  6. 使用命令cat /proc/mounts查找与“/system”对应的/dev列表。您要寻找像/dev/block/mtdblock4 /system yaffs2 ro, realtime 0 0这样的内容。
  7. 使用相应的/dev条目,使用命令(我正在使用上面的示例,但模式应很明显)将文件系统重新挂载为读写模式mount -o rw,remount /dev/block/mtdblock4。输入并使用命令exit退出adb shell
  8. 使用推送命令将apk从计算机复制到手机:adb push C:\VoiceSearch.apk system/app
  9. 使用adb shell重新进入adb shell,并以只读模式重新挂载存储(为了安全起见,如果您想的话可以省略此步骤)mount -o ro,remount /dev/block/mtdblock4并退出shell。

就是这样。无需重新启动。您可以通过转到常规的Google搜索并点击麦克风按钮来测试它是否启动。


0

将语音输入设置中的“增强版谷歌服务”(请参见截图)更改为基本版:

语音输入设置


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接