|
| 1 | +package com.example.shraddhazingade.firstaiapiapp; |
| 2 | + |
| 3 | +import android.support.v7.app.AppCompatActivity; |
| 4 | +import android.os.Bundle; |
| 5 | +import ai.api.AIConfiguration; |
| 6 | +import ai.api.AIListener; |
| 7 | +import ai.api.AIService; |
| 8 | +import ai.api.model.AIError; |
| 9 | +import ai.api.model.AIResponse; |
| 10 | +import ai.api.model.Result; |
| 11 | + |
| 12 | +import com.google.android.gms.appindexing.AppIndex; |
| 13 | +import com.google.android.gms.common.api.GoogleApiClient; |
| 14 | +import com.google.gson.JsonElement; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +import android.util.Log; |
| 18 | +import android.view.View; |
| 19 | +import android.widget.Button; |
| 20 | +import android.widget.TextView; |
| 21 | + |
| 22 | +public class MainActivity extends AppCompatActivity implements AIListener { |
| 23 | + |
| 24 | + private Button listenButton; |
| 25 | + private TextView resultTextView; |
| 26 | + private AIService aiService; |
| 27 | + private AIListener listener; |
| 28 | + private GoogleApiClient client; |
| 29 | + |
| 30 | + @Override |
| 31 | + protected void onCreate(Bundle savedInstanceState) { |
| 32 | + super.onCreate(savedInstanceState); |
| 33 | + setContentView(R.layout.activity_main); |
| 34 | + |
| 35 | + listenButton = (Button) findViewById(R.id.listenButton); |
| 36 | + resultTextView = (TextView) findViewById(R.id.resultTextView); |
| 37 | + |
| 38 | + final AIConfiguration config = new AIConfiguration("6863b1e035514720ba21f579eaef5be4", |
| 39 | + AIConfiguration.SupportedLanguages.English, |
| 40 | + AIConfiguration.RecognitionEngine.System); |
| 41 | + |
| 42 | + aiService = AIService.getService(this, config); |
| 43 | + aiService.setListener(this); |
| 44 | + |
| 45 | + client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onStart() { |
| 52 | + super.onStart(); |
| 53 | + client.connect(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void onStop() { |
| 58 | + super.onStop(); |
| 59 | + client.disconnect(); |
| 60 | + } |
| 61 | + |
| 62 | + public void listenButtonOnClick ( final View view){ |
| 63 | + Log.d("","Entered listenButtonOnClick"); |
| 64 | + aiService.startListening(); |
| 65 | + } |
| 66 | + |
| 67 | + public void onResult(final AIResponse response) { |
| 68 | + Log.d("","Entered onResult"); |
| 69 | + Result result = response.getResult(); |
| 70 | + |
| 71 | + String parameterString=""; |
| 72 | + if (result.getParameters() != null && !result.getParameters().isEmpty()) { |
| 73 | + for (final Map.Entry<String, JsonElement> entry : result.getParameters().entrySet()) { |
| 74 | + parameterString += entry.getValue(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + resultTextView.setText(parameterString); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void onError(final AIError error) { |
| 83 | + resultTextView.setText(error.toString()); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + @Override |
| 88 | + public void onListeningStarted() { |
| 89 | + if (listener != null) { |
| 90 | + listener.onListeningStarted(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onListeningCanceled() {} |
| 96 | + |
| 97 | + @Override |
| 98 | + public void onListeningFinished() { |
| 99 | +// if (listener != null) { |
| 100 | +// listener.onListeningFinished(); |
| 101 | +// } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void onAudioLevel(final float level) { |
| 106 | + if (listener != null) { |
| 107 | + listener.onAudioLevel(level); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments