android不用监听器,没有调用Android监听器语音识别

我正在尝试使用SpeechRecognizer类在Android上进行语音识别,并将其影响到RecognitionListener .

问题:没有调用受影响的监听器 .

这是我的主要活动的代码:

package com.example.testvoicepaper;

import java.util.ArrayList;

import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.speech.RecognitionListener;

import android.speech.RecognizerIntent;

import android.speech.SpeechRecognizer;

import android.util.Log;

import android.view.KeyEvent;

import android.view.Menu;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnKeyListener;

import android.view.View.OnTouchListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.TextView;

public class MainActivity extends Activity {

public int VOICE_RECOGNITION_REQUEST_CODE = 1;

private Button launchRecognition;

private TextView textRecognised;

private ImageView picture;

private SpeechRecognizer sr;

private Context context;

private MyRecognitionListener listener;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textRecognised = (TextView) findViewById(R.id.textRecognised);

launchRecognition = (Button) findViewById(R.id.launchRecognition);

picture = (ImageView) findViewById(R.id.picture);

context = this;

sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());

listener = new MyRecognitionListener();

sr.setRecognitionListener(listener);

launchRecognition.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN: {

sr.startListening(RecognizerIntent.getVoiceDetailsIntent(context));

listener.onBeginningOfSpeech();

break;

}

case MotionEvent.ACTION_UP: {

listener.onEndOfSpeech();

sr.stopListening();

break;

}

}

return false;

}

});

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.activity_main, menu);

return true;

}

class MyRecognitionListener implements RecognitionListener {

@Override

public void onBeginningOfSpeech() {

Log.d("Speech", "onBeginningOfSpeech");

}

@Override

public void onBufferReceived(byte[] buffer) {

Log.d("Speech", "onBufferReceived");

}

@Override

public void onEndOfSpeech() {

Log.d("Speech", "onEndOfSpeech");

}

@Override

public void onError(int error) {

Log.d("Speech", "onError" + error);

}

@Override

public void onEvent(int eventType, Bundle params) {

Log.d("Speech", "onEvent");

}

@Override

public void onPartialResults(Bundle partialResults) {

Log.d("Speech", "onPartialResults");

}

@Override

public void onReadyForSpeech(Bundle params) {

Log.d("Speech", "onReadyForSpeech");

}

@Override

public void onResults(Bundle results) {

Log.d("Speech", "onResults");

ArrayList strlist = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

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

Log.d("Speech", "result=" + strlist.get(i));

}

}

@Override

public void onRmsChanged(float rmsdB) {

Log.d("Speech", "onRmsChanged");

}

}

}

它调用了beginOfSpeech()和endOfSpeech(),但从不调用listener函数 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
About Wouldn't your prefer to let your users speak instead of making them type? This plugin uses OS components for speech recognition and send it to your Unity scripts as String objects. Plugin supports: - Android >= 3.0 (haven’t tested below, it might work though… ), - iOS >= 10.0. That doesn’t mean you can’t target iOS lower than 10 - you simply have to prepare fallback code to cover cases when user doesn’t have access to speech recognition (SpeechRecognizer.EngineExists() for the help!). Keep in mind that both iOS and Android might use Internet connection for speech detection, which means it might fail in case there’s no active connection. Plugin doesn’t work in Editor! You have to run your app on real iOS or Android device. MOBILE SPEECH RECOGNIZER - UNITY PLUGIN ?2 Quick Start Open example scene Go to KKSpeechRecognizer/Example folder inside Unity and open ExampleScene: It shows basic usage of a plugin, which is: 1. Detecting if speech recognition exists on user’s device (keep in mind that it won’t be available on e.g. iOS 9 or old Android phones), 2. If it exists, and user clicks on “Start Recording” button it listens for recognized text and displays it on a screen, 3. On Android, speech recognition automatically detects when user finishes speaking, but on iOS we have to wait for user clicking “Stop Recording” to finish whole process (i.e. get final results). Before running it on Android or iOS device you have to… Setup permissions iOS After generating Xcode project (keep in mind that you have to use Xcode 8 or higher) you have to add two permissions keys to your project: MOBILE SPEECH RECOGNIZER - UNITY PLUGIN ?3 NSMicrophoneUsageDescription explanation from Apple docs: This key lets you describe the reason your app accesses any of the the device’s microphones. When the system prompts the user to allow access, this string is displayed as part of the alert. NSSpeechRecognitionUsageDescription explanation from Apple docs: This key lets you describe the reason y
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值