Commit 27511e1a by Kunj Gupta

Fixed - Make mobile app stop listening to user based on server flag.

parent 0374c45b
......@@ -45,7 +45,7 @@ public class Speaker implements TextToSpeech.OnInitListener {
}
}
public void speak(String text) {
public void speak(String text, String utteranceId) {
//remove all special characters.
String speakText = text.replaceAll("--*", " ");
// Speak only if the TTS is ready
......@@ -54,7 +54,7 @@ public class Speaker implements TextToSpeech.OnInitListener {
HashMap<String, String> hash = new HashMap<String, String>();
hash.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
String.valueOf(AudioManager.STREAM_MUSIC));
hash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "" + System.currentTimeMillis());
hash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
tts.speak(Util.fromHtml(speakText).toString(), TextToSpeech.QUEUE_ADD, hash);
}
}
......
......@@ -91,6 +91,8 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
/*According to this flag we'll close the conversation from mobile side. We'll get this flag in chat response*/
private boolean mIsConversationEnd;
private String mNewMessageUtteranceId;
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
......@@ -228,7 +230,7 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
droidSpeech.closeDroidSpeechOperations();
}
speaker.allow(true);
speaker.speak(message);
speaker.speak(message, mNewMessageUtteranceId);
}
mMessages.add(chatBotHistory);
......@@ -338,22 +340,24 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
username = data.getString(NEW_MESSAGE_USER_NAME);
message = data.getString(NEW_MESSAGE_MESSAGE);
mIsConversationEnd = data.getBoolean(IS_END_OF_INTENT);
mNewMessageUtteranceId = String.valueOf(System.currentTimeMillis());
} catch (JSONException e) {
CatalogueLog.e(e.getMessage());
return;
}
if(!message.isEmpty()) {
ChatBotUser localChatBotUser = ChatBotUserManager.getChatBotUsersByName(username);
if(localChatBotUser == null) {
if (localChatBotUser == null) {
ChatBotUser chatBotUser = ChatBotUser.ChatBotUserBuilder.aChatBotUser()
.setName(username)
.setUserSysId(ChatBotUser.CHAT_BOT_SYS_ID)
.build();
ChatBotUserManager.save(chatBotUser);
}
addMessage(username, message);
}
}
});
}
};
......@@ -458,6 +462,18 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
speaker.getTTS().setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(String utteranceId) {
/*Start Code
Sometime speaker is not getting stop that's why it is breaking.
* We need to forcefully stop it.*/
long utteranceIdLong = Long.parseLong(utteranceId);
long messageUtteranceId = Long.parseLong(mNewMessageUtteranceId);
if(utteranceIdLong == messageUtteranceId
&& speaker != null && speaker.isSpeaking()) {
speaker.stop();
}
/*End Code*/
if (!mIsConversationEnd && (!speaker.isSpeaking() && droidSpeech != null)) {
runOnUiThread(new Runnable() {
@Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment