Android语音识别不会在单独的课程中工作

时间:2014-04-16 20:41:27

标签: java android adapter speech-recognition text-to-speech

我是Android开发的初学者。我正在尝试创建的是一个应用程序,然后等待你的答案,以便它的文本到语音的工作,然后它激活语音识别和用我的代码文本到语音工作然后它调用语音识别。然后显示错误消息,执行catch块 问题是我必须在一个单独的类中添加语音识别,然后在Adapter not Main Activity中添加一个Object。语音识别将无法正常工作,因为所有Android语音识别教程都在主要活动中添加代码而我正在按照适配器中调用的类来解决问题?

UPDATE startActivityForResult()方法无法正常工作

以下是SpeechRecogntion类的代码

public class SpeechRecog extends Activity   {

    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
    String textResult ;

    public void Start (Intent i , Context c){
        i  = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
        i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
        i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Are You Done ,  yet? ");

        try{
            startActivityForResult(i,VOICE_RECOGNITION_REQUEST_CODE); 
        } catch(Exception e) {
            Toast.makeText(c, "SpeechRecogntion is not avalible on your device ", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if ((requestCode == VOICE_RECOGNITION_REQUEST_CODE)&& (resultCode== RESULT_OK)) {
            textResult= data.getStringExtra(RecognizerIntent.EXTRA_RESULTS);

            if (checkResult())
                Toast.makeText(getApplicationContext(), "You are done ", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(getApplicationContext(), "You are not done ", Toast.LENGTH_LONG).show();
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private boolean checkResult() {
        if (textResult.equals("done")||(textResult.equals("yes")))
            return true ; 

        return false;
    }
}

以下是适配器类

中的对象
private SpeechRecog mySP; 
private Intent mySpIntetn ;

    public ListAdapterClass(Context context) {
        mytts = new TxtToSpeechClass(context); 
        mySP = new SpeechRecog();
    }

以下是我的问题依赖的地方,适配器类中的clicklistener

Lh.btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) 
    {
        if (Lh.btn.getText().equals("Play"))
        {
            Lh.btn.setText("Pause"); //change the btn status so user know how to stop it 

            List<Items> hResults=db.getAllItems(Lh.idList + 1);    

            for (int i = 0; i < hResults.size(); i++) 
            {
                Items item = hResults.get(i);
                String s = item.getItemname();  
                mytts.Talk(s,1);

                mySP.Start(mySpIntetn , context ); //the code in here wont work is there something I'm missing here 

                Toast.makeText(context,s, Toast.LENGTH_SHORT).show();
            }

            mytts.Talk("Congrats  ",1);// 1 means Queue_add

        } else {
            Lh.btn.setText("Play");
            mytts.Talk("See you later", 0); // 0 means Queue_Flush
        }
    }
);

1 个答案:

答案 0 :(得分:0)

mySpIntetn是私有的。尝试在Adapter类中将其更改为public。