Skip to content

Commit 99528ba

Browse files
committed
feat(android): init options to define audioattributes
1 parent 4c51c6a commit 99528ba

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/index.d.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1+
export interface InitOptions {
2+
/**
3+
* Android: AudioAttributes flags
4+
*/
5+
flags: number;
6+
/**
7+
* Android: AudioAttributes contentType
8+
*/
9+
contentType: number;
10+
/**
11+
* Android: AudioAttributes usage
12+
*/
13+
usage: number;
14+
}
15+
116
export declare class TNSTextToSpeech {
2-
init(): Promise<void>;
17+
init(options?: InitOptions): Promise<void>;
318

419
/**
520
* Initiate the text to speech.

src/texttospeech.android.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Application } from '@nativescript/core';
2-
import { Language, SpeakOptions } from './index';
1+
import { Application, Device } from '@nativescript/core';
2+
import { InitOptions, Language, SpeakOptions } from './index';
3+
const sdkVersion = parseInt(Device.sdkVersion, 10);
34

45
@NativeClass
56
class UtteranceProgressListener extends android.speech.tts.UtteranceProgressListener {
@@ -23,7 +24,7 @@ export class TNSTextToSpeech {
2324
private _lastOptions: SpeakOptions = null; // saves a reference to the last passed SpeakOptions for pause/resume/callback methods.
2425
private listener: UtteranceProgressListener;
2526

26-
private async init() {
27+
private async init(options: InitOptions = {} as any) {
2728
if (!this._tts || !this._initialized) {
2829
return new Promise<void>((resolve, reject) => {
2930
this._tts = new android.speech.tts.TextToSpeech(
@@ -36,7 +37,26 @@ export class TNSTextToSpeech {
3637
const listener = new UtteranceProgressListener();
3738
this.listener = listener;
3839
this._tts.setOnUtteranceProgressListener(listener);
39-
resolve();
40+
try {
41+
const AudioAttributes = android.media.AudioAttributes;
42+
const audioAttributes = new AudioAttributes.Builder();
43+
if (options.usage !== undefined) {
44+
audioAttributes.setUsage(options.usage);
45+
}
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+
}
4060
} else {
4161
reject(new Error('TextToSpeech failed to init with code ' + status));
4262
}
@@ -53,7 +73,7 @@ export class TNSTextToSpeech {
5373
}
5474
await this.init();
5575
let maxLen: number = 4000; // API level 18 added method for getting value dynamically
56-
if (android.os.Build.VERSION.SDK_INT >= 18) {
76+
if (sdkVersion >= 18) {
5777
try {
5878
maxLen = android.speech.tts.TextToSpeech.getMaxSpeechInputLength();
5979
} catch (error) {
@@ -139,7 +159,7 @@ export class TNSTextToSpeech {
139159
this._tts.setSpeechRate(options.speakRate);
140160

141161
const queueMode = options.queue ? android.speech.tts.TextToSpeech.QUEUE_ADD : android.speech.tts.TextToSpeech.QUEUE_FLUSH;
142-
if (android.os.Build.VERSION.SDK_INT >= 21) {
162+
if (sdkVersion >= 21) {
143163
// Hardcoded this value since the static field LOLLIPOP doesn't exist in Android 4.4
144164
/// >= Android API 21 - https://developer.android.com/reference/android/speech/tts/TextToSpeech.html#speak(java.lang.CharSequence, int, android.os.Bundle, java.lang.String)
145165
const params = new android.os.Bundle();

0 commit comments

Comments
 (0)