|
|
|
import android.content.Context; |
|
import android.os.AsyncTask; |
|
|
|
import ai.api.AIConfiguration; |
|
import ai.api.AIDataService; |
|
import ai.api.AIListener; |
|
import ai.api.AIService; |
|
import ai.api.AIServiceException; |
|
import ai.api.model.AIError; |
|
import ai.api.model.AIRequest; |
|
import ai.api.model.AIResponse; |
|
|
|
|
|
public class VoiceGuidedNavigation implements AIListener { |
|
|
|
private AIService aiService; |
|
private AIDataService aiDataService; |
|
private Context mContext; |
|
|
|
public VoiceGuidedNavigation(Context context) { |
|
mContext = context; |
|
final AIConfiguration config = new AIConfiguration("Your API key", |
|
"Your subscription key", |
|
AIConfiguration.SupportedLanguages.English, |
|
AIConfiguration.RecognitionEngine.System); |
|
|
|
// Use with text search |
|
aiDataService = new AIDataService(context, config); |
|
|
|
// Use with Voice input |
|
aiService = AIService.getService(context, config); |
|
aiService.setListener(this); |
|
} |
|
|
|
/** |
|
* Navigate using text |
|
* |
|
* @param text |
|
*/ |
|
public void navigateUsingText(String text) { |
|
// Setup a request object |
|
final AIRequest aiRequest = new AIRequest(); |
|
aiRequest.setQuery(text); |
|
//Perform a asynchronous task to understand the text |
|
new AsyncTask<AIRequest, Void, AIResponse>() { |
|
@Override |
|
protected AIResponse doInBackground(AIRequest... requests) { |
|
try { |
|
final AIResponse response = aiDataService.request(aiRequest); |
|
return response; |
|
} catch (AIServiceException e) { |
|
} |
|
return null; |
|
} |
|
|
|
@Override |
|
protected void onPostExecute(AIResponse aiResponse) { |
|
parseResponse(aiResponse); |
|
} |
|
}.execute(aiRequest); |
|
} |
|
|
|
/** |
|
* Start listening to user voice |
|
*/ |
|
public void startVoiceInput() { |
|
aiService.startListening(); |
|
} |
|
|
|
/** |
|
* Stop listening to user voice and get send request to understand the voice |
|
*/ |
|
public void stopVoiceInput() { |
|
aiService.stopListening(); |
|
} |
|
|
|
@Override |
|
public void onResult(AIResponse result) { |
|
parseResponse(result); |
|
} |
|
|
|
@Override |
|
public void onError(AIError error) { |
|
|
|
} |
|
|
|
@Override |
|
public void onAudioLevel(float level) { |
|
|
|
} |
|
|
|
@Override |
|
public void onListeningStarted() { |
|
|
|
} |
|
|
|
@Override |
|
public void onListeningCanceled() { |
|
|
|
} |
|
|
|
@Override |
|
public void onListeningFinished() { |
|
|
|
} |
|
|
|
/** |
|
* Parse response and perform relevant action |
|
* |
|
* @param response |
|
*/ |
|
private void parseResponse(AIResponse response) { |
|
if (response != null && response.getResult() != null) { |
|
// Perform relevant action based on the response paramater, action and intent |
|
} |
|
} |
|
} |