cheli_71 2013-09-09 07:05 采纳率: 0%
浏览 1952

语音识别声音,删除重复播放的声音

我使用 Speech Recognizer Intent 来获取 User Input ,然后再翻译成文本。但是我想让intent 连续的获取
user input 然后翻译成文本,来看用户是否说了某个词。现在的代码能实现,但是每次程序开始侦听输入时,电话就会发出短期的警笛声,准备输入。
有什么方法来删除已经播放过的又一次播放的声音?
代码:

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;


public class MainActivity extends Activity implements OnClickListener 
{

       //private TextView mText;
       private SpeechRecognizer sr;
       private static final String TAG = "MyStt3Activity";
       @Override
       public void onCreate(Bundle savedInstanceState) 
       {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                ToggleButton speakButton = (ToggleButton) findViewById(R.id.CarMode);     
                //mText = (TextView) findViewById(R.id.textView1);     
                speakButton.setOnClickListener(this);
                sr = SpeechRecognizer.createSpeechRecognizer(this);       
                sr.setRecognitionListener(new listener());        
       }

       class listener implements RecognitionListener          
       {
                public void onReadyForSpeech(Bundle params)
                {
                         Log.d(TAG, "onReadyForSpeech");
                }
                public void onBeginningOfSpeech()
                {
                         Log.d(TAG, "onBeginningOfSpeech");
                }
                public void onRmsChanged(float rmsdB)
                {
                         Log.d(TAG, "onRmsChanged");
                }
                public void onBufferReceived(byte[] buffer)
                {
                         Log.d(TAG, "onBufferReceived");
                }
                public void onEndOfSpeech()
                {
                         Log.d(TAG, "onEndofSpeech");
                }
                public void onError(int error)
                {
                         Log.d(TAG,  "error " +  error);
                         Toast.makeText(getApplicationContext(), "What was that?", Toast.LENGTH_SHORT).show();
                         sr.cancel();
                         speechIntent();
                }
                public void onResults(Bundle results)                   
                {
                         //String str = new String();
                         Log.d(TAG, "onResults " + results);
                         ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                         Toast.makeText(getApplicationContext(), data.get(0), Toast.LENGTH_SHORT).show();
                         sr.cancel();
                         speechIntent();
                         //mText.setText("results: "+String.valueOf(data.size()));        
                }
                public void onPartialResults(Bundle partialResults)
                {
                         Log.d(TAG, "onPartialResults");
                }
                public void onEvent(int eventType, Bundle params)
                {
                         Log.d(TAG, "onEvent " + eventType);
                }
       }
       public void onClick(View v) {
                if (v.getId() == R.id.CarMode) 
                {
                    speechIntent();
                }
       }

       public void speechIntent()
       {
           Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
                    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"com.example.freestyleandroid");

                    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
                         sr.startListening(intent);
                         Log.i("111111","11111111");
       }
    }
  • 写回答

1条回答

  • caishiming1 2016-11-30 02:21
    关注

    感谢分享 这段代码有点用,

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料