【问题标题】:Get disconnect from speech recognition service与语音识别服务断开连接
【发布时间】:2015-09-25 14:41:04
【问题描述】:

如果我像这样使用语音识别

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

做一些设置.... 并制作

startActivityForResult(intent, SystemData.VOICE_RECOGNITION_REQUEST_CODE);

有没有办法使用某种意图或以任何其他方式停止服务(如销毁)?

谢谢 酒吧。

【问题讨论】:

  • 据我所知,您正在启动一项活动,而不是一项服务。我不确定您要完成什么。

标签: android speech-recognition destroy


【解决方案1】:

我找到了如何停止语音识别服务。 我创建了 SpeechRecognizer 类型的一般变量。

像这样放在课程的开头:

私人 SpeechRecognizer 语音 = null;

然后在语音初始化中,我将它连接到一个听者并在意图中使用它,如下所示:

    speech = SpeechRecognizer.createSpeechRecognizer(getActivity());

    speech.setRecognitionListener(this);

    Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

然后在这堂课的任何部分,如果我想激活演讲,我会使用这个:

speech.startListening(recognizerIntent);

为了阻止它,我这样做:

speech.stopListening();

所以听者是言语活动的关键。

【讨论】:

    【解决方案2】:

    首先将两个按钮 [buttonStart 和 buttonStop] 添加到您的活动 xml,然后
    在包含您问题中的代码的活动中添加: @覆盖 公共无效 onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

        buttonStart = (Button) findViewById(R.id.buttonStart);
        buttonStop = (Button) findViewById(R.id.buttonStop);
    
        buttonStart.setOnClickListener(this);
        buttonStop.setOnClickListener(this);
      }
    
      public void onClick(View src) {
        switch (src.getId()) {
        case R.id.buttonStart:
          Log.d(TAG, "onClick: starting srvice");
          startService(new Intent(this, MyService.class));
          break;
        case R.id.buttonStop:
          Log.d(TAG, "onClick: stopping srvice");
          stopService(new Intent(this, MyService.class));
          break;
        }
      }
    

    MyService.java

    mport android.app.Service;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.IBinder;
    import android.util.Log;
    import android.widget.Toast;
    
    public class MyService extends Service {
        private static final String TAG = "MyService";
        MediaPlayer player;
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onCreate");
    
            player = MediaPlayer.create(this, R.raw.braincandy);
            player.setLooping(false); // Set looping
        }
    
        @Override
        public void onDestroy() {
            Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onDestroy");
            player.stop();
        }
    
        @Override
        public void onStart(Intent intent, int startid) {
            Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onStart");
            player.start();
        }
    }
    

    【讨论】:

    • 也许我没有清除自己。我要停止的服务不是我的程序的一部分。它是安卓的语音识别服务,所以我不能在里面打断。我想知道我是否可以发送任何命令来停止它进行处理(处于等待任何语音但长时间不说话的状态)。所以我想在没有重播预定时间后停止它。所以它想制造一个我决定继续的情况,然后我得到一个不相关的答案,不再连接到任何东西。
    猜你喜欢
    • 1970-01-01
    • 2017-12-28
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 2014-10-04
    相关资源
    最近更新 更多