Skip to content

Commit d9a229a

Browse files
committed
che jnienv before usage
1 parent 99c8b32 commit d9a229a

1 file changed

Lines changed: 50 additions & 3 deletions

File tree

code/sound/speech_android.cpp

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,13 @@ bool speech_play(const SCP_string& text)
102102
}
103103

104104
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
105+
if (env == nullptr) {
106+
mprintf(("Speech : Unable to get JNI environment!\n"));
107+
return false;
108+
}
109+
105110
jstring j_txt = env->NewStringUTF(text.c_str());
106-
mprintf(("Speech : Playing TTS string: %s!\n", text.c_str()));
111+
nprintf(("Speech : Playing TTS string: %s!\n", text.c_str()));
107112
jboolean ok = env->CallStaticBooleanMethod(j_game_class, tts_speak, j_txt);
108113
env->DeleteLocalRef(j_txt);
109114

@@ -118,41 +123,68 @@ bool speech_stop()
118123
{
119124
if (!Speech_init)
120125
return false;
126+
121127
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
128+
if (env == nullptr) {
129+
mprintf(("Speech : Unable to get JNI environment!\n"));
130+
return false;
131+
}
132+
122133
return env->CallStaticBooleanMethod(j_game_class, tts_stop) == JNI_TRUE;
123134
}
124135

125136
bool speech_pause()
126137
{
127138
if (!Speech_init)
128139
return false;
140+
129141
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
142+
if (env == nullptr) {
143+
mprintf(("Speech : Unable to get JNI environment!\n"));
144+
return false;
145+
}
146+
130147
return env->CallStaticBooleanMethod(j_game_class, tts_pause) == JNI_TRUE;
131148
}
132149

133150
bool speech_resume()
134151
{
135152
if (!Speech_init)
136153
return false;
154+
137155
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
156+
if (env == nullptr) {
157+
mprintf(("Speech : Unable to get JNI environment!\n"));
158+
return false;
159+
}
160+
138161
return env->CallStaticBooleanMethod(j_game_class, tts_resume) == JNI_TRUE;
139162
}
140163

141164
bool speech_is_speaking()
142165
{
143166
if (!Speech_init)
144167
return false;
168+
145169
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
170+
if (env == nullptr) {
171+
mprintf(("Speech : Unable to get JNI environment!\n"));
172+
return false;
173+
}
174+
146175
return env->CallStaticBooleanMethod(j_game_class, tts_isSpeaking) == JNI_TRUE;
147176
}
148177

149178
void speech_deinit()
150179
{
151180
if (!Speech_init)
152181
return;
182+
153183
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
154-
env->CallStaticVoidMethod(j_game_class, tts_shutdown);
155-
env->DeleteGlobalRef(j_game_class);
184+
if (env != nullptr) {
185+
env->CallStaticVoidMethod(j_game_class, tts_shutdown);
186+
env->DeleteGlobalRef(j_game_class);
187+
}
156188
j_game_class = nullptr;
157189
tts_speak = nullptr;
158190
tts_stop = nullptr;
@@ -187,6 +219,11 @@ bool speech_set_rate(float rate_percent)
187219
rate_percent = 50.0;
188220

189221
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
222+
if (env == nullptr) {
223+
mprintf(("Speech : Unable to get JNI environment!\n"));
224+
return false;
225+
}
226+
190227
float android_rate = rate_percent / 100.0f; // 0.5 .. 1.0 .. 1.5
191228
env->CallStaticVoidMethod(j_game_class, tts_setRate, (jfloat)android_rate);
192229
return true;
@@ -198,6 +235,11 @@ bool speech_set_voice(int voice)
198235
return false;
199236

200237
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
238+
if (env == nullptr) {
239+
mprintf(("Speech : Unable to get JNI environment!\n"));
240+
return false;
241+
}
242+
201243
jobjectArray tags = (jobjectArray)env->CallStaticObjectMethod(j_game_class, tts_getVoices);
202244
if (tags == nullptr)
203245
return false;
@@ -223,6 +265,11 @@ SCP_vector<std::pair<int, SCP_string>> speech_enumerate_voices()
223265
return voices;
224266

225267
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
268+
if (env == nullptr) {
269+
mprintf(("Speech : Unable to get JNI environment!\n"));
270+
return voices;
271+
}
272+
226273
jobjectArray tags = (jobjectArray)env->CallStaticObjectMethod(j_game_class, tts_getVoices);
227274
if (tags == nullptr)
228275
return voices;

0 commit comments

Comments
 (0)