Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SpeechRecognizer extends Activity implements OnInitListener{
- private static final int REQUEST_CODE = 1234;
- Button Start;
- TextView Speech;
- Dialog match_text_dialog;
- ListView textlist;
- String userRequest;
- private TextToSpeech tts;
- ArrayList<String> matches_text;
- private boolean initialized = false;
- private String queuedText;
- ArrayList<String> EnquiryList;
- //ArrayList<String> questionsList;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_speech_recognizer);
- // initQuestions();
- Start = (Button)findViewById(R.id.start_reg);
- Speech = (TextView)findViewById(R.id.speech);
- tts = new TextToSpeech(this /* context */, this /* listener */);
- Start.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- if(isConnected()){
- callSpeechRecognition();
- }
- else{
- Toast.makeText(getApplicationContext(), "Please Connect to Internet", Toast.LENGTH_LONG).show();
- }}
- });
- }
- private void callSpeechRecognition()
- {
- Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
- RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- startActivityForResult(intent, REQUEST_CODE);
- }
- public boolean isConnected()
- {
- ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo net = cm.getActiveNetworkInfo();
- if (net!=null && net.isAvailable() && net.isConnected()) {
- return true;
- } else {
- return false;
- }
- }
- private void callEnquiryForm() {
- // TODO Auto-generated method stub
- Intent intent = new Intent(getBaseContext(), CreateEnquiry.class);
- startActivity(intent);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
- match_text_dialog = new Dialog(SpeechRecognizer.this);
- match_text_dialog.setContentView(R.layout.dialog_matches);
- match_text_dialog.setTitle("Select Matching Text");
- textlist = (ListView)match_text_dialog.findViewById(R.id.list);
- matches_text = data
- .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, matches_text);
- textlist.setAdapter(adapter);
- textlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- Speech.setText("You have said " + matches_text.get(position));
- String userRequest=matches_text.get(position);
- QuestionsList enquiryList=new QuestionsList();
- EnquiryList=enquiryList.loadQuestions();
- if((userRequest.contains("enquiry"))||(userRequest.contains("query")))
- {
- //callEnquiryForm();
- processEnquiry();
- }
- match_text_dialog.hide();
- }
- });
- match_text_dialog.show();
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
- private void processEnquiry() {
- // TODO Auto-generated method stub
- for(int i=0;i<EnquiryList.size();i++)
- {
- speak(EnquiryList.get(i).toString());
- callSpeechRecognition();
- }
- }
- @Override
- public void onInit(int status) {
- if (status == TextToSpeech.SUCCESS) {
- initialized = true;
- tts.setLanguage(Locale.ENGLISH);
- if (queuedText != null) {
- speak(queuedText);
- }
- }
- }
- public void speak(String text) {
- // If not yet initialized, queue up the text.
- if (!initialized) {
- queuedText = text;
- return;
- }
- queuedText = null;
- // Before speaking the current text, stop any ongoing speech.
- //tts.stop();
- // Speak the text.
- tts.speak(text, TextToSpeech.QUEUE_ADD, null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement