-
Notifications
You must be signed in to change notification settings - Fork 439
[PR] Return support Android 33 offline speech recognition #2578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8a49d0a
1a38d01
2f8b87c
4699994
15a069a
fdfb28e
1e47567
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,10 +57,10 @@ static Intent CreateSpeechIntent(SpeechToTextOptions options) | |
return intent; | ||
} | ||
|
||
static bool IsSpeechRecognitionAvailable() => OperatingSystem.IsAndroidVersionAtLeast(34) && SpeechRecognizer.IsOnDeviceRecognitionAvailable(Application.Context); | ||
static bool IsSpeechRecognitionAvailable() => OperatingSystem.IsAndroidVersionAtLeast(33) && SpeechRecognizer.IsOnDeviceRecognitionAvailable(Application.Context); | ||
|
||
[MemberNotNull(nameof(speechRecognizer), nameof(listener))] | ||
[SupportedOSPlatform("Android34.0")] | ||
[SupportedOSPlatform("Android33.0")] | ||
async Task InternalStartListening(SpeechToTextOptions options, CancellationToken token = default) | ||
{ | ||
if (!IsSpeechRecognitionAvailable()) | ||
|
@@ -81,22 +81,46 @@ async Task InternalStartListening(SpeechToTextOptions options, CancellationToken | |
var recognitionSupportTask = new TaskCompletionSource<RecognitionSupport>(); | ||
speechRecognizer.CheckRecognitionSupport(recognizerIntent, new Executor(), new RecognitionSupportCallback(recognitionSupportTask)); | ||
var recognitionSupportResult = await recognitionSupportTask.Task; | ||
|
||
if (!recognitionSupportResult.SupportedOnDeviceLanguages.Contains(options.Culture.Name)) | ||
{ | ||
throw new NotSupportedException($"Culture '{options.Culture.Name}' is not supported"); | ||
} | ||
if (recognitionSupportResult.PendingOnDeviceLanguages.Contains(options.Culture.Name)) | ||
{ | ||
throw new Exception($"Speech Recognition {options.Culture.Name} pending download. Loading can be delayed if WiFi only"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exception is not correct in this case. Developer needs to be notified when download is completed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to notify the user and ask to download using mobile data? Like store does when you try to download some app? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I did not find such an API to switch to mobile data. I also did not find an API, which method (WiFi or mobile) is now configured. If a solution is in the future, it will be possible to reconsider the logic of loading packages. |
||
} | ||
if (!recognitionSupportResult.InstalledOnDeviceLanguages.Contains(options.Culture.Name)) | ||
{ | ||
if (!recognitionSupportResult.SupportedOnDeviceLanguages.Contains(options.Culture.Name)) | ||
if (OperatingSystem.IsAndroidVersionAtLeast(34)) | ||
{ | ||
throw new NotSupportedException($"Culture '{options.Culture.Name}' is not supported"); | ||
await TryDownloadOfflineRecognizer34Async(recognizerIntent, token); | ||
} | ||
else if (OperatingSystem.IsAndroidVersionAtLeast(33)) | ||
{ | ||
TryDownloadOfflineRecognizer33(recognizerIntent); | ||
} | ||
|
||
var downloadLanguageTask = new TaskCompletionSource(); | ||
speechRecognizer.TriggerModelDownload(recognizerIntent, new Executor(), new ModelDownloadListener(downloadLanguageTask)); | ||
await downloadLanguageTask.Task.WaitAsync(token); | ||
} | ||
|
||
speechRecognizer.SetRecognitionListener(listener); | ||
speechRecognizer.StartListening(recognizerIntent); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not start recognition if language is not installed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I thought about it, it would be possible to do so:
but there is a chance that the package will load before the recognition launch. |
||
} | ||
|
||
[SupportedOSPlatform("Android33.0")] | ||
void TryDownloadOfflineRecognizer33(Intent recognizerIntent) | ||
{ | ||
speechRecognizer?.TriggerModelDownload(recognizerIntent); | ||
} | ||
[SupportedOSPlatform("Android34.0")] | ||
async Task TryDownloadOfflineRecognizer34Async(Intent recognizerIntent, CancellationToken token) | ||
{ | ||
if (speechRecognizer is not null) | ||
{ | ||
var downloadLanguageTask = new TaskCompletionSource(); | ||
speechRecognizer.TriggerModelDownload(recognizerIntent, new Executor(), new ModelDownloadListener(downloadLanguageTask)); | ||
await downloadLanguageTask.Task.WaitAsync(token); | ||
} | ||
} | ||
void HandleListenerError(SpeechRecognizerError error) | ||
{ | ||
OnRecognitionResultCompleted(SpeechToTextResult.Failed(new Exception($"Failure in speech engine - {error}"))); | ||
|
@@ -215,6 +239,7 @@ public void OnProgress(int completedPercent) | |
|
||
public void OnScheduled() | ||
{ | ||
downloadLanguageTask.SetException(new Exception($"Speech Recognition pending download. Loading Scheduled WiFi ")); | ||
} | ||
|
||
public void OnSuccess() | ||
|
Uh oh!
There was an error while loading. Please reload this page.