Skip to content

Commit 9f8cdd0

Browse files
committed
thread safe jnienv
1 parent da27c21 commit 9f8cdd0

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

code/sound/speech_android.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "SDL_system.h"
99

1010
bool Speech_init = false;
11-
static JNIEnv* env = nullptr;
1211
static jclass j_game_class = nullptr;
1312
static jmethodID tts_speak = nullptr;
1413
static jmethodID tts_stop = nullptr;
@@ -42,7 +41,7 @@ bool speech_init()
4241
mprintf(("Speech : Try to init TTSManager on GameActivity...\n"));
4342

4443
// Get the JNI Environment pointer and current Activity instance via SDL
45-
env = (JNIEnv*)SDL_AndroidGetJNIEnv();
44+
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
4645
jobject activity = (jobject)SDL_AndroidGetActivity();
4746

4847
if (env == nullptr) {
@@ -101,7 +100,8 @@ bool speech_play(const SCP_string& text)
101100
nprintf(("Speech", "Not playing speech because passed text is empty.\n"));
102101
return false;
103102
}
104-
103+
104+
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
105105
jstring j_txt = env->NewStringUTF(text.c_str());
106106
mprintf(("Speech : Playing TTS string: %s!\n", text.c_str()));
107107
jboolean ok = env->CallStaticBooleanMethod(j_game_class, tts_speak, j_txt);
@@ -118,34 +118,39 @@ bool speech_stop()
118118
{
119119
if (!Speech_init)
120120
return false;
121+
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
121122
return env->CallStaticBooleanMethod(j_game_class, tts_stop) == JNI_TRUE;
122123
}
123124

124125
bool speech_pause()
125126
{
126127
if (!Speech_init)
127128
return false;
129+
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
128130
return env->CallStaticBooleanMethod(j_game_class, tts_pause) == JNI_TRUE;
129131
}
130132

131133
bool speech_resume()
132134
{
133135
if (!Speech_init)
134136
return false;
137+
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
135138
return env->CallStaticBooleanMethod(j_game_class, tts_resume) == JNI_TRUE;
136139
}
137140

138141
bool speech_is_speaking()
139142
{
140143
if (!Speech_init)
141144
return false;
145+
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
142146
return env->CallStaticBooleanMethod(j_game_class, tts_isSpeaking) == JNI_TRUE;
143147
}
144148

145149
void speech_deinit()
146150
{
147151
if (!Speech_init)
148152
return;
153+
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
149154
env->CallStaticVoidMethod(j_game_class, tts_shutdown);
150155
env->DeleteGlobalRef(j_game_class);
151156
j_game_class = nullptr;
@@ -181,6 +186,7 @@ bool speech_set_rate(float rate_percent)
181186
else if(rate_percent < 50.0)
182187
rate_percent = 50.0;
183188

189+
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
184190
float android_rate = rate_percent / 100.0f; // 0.5 .. 1.0 .. 1.5
185191
env->CallStaticVoidMethod(j_game_class, tts_setRate, (jfloat)android_rate);
186192
return true;
@@ -190,7 +196,8 @@ bool speech_set_voice(int voice)
190196
{
191197
if (!Speech_init)
192198
return false;
193-
199+
200+
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
194201
jobjectArray tags = (jobjectArray)env->CallStaticObjectMethod(j_game_class, tts_getVoices);
195202
if (tags == nullptr)
196203
return false;
@@ -214,7 +221,8 @@ SCP_vector<std::pair<int, SCP_string>> speech_enumerate_voices()
214221

215222
if (!Speech_init)
216223
return voices;
217-
224+
225+
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
218226
jobjectArray tags = (jobjectArray)env->CallStaticObjectMethod(j_game_class, tts_getVoices);
219227
if (tags == nullptr)
220228
return voices;

0 commit comments

Comments
 (0)