Skip to content
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

[PR] Return support Android 33 offline speech recognition #2578

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

alex3696
Copy link
Contributor

@alex3696 alex3696 commented Mar 11, 2025

Nothing complicated, just returned the support of Android 33.
Corrected check placement: "Culture ... is not supported".
Added check, that the language is pending downloading (including delayed through Wi-Fi)
Added check, that avoids endless expectation if the load will be scheduled on WiFi

Description of Change

Linked Issues

#2463 (comment)

  • Fixes #

PR Checklist

Additional information

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for Android 33 for offline speech recognition while refining culture/language checks and download triggers.

  • Updated Android version checks from 34 to 33 where appropriate.
  • Revised language support checks to distinguish between supported, pending, and installed languages.
  • Introduced separate methods for triggering offline recognizer downloads on Android 33 and Android 34.

}
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


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.

Copy link
Collaborator

@VladislavAntonyuk VladislavAntonyuk left a comment

Choose a reason for hiding this comment

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

I have concerns about user behavior with Android 33. Recognition starts before language is downloaded. Getting infinite exception in case language is pending download (as there is no way to notify user when it is finished), more code with checks for different API versions.
All these issues are resolved in Android 34 (just one version up), so I am voting for closing this PR and keeping minimum version = 34.

@alex3696
Copy link
Contributor Author

alex3696 commented Mar 13, 2025

I have concerns about user behavior with Android 33. Recognition starts before language is downloaded. Getting infinite exception in case language is pending download (as there is no way to notify user when it is finished), more code with checks for different API versions. All these issues are resolved in Android 34 (just one version up), so I am voting for closing this PR and keeping minimum version = 34.

I don't understand your concerns.
There are no infinite exceptions: only one check block on start and one exception - recognition will not begin if there is no language or it is in the process of loading.
API Android 34 does not solve anything in this case, only adds the ability to monitor the progress of loading onProgress, onScheduled .
Please note that you do not process this onScheduled, this leads to endless expectation if the load will be scheduled on WiFi or energy saving reason.
This check avoids such behavior if the load is already scheduled.

if (recognitionSupportResult.PendingOnDeviceLanguages.Contains(options.Culture.Name))
{
	throw new Exception($"Speech Recognition {options.Culture.Name} pending download. Loading can be delayed if WiFi only");
}

Look carefully again, someone third may judge us. We can add your corrections. I am for the discussion.

Another one reason to Android 33 - no matter how interesting it, but I can’t help you, I do not have device with android 34 and above ;-)

@VladislavAntonyuk
Copy link
Collaborator

How do you plan to use the API?

do
{
	try
	{
		await speechRecognizer.StartListening(...);
	}
	catch 
    {
	    
	}
}
while (speechRecognizer.State != State.Listening)

Android 34 displays the popup and shows a download progress in the notification.

@alex3696
Copy link
Contributor Author

alex3696 commented Mar 13, 2025

You can use it like that:

try
{
    do
    {
        await speechRecognizer.StartListening(...);
    }
    while (speechRecognizer.State != State.Listening)
}
catch 
{
	    
}

I want to make voice control in my application. The prototype of API using is not yet reached. In theory, yes, a continuous listening is needed.
Maybe we will create DownloadScheduledException for cases when a language package loading or scheduled? Then it will be possible to catch and react in the right way.

my Android 33(S20+) keep silent when loading

@pictos
Copy link
Member

pictos commented Mar 18, 2025

You can use it like that:

try
{
    do
    {
        await speechRecognizer.StartListening(...);
    }
    while (speechRecognizer.State != State.Listening)
}
catch 
{
	    
}

I want to make voice control in my application. The prototype of API using is not yet reached. In theory, yes, a continuous listening is needed. Maybe we will create DownloadScheduledException for cases when a language package loading or scheduled? Then it will be possible to catch and react in the right way.

my Android 33(S20+) keep silent when loading

I don't like this API, is error prone and not intuitive at all. I advocate to rethink it or add some way to extend what we've today, then devs can implement how they want to handle this feature in older versions

@alex3696
Copy link
Contributor Author

I don't like this API

my PR - not about the API and not about use. This PR about the fact that if it is impossible to immediately start recognition for any reason, then you need to give out exception, +android 33.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants