Commit 064686d9 by Kunj Gupta

Fixed - TTS two messages speak issue.

parent 59b3733d
......@@ -31,7 +31,7 @@ android {
defaultConfig {
applicationId "com.vsoft.servicenow"
minSdkVersion 16
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "0.0.29"
......
......@@ -33,6 +33,7 @@ import com.vsoft.servicenow.R;
import com.vsoft.servicenow.speechRecognizer.DroidSpeech;
import com.vsoft.servicenow.speechRecognizer.OnDSListener;
import com.vsoft.servicenow.speechRecognizer.OnDSPermissionsListener;
import com.vsoft.servicenow.utils.CatalogueLog;
import com.vsoft.servicenow.utils.PrefManager;
import org.json.JSONException;
......@@ -107,10 +108,7 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
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();
checkTTS();
......@@ -135,6 +133,7 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
return false;
}
});
mInputMessageView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
......@@ -149,9 +148,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
mTyping = true;
mSocket.emit("typing");
}
mTypingHandler.removeCallbacks(onTypingTimeout);
mTypingHandler.postDelayed(onTypingTimeout, TYPING_TIMER_LENGTH);
}
@Override
......@@ -179,6 +175,9 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
mVoiceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null == mUsername) return;
if (!mSocket.connected()) return;
if(speaker != null && speaker.isSpeaking()) {
speaker.stop();
}
......@@ -190,32 +189,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
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) {
if(username.isEmpty() || message.isEmpty())
return;
......@@ -225,7 +198,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
}
speaker.allow(true);
speaker.speak(message);
speaker.allow(false);
}
mMessages.add(new Message.Builder(Message.TYPE_MESSAGE)
.username(username).message(message).build());
......@@ -234,25 +206,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
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() {
if (null == mUsername) return;
if (!mSocket.connected()) return;
......@@ -345,145 +298,26 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
return;
}
removeTyping(username);
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;
// 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 is where you process the intent and extract the speech text from the intent.
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
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 (requestCode == CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
speaker = new Speaker(this);
speaker.getTTS().setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(String utteranceId) {
if (droidSpeech != null) {
if(speaker != null && speaker.isSpeaking()) {
speaker.stop();
}
if (!speaker.isSpeaking() && droidSpeech != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
......@@ -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;
import android.content.Context;
import android.media.AudioManager;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
import com.vsoft.servicenow.utils.Util;
import java.util.HashMap;
......@@ -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
// and the user has allowed speech
if(ready && allowed) {
HashMap<String, String> hash = new HashMap<String,String>();
if (ready && allowed) {
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, "UniqueID");
hash.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "" + System.currentTimeMillis());
tts.speak(Util.fromHtml(text).toString(), TextToSpeech.QUEUE_ADD, hash);
}
}
......@@ -63,16 +60,16 @@ public class Speaker implements TextToSpeech.OnInitListener {
return tts.isSpeaking();
}
public void stop(){
public void stop() {
tts.stop();
}
public void pause(int duration){
public void pause(int duration) {
tts.playSilence(duration, TextToSpeech.QUEUE_ADD, null);
}
// Free up resources
public void destroy(){
public void destroy() {
tts.shutdown();
}
......
......@@ -246,8 +246,6 @@ public class DroidSpeech {
if (play) {
recognitionProgressView.play();
speechProgressAlertDialog.show();
// confirmLayout.setVisibility(View.GONE);
} else {
recognitionProgressView.stop();
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