没有点击的Android语音输入

时间:2014-07-06 16:52:52

标签: android android-activity voice-recognition google-voice-search

我是Android开发的新手。我需要一个我正在开发的新应用程序的解决方案,它通过映射数据库进行语音输入和语音输出。当前程序通过onlick on按钮进行语音输入。我需要一个可以接受语音输入的灵魂,而无需点击任何与Talking Tom应用程序相似的按钮。这是我的代码。我的主要代码是 speakToMe ,这是onclick& amp;的 onActivityResult

package com.example.secondprog;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
import com.example.secondprog.*;

//import com.example.secondprog.DatabaseHelper;

public class MainActivity extends Activity {

    private static final int VOICE_RECOGNITION_REQUEST = 0;

    //private static final int VOICE_RECOGNITION_REQUEST = 0x10101;

    TextToSpeech ttobj;

     String resulttxt ;

     TestDBClass db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



       db = new TestDBClass(this, null, null, 1);

        try {
            db.loadWords();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        ttobj=new TextToSpeech(getApplicationContext(), 
                  new TextToSpeech.OnInitListener() {
                  @Override
                  public void onInit(int status) {
                     if(status != TextToSpeech.ERROR){
                         ttobj.setLanguage(Locale.UK);
                         Toast.makeText(getApplicationContext(), "No error",
                                   Toast.LENGTH_LONG).show();
                        }               
                     }
                  });



        //DatabaseHelper db = DatabaseHelper(this);



        //dbdtls dbdtlsresult = new dbdtls();

        //String message3 = db.getdtls("how are");

        //String output = dbdtlsresult.new_name();

        //EditText editText = (EditText) findViewById(R.id.edit_message);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }






    public void sendMessage(View view) {
        EditText editText = (EditText) findViewById(R.id.edit_message);
         String txt = editText.getText().toString();

        // Toast.makeText(getApplicationContext(), txt.toUpperCase(),
            //     Toast.LENGTH_LONG).show();


        DictonaryDAO dictonaryDAO = 
                   db.findname(txt.toUpperCase());




         if (dictonaryDAO != null) {

                resulttxt = String.valueOf(dictonaryDAO.getnewname());

                Toast.makeText(getApplicationContext(), resulttxt.toUpperCase(),
                           Toast.LENGTH_LONG).show();


         }

         ttobj.speak(resulttxt, TextToSpeech.QUEUE_FLUSH, null);







    }



    /*@Override
       public void onPause(){
          if(ttobj !=null){
             ttobj.stop();
             ttobj.shutdown();
          }
          super.onPause();
       }
    */
    **public void speakToMe(View view) {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            "Please speak slowly and enunciate clearly.");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST);
      }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST && resultCode == RESULT_OK) {
          ArrayList<String> matches = data
              .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        //  TextView textView = (TextView) findViewById(R.id.speech_io_text);
          String firstMatch = matches.get(0);
        //  textView.setText(firstMatch);




        DictonaryDAO dictonaryDAO = 
                   db.findname(firstMatch.toUpperCase());




         if (dictonaryDAO != null) {



                resulttxt = String.valueOf(dictonaryDAO.getnewname());

                Toast.makeText(getApplicationContext(), resulttxt.toUpperCase(),
                           Toast.LENGTH_LONG).show();

                ttobj.speak(resulttxt, TextToSpeech.QUEUE_FLUSH, null);



        }


        }
      }**

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

Voice Recognition not working for talking tom app so you try to use noise detection with sound meter. below here link for voice/ noise detection with audio recording without click.

http://androidexample.com/Detect_Noise_Or_Blow_Sound_-_Set_Sound_Frequency_Thersold/index.php?view=article_discription&aid=108&aaid=130


Please change SoundMeter.java File

mRecorder.setOutputFile(Environment.getExternalStorageDirectory().
                    getAbsolutePath() + "/test.3ggp");