Commit 064686d9 by Kunj Gupta

Fixed - TTS two messages speak issue.

parent 59b3733d
...@@ -31,7 +31,7 @@ android { ...@@ -31,7 +31,7 @@ android {
defaultConfig { defaultConfig {
applicationId "com.vsoft.servicenow" applicationId "com.vsoft.servicenow"
minSdkVersion 16 minSdkVersion 15
targetSdkVersion 27 targetSdkVersion 27
versionCode 1 versionCode 1
versionName "0.0.29" versionName "0.0.29"
......
...@@ -33,6 +33,7 @@ import com.vsoft.servicenow.R; ...@@ -33,6 +33,7 @@ import com.vsoft.servicenow.R;
import com.vsoft.servicenow.speechRecognizer.DroidSpeech; import com.vsoft.servicenow.speechRecognizer.DroidSpeech;
import com.vsoft.servicenow.speechRecognizer.OnDSListener; import com.vsoft.servicenow.speechRecognizer.OnDSListener;
import com.vsoft.servicenow.speechRecognizer.OnDSPermissionsListener; import com.vsoft.servicenow.speechRecognizer.OnDSPermissionsListener;
import com.vsoft.servicenow.utils.CatalogueLog;
import com.vsoft.servicenow.utils.PrefManager; import com.vsoft.servicenow.utils.PrefManager;
import org.json.JSONException; import org.json.JSONException;
...@@ -107,10 +108,7 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -107,10 +108,7 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError); mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError); mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.on("new message", onNewMessage); mSocket.on("new message", onNewMessage);
mSocket.on("user joined", onUserJoined);
mSocket.on("user left", onUserLeft);
mSocket.on("typing", onTyping);
mSocket.on("stop typing", onStopTyping);
mSocket.connect(); mSocket.connect();
checkTTS(); checkTTS();
...@@ -135,6 +133,7 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -135,6 +133,7 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
return false; return false;
} }
}); });
mInputMessageView.addTextChangedListener(new TextWatcher() { mInputMessageView.addTextChangedListener(new TextWatcher() {
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {
...@@ -149,9 +148,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -149,9 +148,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
mTyping = true; mTyping = true;
mSocket.emit("typing"); mSocket.emit("typing");
} }
mTypingHandler.removeCallbacks(onTypingTimeout);
mTypingHandler.postDelayed(onTypingTimeout, TYPING_TIMER_LENGTH);
} }
@Override @Override
...@@ -179,6 +175,9 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -179,6 +175,9 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
mVoiceButton.setOnClickListener(new View.OnClickListener() { mVoiceButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (null == mUsername) return;
if (!mSocket.connected()) return;
if(speaker != null && speaker.isSpeaking()) { if(speaker != null && speaker.isSpeaking()) {
speaker.stop(); speaker.stop();
} }
...@@ -190,32 +189,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -190,32 +189,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
mMessagesView.addOnItemTouchListener(mOnItemTouchListener); mMessagesView.addOnItemTouchListener(mOnItemTouchListener);
} }
@Override
public void onDestroy() {
super.onDestroy();
mSocket.disconnect();
mSocket.off(Socket.EVENT_CONNECT, onConnect);
mSocket.off(Socket.EVENT_DISCONNECT, onDisconnect);
mSocket.off(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.off("new message", onNewMessage);
mSocket.off("user joined", onUserJoined);
mSocket.off("user left", onUserLeft);
mSocket.off("typing", onTyping);
mSocket.off("stop typing", onStopTyping);
speaker.destroy();
}
private void addLog(String message) {
mMessages.add(new Message.Builder(Message.TYPE_LOG)
.message(message).build());
mAdapter.notifyItemInserted(mMessages.size() - 1);
scrollToBottom();
}
private void addMessage(String username, String message) { private void addMessage(String username, String message) {
if(username.isEmpty() || message.isEmpty()) if(username.isEmpty() || message.isEmpty())
return; return;
...@@ -225,7 +198,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -225,7 +198,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
} }
speaker.allow(true); speaker.allow(true);
speaker.speak(message); speaker.speak(message);
speaker.allow(false);
} }
mMessages.add(new Message.Builder(Message.TYPE_MESSAGE) mMessages.add(new Message.Builder(Message.TYPE_MESSAGE)
.username(username).message(message).build()); .username(username).message(message).build());
...@@ -234,25 +206,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -234,25 +206,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
scrollToBottom(); scrollToBottom();
} }
private void addTyping(String username) {
if(username.isEmpty())
return;
mMessages.add(new Message.Builder(Message.TYPE_ACTION)
.username(username).build());
mAdapter.notifyItemInserted(mMessages.size() - 1);
scrollToBottom();
}
private void removeTyping(String username) {
for (int i = mMessages.size() - 1; i >= 0; i--) {
Message message = mMessages.get(i);
if (message.getType() == Message.TYPE_ACTION && message.getUsername().equals(username)) {
mMessages.remove(i);
mAdapter.notifyItemRemoved(i);
}
}
}
private void attemptSend() { private void attemptSend() {
if (null == mUsername) return; if (null == mUsername) return;
if (!mSocket.connected()) return; if (!mSocket.connected()) return;
...@@ -345,145 +298,26 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -345,145 +298,26 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
return; return;
} }
removeTyping(username);
addMessage(username, message); addMessage(username, message);
} }
}); });
} }
}; };
private Emitter.Listener onUserJoined = new Emitter.Listener() {
@Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
String username;
int numUsers;
try {
username = data.getString("username");
numUsers = data.getInt("numUsers");
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
return;
}
// addLog(getResources().getString(R.string.message_user_joined, username));
// addParticipantsLog(numUsers);
}
});
}
};
private Emitter.Listener onUserLeft = new Emitter.Listener() {
@Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
String username;
int numUsers;
try {
username = data.getString("username");
numUsers = data.getInt("numUsers");
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
return;
}
removeTyping(username);
}
});
}
};
private Emitter.Listener onTyping = new Emitter.Listener() {
@Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
String username;
try {
username = data.getString("username");
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
return;
}
addTyping(username);
}
});
}
};
private Emitter.Listener onStopTyping = new Emitter.Listener() {
@Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
String username;
try {
username = data.getString("username");
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
return;
}
removeTyping(username);
}
});
}
};
private Runnable onTypingTimeout = new Runnable() {
@Override
public void run() {
if (!mTyping) return;
mTyping = false;
mSocket.emit("stop typing");
}
};
private static final int SPEECH_REQUEST_CODE = 101;
private final int CHECK_CODE = 102; private final int CHECK_CODE = 102;
// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Start the activity, the intent will be populated with the speech text
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
// This callback is invoked when the Speech Recognizer returns. // This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent. // This is where you process the intent and extract the speech text from the intent.
@Override @Override
public void onActivityResult(int requestCode, int resultCode, public void onActivityResult(int requestCode, int resultCode,
Intent data) { Intent data) {
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) { if (requestCode == CHECK_CODE) {
CURRENT_STATE = VOICE_STATE;
List<String> results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
String spokenText = results.get(0);
// Do something with spokenText
mInputMessageView.setText(spokenText);
attemptSend();
} else if (requestCode == CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
speaker = new Speaker(this); speaker = new Speaker(this);
speaker.getTTS().setOnUtteranceProgressListener(new UtteranceProgressListener() { speaker.getTTS().setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override @Override
public void onDone(String utteranceId) { public void onDone(String utteranceId) {
if (droidSpeech != null) { if (!speaker.isSpeaking() && droidSpeech != null) {
if(speaker != null && speaker.isSpeaking()) {
speaker.stop();
}
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
...@@ -594,5 +428,20 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD ...@@ -594,5 +428,20 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
} }
} }
} }
@Override
public void onDestroy() {
super.onDestroy();
mSocket.disconnect();
mSocket.off(Socket.EVENT_CONNECT, onConnect);
mSocket.off(Socket.EVENT_DISCONNECT, onDisconnect);
mSocket.off(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.off("new message", onNewMessage);
speaker.destroy();
}
} }
...@@ -3,9 +3,6 @@ package com.vsoft.servicenow.chat; ...@@ -3,9 +3,6 @@ package com.vsoft.servicenow.chat;
import android.content.Context; import android.content.Context;
import android.media.AudioManager; import android.media.AudioManager;
import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
import com.vsoft.servicenow.utils.Util; import com.vsoft.servicenow.utils.Util;
import java.util.HashMap; import java.util.HashMap;
...@@ -47,14 +44,14 @@ public class Speaker implements TextToSpeech.OnInitListener { ...@@ -47,14 +44,14 @@ public class Speaker implements TextToSpeech.OnInitListener {
} }
} }
public void speak(String text){ public void speak(String text) {
// Speak only if the TTS is ready // Speak only if the TTS is ready
// and the user has allowed speech // and the user has allowed speech
if(ready && allowed) { if (ready && allowed) {
HashMap<String, String> hash = new HashMap<String,String>(); HashMap<String, String> hash = new HashMap<String, String>();
hash.put(TextToSpeech.Engine.KEY_PARAM_STREAM, hash.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
String.valueOf(AudioManager.STREAM_MUSIC)); String.valueOf(AudioManager.STREAM_MUSIC));
hash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "UniqueID"); hash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "" + System.currentTimeMillis());
tts.speak(Util.fromHtml(text).toString(), TextToSpeech.QUEUE_ADD, hash); tts.speak(Util.fromHtml(text).toString(), TextToSpeech.QUEUE_ADD, hash);
} }
} }
...@@ -63,16 +60,16 @@ public class Speaker implements TextToSpeech.OnInitListener { ...@@ -63,16 +60,16 @@ public class Speaker implements TextToSpeech.OnInitListener {
return tts.isSpeaking(); return tts.isSpeaking();
} }
public void stop(){ public void stop() {
tts.stop(); tts.stop();
} }
public void pause(int duration){ public void pause(int duration) {
tts.playSilence(duration, TextToSpeech.QUEUE_ADD, null); tts.playSilence(duration, TextToSpeech.QUEUE_ADD, null);
} }
// Free up resources // Free up resources
public void destroy(){ public void destroy() {
tts.shutdown(); tts.shutdown();
} }
......
...@@ -246,8 +246,6 @@ public class DroidSpeech { ...@@ -246,8 +246,6 @@ public class DroidSpeech {
if (play) { if (play) {
recognitionProgressView.play(); recognitionProgressView.play();
speechProgressAlertDialog.show(); speechProgressAlertDialog.show();
// confirmLayout.setVisibility(View.GONE);
} else { } else {
recognitionProgressView.stop(); recognitionProgressView.stop();
speechProgressAlertDialog.cancel(); speechProgressAlertDialog.cancel();
......
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