Example usage for android.speech RecognizerIntent RESULT_AUDIO_ERROR

List of usage examples for android.speech RecognizerIntent RESULT_AUDIO_ERROR

Introduction

In this page you can find the example usage for android.speech RecognizerIntent RESULT_AUDIO_ERROR.

Prototype

int RESULT_AUDIO_ERROR

To view the source code for android.speech RecognizerIntent RESULT_AUDIO_ERROR.

Click Source Link

Document

Result code returned when an audio error was encountered

Usage

From source file:com.manueldeveloper.SpeechRecognizer.java

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Check the request code
    if (requestCode == REQUEST_CODE) {
        // Check the result code
        if (resultCode == Activity.RESULT_OK) {
            // Get the results
            this.results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            sendResults();//ww  w  .  ja v a 2  s  .  c o  m
        } else {
            if (this.speechRecognizerCallbackContext != null) {
                switch (resultCode) {
                case RecognizerIntent.RESULT_NETWORK_ERROR:
                    this.speechRecognizerCallbackContext.error("NETWORK_ERROR");
                    break;
                case RecognizerIntent.RESULT_CLIENT_ERROR:
                    this.speechRecognizerCallbackContext.error("CLIENT_ERROR");
                    break;
                case RecognizerIntent.RESULT_SERVER_ERROR:
                    this.speechRecognizerCallbackContext.error("SERVER_ERROR");
                    break;
                case RecognizerIntent.RESULT_AUDIO_ERROR:
                    this.speechRecognizerCallbackContext.error("AUDIO_ERROR");
                    break;
                case RecognizerIntent.RESULT_NO_MATCH:
                    this.speechRecognizerCallbackContext.error("NO_MATCH");
                    break;
                }
            }
        }
    }
}

From source file:com.google.developers.actions.debugger.MainActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RECOGNIZER_REQ_CODE && resultCode == RESULT_OK) {
        ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        if (!matches.isEmpty()) {
            for (String match : matches) {
                if (match.startsWith(PLAY_PREFIX)) {
                    askCayley(match.substring(PLAY_PREFIX.length()));
                    Toast.makeText(this, match, Toast.LENGTH_LONG).show();
                    break;
                }/*from  w ww  . j a  va  2  s  .co m*/
            }
            //Result code for various error.
        } else if (resultCode == RecognizerIntent.RESULT_AUDIO_ERROR) {
            Utils.showError(MainActivity.this, "Audio Error");
        } else if (resultCode == RecognizerIntent.RESULT_CLIENT_ERROR) {
            Utils.showError(MainActivity.this, "Client Error");
        } else if (resultCode == RecognizerIntent.RESULT_NETWORK_ERROR) {
            Utils.showError(MainActivity.this, "Network Error");
        } else if (resultCode == RecognizerIntent.RESULT_NO_MATCH) {
            Utils.showError(MainActivity.this, "No Match");
        } else if (resultCode == RecognizerIntent.RESULT_SERVER_ERROR) {
            Utils.showError(MainActivity.this, "Server Error");
        }
    }
}

From source file:onion.chat.MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK)
        return;/*w ww  .  j  a v  a2 s  .  c o m*/
    if (requestCode == REQUEST_QR) {
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        int width = bitmap.getWidth(), height = bitmap.getHeight();
        int[] pixels = new int[width * height];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
        bitmap.recycle();
        RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
        BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
        MultiFormatReader reader = new MultiFormatReader();
        try {
            Result result = reader.decode(bBitmap);
            String str = result.getText();
            Log.i("ID", str);

            String[] tokens = str.split(" ", 3);

            if (tokens.length < 2 || !tokens[0].equals("Its Ur's")) {
                snack(getString(R.string.qr_invalid));
                return;
            }

            String id = tokens[1].toLowerCase();

            if (id.length() != 16) {
                snack(getString(R.string.qr_invalid));
                return;
            }

            if (db.hasContact(id)) {
                snack(getString(R.string.contact_already_added));
                return;
            }

            String name = "";
            if (tokens.length > 2) {
                name = tokens[2];
            }

            addContact(id, name);

            return;

        } catch (Exception ex) {
            snack(getString(R.string.qr_invalid));
            ex.printStackTrace();
        }
    } else if (requestCode == VOICE_RECOGNITION_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            ArrayList<String> textMatchList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            String[] textTyped = new String[textMatchList.size()];
            String typeText = "";
            for (int i = 0; i < textMatchList.size(); i++) {
                textTyped[i] = textMatchList.get(i);
                typeText += textTyped[i];
            }
            if (!textMatchList.isEmpty()) {

                if (textMatchList.get(0).contains("open") || textMatchList.get(0).contains("OPEN")) {
                    String contName = "";
                    if (typeText.contains("open chat")) {
                        if (textMatchList.size() >= 2) {
                            contName = textMatchList.get(2);
                        }
                        if (contName != "") {
                            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("chat:" + contName),
                                    getApplicationContext(), ChatActivity.class));
                        }
                    }
                    listen();
                } else if (textMatchList.get(0).contains("password")
                        || textMatchList.get(0).contains("PASSWORD")) {
                    String password = "password";

                    if (textMatchList.size() >= 2) {

                        password = textMatchList.get(0).replaceFirst("password ", "");

                    } else if (textMatchList.size() >= 1) {

                        password = textMatchList.get(0).replaceFirst("password ", "");

                    }
                    db.setPassword(password);
                    update();
                    String toSpeak = "password changed successfully to " + password;
                    Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
                    listen();

                } else if (textMatchList.get(0).contains("change") || textMatchList.get(0).contains("CHANGE")) {
                    String name = "";
                    if (textMatchList.size() >= 2) {
                        name = textMatchList.get(2);
                    }
                    db.setName(name);
                    update();
                    snack(getString(R.string.snack_alias_changed));
                    String toSpeak = "Alias changed to " + name;
                    Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
                    t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                    listen();
                } else if (textMatchList.get(0).contains("tell") || textMatchList.get(0).contains("tell")) {
                    String id1 = "";
                    id1 = (tor.getID());
                    Toast.makeText(getApplicationContext(), id1, Toast.LENGTH_SHORT).show();
                    //t1.speak(id1, TextToSpeech.QUEUE_FLUSH, null);
                    listen();
                } else if (textMatchList.get(0).contains("enter") || textMatchList.get(0).contains("ENTER")) {
                    String id1 = "Yet to come";
                    Toast.makeText(getApplicationContext(), id1, Toast.LENGTH_SHORT).show();
                    //t1.speak(id1, TextToSpeech.QUEUE_FLUSH, null);
                    listen();
                } else if (textMatchList.get(0).contains("HELP") || textMatchList.get(0).contains("help")) {
                    String toSpeak = "Voice Commands that can be used are 1 Open chat with contact name 2 change with alias name 3 tell";
                    Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
                    t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                    listen();
                }

                else if (textMatchList.get(0).contains("close") || textMatchList.get(0).contains("CLOSE")) {
                    String toSpeak = "Closing Voice command";
                    Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
                    t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                }

                else {
                    // poString
                    String toSpeak = "I Can't Understand";
                    Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
                    // t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                    listen();
                }

            }
            //Result code for various error.
        } else if (resultCode == RecognizerIntent.RESULT_AUDIO_ERROR) {

            String toSpeak = "Audio Error";
            Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
            t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
        } else if (resultCode == RecognizerIntent.RESULT_CLIENT_ERROR) {

            String toSpeak = "Client Error";
            Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
            t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
        } else if (resultCode == RecognizerIntent.RESULT_NETWORK_ERROR) {

            String toSpeak = "Network Error";
            Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
            t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
        } else if (resultCode == RecognizerIntent.RESULT_NO_MATCH) {

            String toSpeak = "No Match";
            Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
            t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
        } else if (resultCode == RecognizerIntent.RESULT_SERVER_ERROR) {

            String toSpeak = "Server Error";
            Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
            t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
        }

    }

}