首先,我知道在后台或服务中已经有很多关于这个语音识别的问题。我想我已经在2周内检查了所有这些问题:P。但我不明白所有这些答案。我也使用了那里的代码,但它不起作用。我想要的是当用户点击按钮启动语音识别服务时,服务就会启动,甚至android也被锁定,服务会听取用户的指示。有人能告诉我如何实现这个或任何教程吗?我从2周开始就在做这个。我在谷歌和SO上搜索了很多。
===========================更新=========================我正在调用MainActivity中的服务,但该服务已启动,并且也收到消息,但RecognitionListener类方法未启动。我正在使用这个Android语音识别连续服务的代码
有人能告诉我我的代码出了什么问题吗......
这是主要活动
package com.android.jarvis.voicerecognitionservice;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import static com.android.jarvis.voicerecognitionservice.BuildConfig.DEBUG;
public class MainActivity extends AppCompatActivity {
static final String TAG = "Service";
private int mBindFlag;
private Messenger mServiceMessenger;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Intent service = new Intent(MainActivity, RecognitionService.class);
startService(new Intent(this, RecognitionService.class));
mBindFlag = Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH ? 0 : Context.BIND_ABOVE_CLIENT;
}
@Override
protected void onStart()
{
super.onStart();
bindService(new Intent(this, RecognitionService.class), mServiceConnection, mBindFlag);
}
@Override
protected void onStop()
{
super.onStop();
if (mServiceMessenger != null)
{
unbindService(mServiceConnection);
mServiceMessenger = null;
}
}
private final ServiceConnection mServiceConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
if (DEBUG) {Log.d(TAG, "onServiceConnected");} //$NON-NLS-1$
mServiceMessenger = new Messenger(service);
Message msg = new Message();
msg.what = RecognitionService.MSG_RECOGNIZER_START_LISTENING;
try
{
mServiceMessenger.send(msg);
Log.d(TAG,"Message Sent");
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name)
{
if (DEBUG) {
Log.d(TAG, "onServiceDisconnected");} //$NON-NLS-1$
mServiceMessenger = null;
}
}; //
}
这是识别服务
package com.android.jarvis.voicerecognitionservice;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.widget.Toast;
import java.lang.ref.WeakReference;
import static com.android.jarvis.voicerecognitionservice.MainActivity.TAG;
public class RecognitionService extends Service {
static AudioManager mAudioManager;
protected SpeechRecognizer mSpeechRecognizer;
protected Intent mSpeechRecognizerIntent;
protected final Messenger mServerMessenger = new Messenger(new IncomingHandler(this));
static boolean mIsListening;
static volatile boolean mIsCountDownOn;
static boolean mIsStreamSolo;
static final int MSG_RECOGNIZER_START_LISTENING = 1;
static final int MSG_RECOGNIZER_CANCEL = 2;
@Override
public void onCreate()
{
super.onCreate();
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener());
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
}
protected static class IncomingHandler extends Handler
{
private WeakReference<RecognitionService> mtarget;
IncomingHandler(RecognitionService target)
{
mtarget = new WeakReference<RecognitionService>(target);
}
@Override
public void handleMessage(Message msg)
{
final RecognitionService target = mtarget.get();
switch (msg.what)
{
case MSG_RECOGNIZER_START_LISTENING:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
// turn off beep sound
// if (!mIsStreamSolo)
// {
// mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
// mIsStreamSolo = true;
// }
}
if (!target.mIsListening)
{
target.mSpeechRecognizer.startListening(target.mSpeechRecognizerIntent);
target.mIsListening = true;
Log.d(TAG, "message start listening"); //$NON-NLS-1$
}
break;
case MSG_RECOGNIZER_CANCEL:
if (mIsStreamSolo)
{
mAudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false);
mIsStreamSolo = false;
}
target.mSpeechRecognizer.cancel();
target.mIsListening = false;
Log.d(TAG, "message canceled recognizer"); //$NON-NLS-1$
break;
}
}
}
// Count down timer for Jelly Bean work around
protected CountDownTimer mNoSpeechCountDown = new CountDownTimer(5000, 5000)
{
@Override
public void onTick(long millisUntilFinished)
{
// TODO Auto-generated method stub
}
@Override
public void onFinish()
{
mIsCountDownOn = false;
Message message = Message.obtain(null, MSG_RECOGNIZER_CANCEL);
try
{
mServerMessenger.send(message);
message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
mServerMessenger.send(message);
}
catch (RemoteException e)
{
}
}
};
@Override
public void onDestroy()
{
super.onDestroy();
if (mIsCountDownOn)
{
mNoSpeechCountDown.cancel();
}
if (mSpeechRecognizer != null)
{
mSpeechRecognizer.destroy();
}
}
@Override
public IBinder onBind(Intent intent)
{
Log.d(TAG, "onBind"); //$NON-NLS-1$
return mServerMessenger.getBinder();
}
protected class SpeechRecognitionListener implements RecognitionListener
{
@Override
public void onBeginningOfSpeech()
{
// speech input will be processed, so there is no need for count down anymore
if (mIsCountDownOn)
{
mIsCountDownOn = false;
mNoSpeechCountDown.cancel();
}
Log.d(TAG, "onBeginingOfSpeech"); //$NON-NLS-1$
}
@Override
public void onBufferReceived(byte[] buffer)
{
}
@Override
public void onEndOfSpeech()
{
Log.d(TAG, "onEndOfSpeech"); //$NON-NLS-1$
}
@Override
public void onError(int error)
{
if (mIsCountDownOn)
{
mIsCountDownOn = false;
mNoSpeechCountDown.cancel();
}
mIsListening = false;
Message message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
try
{
mServerMessenger.send(message);
}
catch (RemoteException e)
{
}
Log.d(TAG, "error = " + error); //$NON-NLS-1$
}
@Override
public void onEvent(int eventType, Bundle params)
{
}
@Override
public void onPartialResults(Bundle partialResults)
{
}
@Override
public void onReadyForSpeech(Bundle params)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
mIsCountDownOn = true;
mNoSpeechCountDown.start();
}
Log.d(TAG, "onReadyForSpeech"); //$NON-NLS-1$
}
@Override
public void onResults(Bundle results)
{
Log.d(TAG, "onResults"); //$NON-NLS-1$
}
@Override
public void onRmsChanged(float rmsdB)
{
}
}
}
您应该实现识别服务。
https://developer.android.com/reference/android/speech/RecognitionService
有一个来自android的演示示例:https://android.googlesource.com/platform/development//主/示例/语音识别服务/
我正在尝试创建一个在Android 4.2中运行连续语音识别的服务。使用此链接中的答案(Android 4.1上的Android语音识别服务 我到底做错了什么? 相关活动代码(从活动调用的静态方法,actiityContext是调用此方法的活动): 服务代码:
我正在寻找一个Android语音识别库。我只需要它来理解“是/否”的答案(用不同的语言,如英语、德语、法语)。 有什么建议吗?
[可能重复]但我没有找到下面问题的答案。 在过去的两天里,我一直在做一些语音识别方面的研究,但我的问题没有得到答案: 是否可以将语音识别作为一项服务运行?我想实现这样的功能:虽然我的手机处于睡眠模式,但我需要拨打一个号码
问题内容: 我正在研究语音识别,需要一些示例程序。 谁能指导我? 问题答案: 让我剪切粘贴一下,向您展示所需的代码。 编辑:您还可以从该项目下载一个方便的抽象类。 您将需要此意图(在您认为合适的情况下进行参数化): 然后,您需要将自己的意图发送到语音识别活动,例如, 然后,您需要让自己的活动处理语音结果:
我已经设法得到连续的语音识别工作(使用Speech识别器类)作为一个服务在所有Android版本高达4.1.我的问题是让它在版本4.1和4.2上工作,因为众所周知,有一个问题是,在语音识别启动后的几秒钟内,应用编程接口没有像文件中记录的那样工作,如果没有检测到语音输入,那么就好像语音识别器无声地死去。(http://code.google.com/p/android/issues/detail?i
我想制作一个语音识别器应用程序来转录用户的语音。我不想在这样做的时候有任何对话,所以是不可能的。(我知道如果我使用这种方法,我可以获得音频) 我正在使用SpeechRecognitor,并调用startListening来收听用户的音频。我在onResults中得到了非常准确的结果。 现在,我还需要在我的设备SD卡中存储用户的音频。为此,我尝试了MediaRecorder和AudioRecord,