这是从语音到文本的基本实现。按下按钮,它会录制声音并将单词显示为文本。
它在android 8上完美运行,并且仍然在运行Oreo的设备上运行,但是当我尝试在使用android 9 Pie的设备上运行它时,应用程序崩溃了。代码是100%一样,我还没有改变。
清单文件的补充:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPermission();
final EditText editText = findViewById(R.id.editText);
final SpeechRecognizer mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
final Intent mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
Locale.getDefault());
mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
@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) {
//getting all the matches
ArrayList<String> matches = bundle
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
//displaying the first match
if (matches != null)
editText.setText(matches.get(0));
}
@Override
public void onPartialResults(Bundle bundle) {
}
@Override
public void onEvent(int i, Bundle bundle) {
}
});
findViewById(R.id.button).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_UP:
mSpeechRecognizer.stopListening();
editText.setHint("You will see input here");
break;
case MotionEvent.ACTION_DOWN:
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
editText.setText("");
editText.setHint("Listening...");
break;
}
return false;
}
});
}
private void checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED)) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + getPackageName()));
startActivity(intent);
finish();
}
}
}
}
我知道发布整个代码文本是不常见的做法,但是我没有其他办法让您知道Android 9可能缺少的内容。
运行日志:
E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.SecurityException: Not allowed to bind to service Intent { act=android.speech.RecognitionService cmp=com.samsung.android.bixby.agent/.mainui.voiceinteraction.MainVoiceInteractionService }
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1766)
at android.app.ContextImpl.bindService(ContextImpl.java:1701)
at android.content.ContextWrapper.bindService(ContextWrapper.java:711)
at android.speech.SpeechRecognizer.startListening(SpeechRecognizer.java:287)
at tk.gandriks.speechtest.MainActivity$2.onTouch(MainActivity.java:107)
at android.view.View.dispatchTouchEvent(View.java:13467)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:697)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1869)
at android.app.Activity.dispatchTouchEvent(Activity.java:3487)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:655)
at android.view.View.dispatchPointerEvent(View.java:13719)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6111)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5889)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5338)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5391)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5357)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5516)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5365)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5573)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5338)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5391)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5357)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5365)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5338)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8400)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8333)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8286)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8515)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:181)
at android.app.ActivityThread.main(ActivityThread.java:7058)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: tk.gandriks.speechtest, PID: 9824
java.lang.SecurityException: Not allowed to bind to service Intent { act=android.speech.RecognitionService cmp=com.samsung.android.bixby.agent/.mainui.voiceinteraction.MainVoiceInteractionService }
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1766)
at android.app.ContextImpl.bindService(ContextImpl.java:1701)
at android.content.ContextWrapper.bindService(ContextWrapper.java:711)
at android.speech.SpeechRecognizer.startListening(SpeechRecognizer.java:287)
at tk.gandriks.speechtest.MainActivity$2.onTouch(MainActivity.java:107)
at android.view.View.dispatchTouchEvent(View.java:13467)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:697)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1869)
at android.app.Activity.dispatchTouchEvent(Activity.java:3487)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:655)
at android.view.View.dispatchPointerEvent(View.java:13719)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6111)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5889)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5338)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5391)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5357)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5516)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5365)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5573)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5338)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5391)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5357)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5365)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5338)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8400)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8333)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8286)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8515)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:181)
at android.app.ActivityThread.main(ActivityThread.java:7058)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
I/Process: Sending signal. PID: 9824 SIG: 9
Application terminated.