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

如果您正在模擬器中運行,它不具備語音識別功能。