Skip to content

[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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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");
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.
The correct behavior is waiting for the download.

Copy link
Contributor Author

@alex3696 alex3696 Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. If the language is not supported at all in Supportedondevicelanguages, it makes no sense to do anything, so I suffered : throw new NotSupportedException($"Culture '{options.Culture.Name}' is not supported");
  2. The download can be delayed for an indefinite period or not at all if there is no connection by WiFi. For example, in my case (Android 33), the download occurred only at night when connecting to wifi in home. This should be reported immediately, and not wait indefinite period.

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

@alex3696 alex3696 Mar 13, 2025

Choose a reason for hiding this comment

The 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?

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.
Android 34 has an extended TriggerModelDownload with ModelDownloadListener(OnProgress, OnScheduled) is better than nothing, but this is not an answer to your question - https://developer.android.com/reference/android/speech/ModelDownloadListener

}
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not start recognition if language is not installed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I thought about it, it would be possible to do so:

			else if (OperatingSystem.IsAndroidVersionAtLeast(33))
			{
				TryDownloadOfflineRecognizer33(recognizerIntent);
				throw new Exception($"Speech Recognition {options.Culture.Name} pending download. Loading can be delayed if WiFi only");
			}

but there is a chance that the package will load before the recognition launch.
I decided not to complicate the code now.

}

[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}")));
Expand Down Expand Up @@ -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()
Expand Down
Loading