gpt4 book ai didi

java - 为什么我的应用程序不显示录制的消息?

转载 作者:行者123 更新时间:2023-12-02 04:37:23 25 4
gpt4 key购买 nike

所以,我正在创建一个人工智能音乐播放器应用程序。一切看起来都很好,但每当我给它一些语音命令时,它都不会显示所需的消息。

我尝试过使用不同的模拟器和不同的 Android 版本。这些都不起作用。

package com.learning.intelligentmusicplayer;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

private RelativeLayout parentRelativeLayout;
private SpeechRecognizer speechRecognizer;
private Intent speechRecogniserIntent;
private String keeper="";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

checkVoiceCommandPermission();
parentRelativeLayout = findViewById(R.id.parentRelativeLayout);

speechRecognizer = SpeechRecognizer.createSpeechRecognizer(MainActivity.this);
speechRecogniserIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechRecogniserIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM );
speechRecogniserIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

speechRecognizer.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) {
ArrayList<String> matchesFound = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if(matchesFound!=null)
{
keeper = matchesFound.get(0);
Toast.makeText(MainActivity.this, "Result = "+keeper, Toast.LENGTH_LONG).show();
}
}

@Override
public void onPartialResults(Bundle bundle) {

}

@Override
public void onEvent(int i, Bundle bundle) {

}
});

parentRelativeLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {

switch(motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
speechRecognizer.startListening(speechRecogniserIntent);
keeper="";
break;
case MotionEvent.ACTION_UP:
speechRecognizer.stopListening();
break;
}
return false;
}
});
}



private void checkVoiceCommandPermission()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if(!(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO)==PackageManager.PERMISSION_GRANTED))
{
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package: "+getPackageName()));
startActivity(intent);
finish();
}
}
}
}

没有错误消息。我听到录制消息的嘟嘟声,但屏幕上没有显示消息。

最佳答案

下载 Google 语音助手解决了我的问题!

关于java - 为什么我的应用程序不显示录制的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56544463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com