@@ -57,10 +57,10 @@ static Intent CreateSpeechIntent(SpeechToTextOptions options)
5757 return intent ;
5858 }
5959
60- static bool IsSpeechRecognitionAvailable ( ) => OperatingSystem . IsAndroidVersionAtLeast ( 34 ) && SpeechRecognizer . IsOnDeviceRecognitionAvailable ( Application . Context ) ;
60+ static bool IsSpeechRecognitionAvailable ( ) => OperatingSystem . IsAndroidVersionAtLeast ( 33 ) && SpeechRecognizer . IsOnDeviceRecognitionAvailable ( Application . Context ) ;
6161
6262 [ MemberNotNull ( nameof ( speechRecognizer ) , nameof ( listener ) ) ]
63- [ SupportedOSPlatform ( "Android34 .0" ) ]
63+ [ SupportedOSPlatform ( "Android33 .0" ) ]
6464 async Task InternalStartListening ( SpeechToTextOptions options , CancellationToken token = default )
6565 {
6666 if ( ! IsSpeechRecognitionAvailable ( ) )
@@ -81,22 +81,46 @@ async Task InternalStartListening(SpeechToTextOptions options, CancellationToken
8181 var recognitionSupportTask = new TaskCompletionSource < RecognitionSupport > ( ) ;
8282 speechRecognizer . CheckRecognitionSupport ( recognizerIntent , new Executor ( ) , new RecognitionSupportCallback ( recognitionSupportTask ) ) ;
8383 var recognitionSupportResult = await recognitionSupportTask . Task ;
84+
85+ if ( ! recognitionSupportResult . SupportedOnDeviceLanguages . Contains ( options . Culture . Name ) )
86+ {
87+ throw new NotSupportedException ( $ "Culture '{ options . Culture . Name } ' is not supported") ;
88+ }
89+ if ( recognitionSupportResult . PendingOnDeviceLanguages . Contains ( options . Culture . Name ) )
90+ {
91+ throw new Exception ( $ "Speech Recognition { options . Culture . Name } pending download. Loading can be delayed if WiFi only") ;
92+ }
8493 if ( ! recognitionSupportResult . InstalledOnDeviceLanguages . Contains ( options . Culture . Name ) )
8594 {
86- if ( ! recognitionSupportResult . SupportedOnDeviceLanguages . Contains ( options . Culture . Name ) )
95+ if ( OperatingSystem . IsAndroidVersionAtLeast ( 34 ) )
8796 {
88- throw new NotSupportedException ( $ "Culture '{ options . Culture . Name } ' is not supported") ;
97+ await TryDownloadOfflineRecognizer34Async ( recognizerIntent , token ) ;
98+ }
99+ else if ( OperatingSystem . IsAndroidVersionAtLeast ( 33 ) )
100+ {
101+ TryDownloadOfflineRecognizer33Async ( recognizerIntent ) ;
89102 }
90-
91- var downloadLanguageTask = new TaskCompletionSource ( ) ;
92- speechRecognizer . TriggerModelDownload ( recognizerIntent , new Executor ( ) , new ModelDownloadListener ( downloadLanguageTask ) ) ;
93- await downloadLanguageTask . Task . WaitAsync ( token ) ;
94103 }
95104
96105 speechRecognizer . SetRecognitionListener ( listener ) ;
97106 speechRecognizer . StartListening ( recognizerIntent ) ;
98107 }
99108
109+ [ SupportedOSPlatform ( "Android33.0" ) ]
110+ void TryDownloadOfflineRecognizer33Async ( Intent recognizerIntent )
111+ {
112+ speechRecognizer ? . TriggerModelDownload ( recognizerIntent ) ;
113+ }
114+ [ SupportedOSPlatform ( "Android34.0" ) ]
115+ async Task TryDownloadOfflineRecognizer34Async ( Intent recognizerIntent , CancellationToken token )
116+ {
117+ if ( speechRecognizer is not null )
118+ {
119+ var downloadLanguageTask = new TaskCompletionSource ( ) ;
120+ speechRecognizer . TriggerModelDownload ( recognizerIntent , new Executor ( ) , new ModelDownloadListener ( downloadLanguageTask ) ) ;
121+ await downloadLanguageTask . Task . WaitAsync ( token ) ;
122+ }
123+ }
100124 void HandleListenerError ( SpeechRecognizerError error )
101125 {
102126 OnRecognitionResultCompleted ( SpeechToTextResult . Failed ( new Exception ( $ "Failure in speech engine - { error } ") ) ) ;
0 commit comments