我正在尝试使语音识别在我的Android 2.3.3应用程序上正常工作,但是缺少一些基本功能。

首先,在我的AndroidManifest.xml文件的顶部,我有:

package="com.example.myuser.myapp">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

... remainder omitted for brevity

我认为这应该会提示Android询问最终用户,他们是否希望在麦克风启动时向其授予我的应用权限( 如果没有,请更正我! )...

接下来,我有一个“ Activity ,其中包含对我的应用程序的语音识别功能的全部需求。 基本上,我希望应用程序能够检测到有人大声说“ Go ”:

public class SpeechActivity extends AppCompatActivity implements RecognitionListener {

private SpeechRecognizer speechRecognizer;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_speech);

int check = ContextCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO);

if(check != PackageManager.PERMISSION_GRANTED) {

throw new RuntimeException("Ya can't do that now, ya here.");

}

speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);

speechRecognizer.setRecognitionListener(this);

Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test");

speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);

speechRecognizer.startListening(speechIntent);

Log.i("SpeechActivity", "Speech Recognizer is listening");

}

@Override

public void onReadyForSpeech(Bundle bundle) {

}

@Override

public void onBeginningOfSpeech() {

}

@Override

public void onRmsChanged(float v) {

}

@Override

public void onBufferReceived(byte[] bytes) {

}

@Override

public void onEndOfSpeech() {

}

@Override

public void onError(int i) {

}

@Override

public void onResults(Bundle bundle) {

Log.i("SpeechActivity", "Speech was detected...");

String spoken = "";

ArrayList data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

for (int i = 0; i < data.size(); i++) {

spoken += data.get(i);

}

Log.i(this.getClass().getName(), String.format("The word \"%s\" was detected.", spoken));

if(spoken.equalsIgnoreCase("go")) {

doSomething();

}

}

@Override

public void onPartialResults(Bundle bundle) {

}

@Override

public void onEvent(int i, Bundle bundle) {

}

}

通过将手机连接到笔记本电脑(通过USB),单击Android Studio中的“运行”(应用程序)按钮,然后选择手机作为连接的设备,可以在Samsung Galaxy S7 Edge上运行该应用程序。

当应用在手机上启动时,我注意到的第一件事是它不会提示我接受/拒绝应用对使用麦克风的许可。 这对我来说是个预警信号,表明出现了问题。

但是,当我导航到SpeechActivity/activity_speech.xml屏幕时,我得到:

运行时,我得到:

FATAL EXCEPTION: main

Process: com.example.myuser.myapp, PID: 9585

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myuser.myapp/com.example.myuser.myapp.SpeechActivity}: java.lang.RuntimeException: Ya can't do that now, ya here.

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)

at android.app.ActivityThread.-wrap14(ActivityThread.java)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)

at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:154)

at android.app.ActivityThread.main(ActivityThread.java:6688)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

Caused by: java.lang.RuntimeException: Ya can't do that now, ya here.

at com.example.myuser.myapp.SpeechActivity.onCreate(SpeechActivity.java:43)

at android.app.Activity.performCreate(Activity.java:6912)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)

是否可能需要下载并安装插件或APK才能使语音识别正常工作? 为什么我的应用程序不要求使用手机麦克风的许可? 语音识别器是如何开始/收听的,但是似乎根本不接听任何语音?

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐