3

我正在测试VoiceRecognition.i已搜索并未找到任何可行的答案。 这里是代码。绑定到识别服务失败

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:结合识别服务失败” 也找不到它的任何工作示例。

回答

1

我只是碰到这种同样的问题跑,对我来说,问题是,我有一个版本安装在我的手机上的CyanogenMod是没有安装语音搜索。这里是我的步骤来解决它:

  1. this thread in xda-developers下载APK文件。该线程有关于如何安装该文件的高级描述,但一步一步如下。
  2. 使用命令提示符(Windows)或shell窗口(Linux)上,运行位于您的Android SDK /平台的工具文件夹中的亚行申请
  3. 确保它可以通过如果您的设备运行的命令adb devices谈谈您的设备没有列出,你需要确保你的手机USB驱动程序是正确的。
  4. 通过adb获取root访问权限。你可能已经有了,但运行adb root
  5. 通过执行adb shell
  6. 查找对应使用命令cat /proc/mounts为“/系统”在/ dev上市前往手机外壳。您正在寻找类似/dev/block/mtdblock4 /system yaffs2 ro, realtime 0 0的东西。
  7. 使用相应的/ dev条目,重新安装该文件系统与该命令(我使用上面的例子,但图案应该是显而易见的)mount -o rw,remount /dev/block/mtdblock4读/写模式。击中命令exit
  8. 复制从您的计算机的apk到您的手机与推命令进入和退出亚行外壳:在只读模式adb push C:\VoiceSearch.apk system/app
  9. 回到在亚行外壳采用adb shell并重新安装存储(为了安全起见,如果您愿意,可以将其忽略)与mount -o ro,remount /dev/block/mtdblock4并退出外壳。

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

1

如果您正在模拟器中运行,它不具备语音识别功能。