Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Use NSSpeechSynthesizer.AttributesForVoice to correctly populate the …
Browse files Browse the repository at this point in the history
…X.E Locale on Mac. (#1623)

* Use Attributes to correctly populate the X.E Locale on Mac. Fix Locale Name on iOS.

* Add ? check just in case the atrribute look-up fails and ToString() is called on a null object.

* Improve the contents of the picker for Android

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
  • Loading branch information
CartBlanche and mattleibow committed Jan 17, 2021
1 parent c541cbe commit 2ef3407
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
27 changes: 21 additions & 6 deletions Samples/Samples/ViewModel/TextToSpeechViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
Expand Down Expand Up @@ -87,13 +88,27 @@ void OnCancel()

async Task OnPickLocale()
{
var locales = await TextToSpeech.GetLocalesAsync();
var names = locales.Select(i => i.Name).ToArray();
var allLocales = await TextToSpeech.GetLocalesAsync();
var locales = allLocales
.OrderBy(i => i.Language.ToLowerInvariant())
.ToArray();

var result = await Application.Current.MainPage.DisplayActionSheet("Pick", "OK", null, names);
var languages = locales
.Select(i => string.IsNullOrEmpty(i.Country) ? i.Language : $"{i.Language} ({i.Country})")
.ToArray();

selectedLocale = locales.FirstOrDefault(i => i.Name == result);
Locale = (result == "OK" || string.IsNullOrEmpty(result)) ? "Default" : result;
var result = await Application.Current.MainPage.DisplayActionSheet("Pick", "OK", null, languages);

if (!string.IsNullOrEmpty(result) && Array.IndexOf(languages, result) is int idx && idx != -1)
{
selectedLocale = locales[idx];
Locale = result;
}
else
{
selectedLocale = null;
Locale = "Default";
}
}

public ICommand CancelCommand { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static partial class TextToSpeech

internal static Task<IEnumerable<Locale>> PlatformGetLocalesAsync() =>
Task.FromResult(AVSpeechSynthesisVoice.GetSpeechVoices()
.Select(v => new Locale(v.Language, null, v.Language, v.Identifier)));
.Select(v => new Locale(v.Language, null, v.Name, v.Identifier)));

internal static async Task PlatformSpeakAsync(string text, SpeechOptions options, CancellationToken cancelToken = default)
{
Expand Down
5 changes: 3 additions & 2 deletions Xamarin.Essentials/TextToSpeech/TextToSpeech.macos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public static partial class TextToSpeech

internal static Task<IEnumerable<Locale>> PlatformGetLocalesAsync() =>
Task.FromResult(NSSpeechSynthesizer.AvailableVoices
.Select(v => new Locale(v, null, null, null)));
.Select(voice => NSSpeechSynthesizer.AttributesForVoice(voice))
.Select(attribute => new Locale(attribute["VoiceLanguage"]?.ToString(), null, attribute["VoiceName"]?.ToString(), attribute["VoiceIdentifier"]?.ToString())));

internal static async Task PlatformSpeakAsync(string text, SpeechOptions options, CancellationToken cancelToken = default)
{
Expand All @@ -30,7 +31,7 @@ internal static async Task PlatformSpeakAsync(string text, SpeechOptions options
ss.Volume = NormalizeVolume(options.Volume);

if (options.Locale != null)
ss.Voice = options.Locale.Language;
ss.Voice = options.Locale.Id;
}

ssd.FinishedSpeaking += OnFinishedSpeaking;
Expand Down

0 comments on commit 2ef3407

Please sign in to comment.