@@ -59,7 +59,26 @@ export class TNSTextToSpeech {
5959
6060 this . _lastOptions = options ;
6161
62- this . delegate . doneCallback = options . finishedCallback ;
62+ const needsAudioSession = options . sessionCategory || options . sessionMode || options . sessionRouteSharingPolicy || options . audioMixing ;
63+ const audioSession = AVAudioSession . sharedInstance ( ) ;
64+
65+ if ( needsAudioSession ) {
66+ audioSession . setCategoryModeRouteSharingPolicyOptionsError (
67+ options . sessionCategory || AVAudioSessionCategoryAmbient ,
68+ options . sessionMode || AVAudioSessionModeDefault ,
69+ options . sessionRouteSharingPolicy || AVAudioSessionRouteSharingPolicy . LongForm ,
70+ options . audioMixing ? AVAudioSessionCategoryOptions . MixWithOthers : AVAudioSessionCategoryOptions . DuckOthers ,
71+ //@ts -ignore
72+ null
73+ ) ;
74+ audioSession . setActiveError ( true ) ;
75+ }
76+ this . delegate . doneCallback = ( ) => {
77+ if ( needsAudioSession ) {
78+ audioSession . setActiveError ( false ) ;
79+ }
80+ options ?. finishedCallback ?.( ) ;
81+ } ;
6382
6483 // valid values for pitch are 0.5 to 2.0
6584 if ( ! isNumber ( options . pitch ) ) {
@@ -72,7 +91,7 @@ export class TNSTextToSpeech {
7291
7392 // valid values are AVSpeechUtteranceMinimumSpeechRate to AVSpeechUtteranceMaximumSpeechRate
7493 if ( ! isNumber ( options . speakRate ) ) {
75- options . speakRate = AVSpeechUtteranceMaximumSpeechRate / 4.0 ; // default rate is way too fast
94+ options . speakRate = AVSpeechUtteranceMaximumSpeechRate / 2.5 ; // default rate is way too fast
7695 } else if ( options . speakRate < AVSpeechUtteranceMinimumSpeechRate ) {
7796 options . speakRate = AVSpeechUtteranceMinimumSpeechRate ;
7897 } else if ( options . speakRate > AVSpeechUtteranceMaximumSpeechRate ) {
@@ -88,12 +107,13 @@ export class TNSTextToSpeech {
88107
89108 const speechUtterance = AVSpeechUtterance . alloc ( ) . initWithString ( options . text ) ;
90109
91- if ( isString ( options . locale ) && this . isValidLocale ( options . locale ) ) {
92- speechUtterance . voice = AVSpeechSynthesisVoice . voiceWithLanguage ( options . locale ) ;
93- } else if ( isString ( options . language ) && this . isValidLocale ( options . language ) ) {
94- speechUtterance . voice = AVSpeechSynthesisVoice . voiceWithLanguage ( options . language ) ;
110+ let localeStr = options . locale || options . language ;
111+ if ( localeStr ) {
112+ if ( localeStr . indexOf ( '-' ) === - 1 ) {
113+ localeStr = localeStr + '-' + localeStr . toUpperCase ( ) ;
114+ }
115+ speechUtterance . voice = AVSpeechSynthesisVoice . voiceWithLanguage ( localeStr ) ;
95116 }
96-
97117 speechUtterance . pitchMultiplier = options . pitch ;
98118 speechUtterance . volume = options . volume ;
99119 speechUtterance . rate = options . speakRate ;
0 commit comments