Skip to content

Commit b4d8edb

Browse files
committed
fix(android): fix crash on <= 21
1 parent 135bae5 commit b4d8edb

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/texttospeech.android.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,35 @@ export class TNSTextToSpeech {
3232
new android.speech.tts.TextToSpeech.OnInitListener({
3333
onInit: (status) => {
3434
// if the TextToSpeech was successful initializing
35-
if (status === android.speech.tts.TextToSpeech.SUCCESS) {
36-
this._initialized = true;
37-
const listener = new UtteranceProgressListener();
38-
this.listener = listener;
39-
this._tts.setOnUtteranceProgressListener(listener);
40-
try {
41-
const AudioAttributes = android.media.AudioAttributes;
42-
const audioAttributes = new AudioAttributes.Builder();
43-
if (options.usage !== undefined) {
44-
audioAttributes.setUsage(options.usage);
35+
try {
36+
if (status === android.speech.tts.TextToSpeech.SUCCESS) {
37+
this._initialized = true;
38+
const listener = new UtteranceProgressListener();
39+
this.listener = listener;
40+
this._tts.setOnUtteranceProgressListener(listener);
41+
if (sdkVersion >= 21) {
42+
const AudioAttributes = android.media.AudioAttributes;
43+
const audioAttributes = new AudioAttributes.Builder();
44+
if (options.usage !== undefined) {
45+
audioAttributes.setUsage(options.usage);
46+
}
47+
if (options.contentType !== undefined) {
48+
audioAttributes.setContentType(options.contentType);
49+
}
50+
if (options.flags !== undefined) {
51+
audioAttributes.setFlags(options.flags);
52+
}
53+
// const audioAttributes = new AudioAttributes.Builder()
54+
// .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION_SIGNALLING)
55+
// .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
56+
// .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED);
57+
this._tts.setAudioAttributes(audioAttributes.build());
4558
}
46-
if (options.contentType !== undefined) {
47-
audioAttributes.setContentType(options.contentType);
48-
}
49-
if (options.flags !== undefined) {
50-
audioAttributes.setFlags(options.flags);
51-
}
52-
// const audioAttributes = new AudioAttributes.Builder()
53-
// .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION_SIGNALLING)
54-
// .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
55-
// .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED);
56-
this._tts.setAudioAttributes(audioAttributes.build());
57-
} catch (e) {
58-
e.printStackTrace();
59+
} else {
60+
throw new Error('TextToSpeech failed to init with code ' + status);
5961
}
60-
} else {
61-
reject(new Error('TextToSpeech failed to init with code ' + status));
62+
} catch (e) {
63+
reject(e);
6264
}
6365
},
6466
})

0 commit comments

Comments
 (0)