简体   繁体   English

如何在 Xamarin.Android C# 中将文本更改为语音语言

[英]How to change text to speech language in Xamarin.Android C#

I wanted to make a text to speech application for a project and I used this as a reference: https://github.com/xamarin/monodroid-samples/tree/main/PlatformFeatures/TextToSpeech我想为一个项目制作一个文本到语音的应用程序,并以此作为参考: https ://github.com/xamarin/monodroid-samples/tree/main/PlatformFeatures/TextToSpeech

However, whenever i open the app it always opens up the google tts service and I cannot change the language, and the spinner does not show any languages only "Default" is there a way to make this program not open up google tts service each time and making the language options available on the spinner?但是,每当我打开应用程序时,它总是打开 google tts 服务,我无法更改语言,并且微调器不显示任何语言只有“默认”有没有办法让这个程序每次都不打开 google tts 服务并在微调器上提供语言选项? thanks so much!非常感谢!

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.Support.V7.App;
using Android.Speech.Tts;
using Java.Util;
using Android.Graphics;

namespace DND_Connect
{
    [Activity(Label = "Deaf")]
    public class DeafPage : Activity, TextToSpeech.IOnInitListener
    {
        TextToSpeech textToSpeech;
        Context context;
        private readonly int MyCheckCode = 101, NeedLang = 103;
        Java.Util.Locale lang;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.deaf);

            var btnSayIt = FindViewById<Button>(Resource.Id.buttonSpeak);
            var editWhatToSay = FindViewById<EditText>(Resource.Id.edtSpch);
            var spinLanguages = FindViewById<Spinner>(Resource.Id.spnLng);
            var txtSpeedVal = FindViewById<TextView>(Resource.Id.txtSpeed);
            var txtPitchVal = FindViewById<TextView>(Resource.Id.txtPitch);
            var seekSpeed = FindViewById<SeekBar>(Resource.Id.skSpeed);
            var seekPitch = FindViewById<SeekBar>(Resource.Id.skPitch);

            var txtView = FindViewById<TextView>(Resource.Id.deaftitle);
            var txtView1 = FindViewById<TextView>(Resource.Id.deafdesc);
            Typeface tf = Typeface.CreateFromAsset(Assets, "Roboto-Condensed.ttf");
            txtView.SetTypeface(tf, Android.Graphics.TypefaceStyle.Normal);
            txtView1.SetTypeface(tf, Android.Graphics.TypefaceStyle.Normal);

            seekSpeed.Progress = seekPitch.Progress = 127;
            txtSpeedVal.Text = txtPitchVal.Text = "0.5";

            context = btnSayIt.Context;

            textToSpeech = new TextToSpeech(this, this, "com.google.android.tts");

            var langAvailable = new List<string> { "Default" };

            var localesAvailable = Java.Util.Locale.GetAvailableLocales().ToList();
            foreach (var locale in localesAvailable)
            {
                LanguageAvailableResult res = textToSpeech.IsLanguageAvailable(locale);
                switch (res)
                {
                    case LanguageAvailableResult.Available:
                        langAvailable.Add(locale.DisplayLanguage);
                        break;
                    case LanguageAvailableResult.CountryAvailable:
                        langAvailable.Add(locale.DisplayLanguage);
                        break;
                    case LanguageAvailableResult.CountryVarAvailable:
                        langAvailable.Add(locale.DisplayLanguage);
                        break;
                }

            }
            langAvailable = langAvailable.OrderBy(t => t).Distinct().ToList();

            var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, langAvailable);
            spinLanguages.Adapter = adapter;

            lang = Java.Util.Locale.Default;
            textToSpeech.SetLanguage(lang);

            textToSpeech.SetPitch(.5f);
            textToSpeech.SetSpeechRate(.5f);

            btnSayIt.Click += delegate
            {
                if (!string.IsNullOrEmpty(editWhatToSay.Text))
                    textToSpeech.Speak(editWhatToSay.Text, QueueMode.Flush, null);
            };

            seekPitch.StopTrackingTouch += (object sender, SeekBar.StopTrackingTouchEventArgs e) =>
            {
                var seek = sender as SeekBar;
                var progress = seek.Progress / 255f;
                textToSpeech.SetPitch(progress);
                txtPitchVal.Text = progress.ToString("F2");
            };
            seekSpeed.StopTrackingTouch += (object sender, SeekBar.StopTrackingTouchEventArgs e) =>
            {
                var seek = sender as SeekBar;
                var progress = seek.Progress / 255f;
                textToSpeech.SetSpeechRate(progress);
                txtSpeedVal.Text = progress.ToString("F2");
            };

            spinLanguages.ItemSelected += (object sender, AdapterView.ItemSelectedEventArgs e) =>
            {
                lang = Java.Util.Locale.GetAvailableLocales().FirstOrDefault(t => t.DisplayLanguage == langAvailable[(int)e.Id]);
                var checkTTSIntent = new Intent();
                checkTTSIntent.SetAction(TextToSpeech.Engine.ActionCheckTtsData);
                StartActivityForResult(checkTTSIntent, NeedLang);
            };
        }
        void TextToSpeech.IOnInitListener.OnInit(OperationResult status)
        {
            if (status == OperationResult.Error)
                textToSpeech.SetLanguage(Java.Util.Locale.Default);
            if (status == OperationResult.Success)
                textToSpeech.SetLanguage(lang);
        }
        protected override void OnActivityResult(int req, Result res, Intent data)
        {
            if (req == NeedLang)
            {
                var installTTS = new Intent();
                installTTS.SetAction(TextToSpeech.Engine.ActionInstallTtsData);
                StartActivity(installTTS);
            }
        }
    }
}

[assembly: Xamarin.Forms.Dependency(typeof(TextToSpeechImplementation))] namespace ShopFloor_Automation.Droid.Implementations { public class TextToSpeechImplementation : Java.Lang.Object, ITextToSpeech, TextToSpeech.IOnInitListener { TextToSpeech speaker; [程序集:Xamarin.Forms.Dependency(typeof(TextToSpeechImplementation))] 命名空间 ShopFloor_Automation.Droid.Implementations { public class TextToSpeechImplementation : Java.Lang.Object, ITextToSpeech, TextToSpeech.IOnInitListener { TextToSpeech 扬声器; string toSpeak;字符串 toSpeak;

    public TextToSpeechImplementation() { }
    public void OnInit([GeneratedEnum] OperationResult status)
    {
        if (status.Equals(OperationResult.Success))
        {
            var p = new Dictionary<string, string>();
            speaker.Speak(toSpeak, QueueMode.Flush, p);
        }
    }

    public void Speak(string text)
    {
        var ctx = Forms.Context; // useful for many Android SDK features
        toSpeak = text;
        if (speaker == null)
        {
            speaker = new TextToSpeech(ctx, this);
        }
        else
        {
            var p = new Dictionary<string, string>();
            speaker.Speak(toSpeak, QueueMode.Flush, p);
        }
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM