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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码