C# Xamarin Android TextToSpeech.Speak不';如果从任何生命周期方法运行,请不要说任何话
我使用TextToSpeech的一个实例来转换一些文本,使用Speak()方法,如下所示:C# Xamarin Android TextToSpeech.Speak不';如果从任何生命周期方法运行,请不要说任何话,c#,android,xamarin.android,C#,Android,Xamarin.android,我使用TextToSpeech的一个实例来转换一些文本,使用Speak()方法,如下所示: textToSpeech = new TextToSpeech(context, this, "com.google.android.tts"); textToSpeech.SetPitch(1f); textToSpeech.SetSpeechRate(1f); textToSpeech.Speak(textToConvert, QueueMode.Flush, null, null); 该函数运行正
textToSpeech = new TextToSpeech(context, this, "com.google.android.tts");
textToSpeech.SetPitch(1f);
textToSpeech.SetSpeechRate(1f);
textToSpeech.Speak(textToConvert, QueueMode.Flush, null, null);
该函数运行正常,没有错误,但只有在未从生命周期方法调用该函数时,才能真正听到语音(并且isSpeaking属性仅更改为true)
我试着将它放在OnCreate()、OnStart()和OnResume()中,结果都是一样的,不过如果从按钮事件调用该函数,则该函数运行良好
这是类的限制,还是我可以解决的问题?这是因为您在加载TTS引擎之前调用Speak()方法。它需要一些时间来初始化 幸运的是,TextToSpeech.oninitListener接口提供了一种通过OnInit()方法知道引擎何时成功加载的方法 因此,如果您希望应用程序在OnCreate()中讲话,则需要将speak()方法移动到OnInit()方法。这是我为你准备的一个工作示例
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Speech.Tts;
namespace XamdroidMaster.Activities {
[Activity(ParentActivity = typeof(MainActivity), Label = "Text to Speech")]
public class TextToSpeechActivity : Activity, TextToSpeech.IOnInitListener {
private TextToSpeech tts;
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.TextToSpeech);
// Create text to speech object (first parameter: context; second parameter: object implementing TextToSpeech.IOnInitListener)
// Note that it may take a few moments for the TTS engine to initialize (OnInit() will fire when it's ready)
tts = new TextToSpeech(this, this, "com.google.android.tts");
}
public void OnInit([GeneratedEnum] OperationResult status) {
if (status == OperationResult.Success) {
tts.SetPitch(1f);
tts.SetSpeechRate(1f);
tts.Speak("Hello Luke!", QueueMode.Flush, null);
}
}
}
}
另外,通过在OnCreate()中初始化TTS引擎(如我在示例中所示),您应该能够在稍后的OnResume()中启动Speak()命令
希望这有帮助 这是因为您在加载TTS引擎之前调用Speak()方法。它需要一些时间来初始化 幸运的是,TextToSpeech.oninitListener接口提供了一种通过OnInit()方法知道引擎何时成功加载的方法 因此,如果您希望应用程序在OnCreate()中讲话,则需要将speak()方法移动到OnInit()方法。这是我为你准备的一个工作示例
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Speech.Tts;
namespace XamdroidMaster.Activities {
[Activity(ParentActivity = typeof(MainActivity), Label = "Text to Speech")]
public class TextToSpeechActivity : Activity, TextToSpeech.IOnInitListener {
private TextToSpeech tts;
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.TextToSpeech);
// Create text to speech object (first parameter: context; second parameter: object implementing TextToSpeech.IOnInitListener)
// Note that it may take a few moments for the TTS engine to initialize (OnInit() will fire when it's ready)
tts = new TextToSpeech(this, this, "com.google.android.tts");
}
public void OnInit([GeneratedEnum] OperationResult status) {
if (status == OperationResult.Success) {
tts.SetPitch(1f);
tts.SetSpeechRate(1f);
tts.Speak("Hello Luke!", QueueMode.Flush, null);
}
}
}
}
另外,通过在OnCreate()中初始化TTS引擎(如我在示例中所示),您应该能够在稍后的OnResume()中启动Speak()命令
希望这有帮助 问题在于TTS引擎初始化需要一些时间。如果初始化未结束,speak方法调用将失败。 如果您在单击按钮时“说”了什么,您可能不需要这样做,因为用户在按下按钮之前会花一些时间思考,初始化将结束。 如果您想在初始化完成后立即“说”些什么,请使用以下代码:
public class MainActivity : AppCompatActivity
{
private static TextView speechtext;
private static TextToSpeech saytext;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.layout1);
speechtext = FindViewById<TextView>(Resource.Id.textView1);
saytext = new TextToSpeech(this, new MyListener(), "com.google.android.tts");
}
class MyListener : Java.Lang.Object, TextToSpeech.IOnInitListener
{
public void OnInit([GeneratedEnum] OperationResult status)
{
if(status==OperationResult.Success)
{
saytext.SetLanguage(Locale.Us);
saytext.SetPitch(1.5f);
saytext.SetSpeechRate(1.5f);
saytext.Speak(speechtext.Text, QueueMode.Flush, null, null);
}
}
}
}
public类main活动:AppCompatActivity
{
私有静态文本视图speechtext;
私有静态文本到speech saytext;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.layout1);
speechtext=findviewbyd(Resource.Id.textView1);
saytext=newtexttospeech(这是新的MyListener(),“com.google.android.tts”);
}
类MyListener:Java.Lang.Object,TextToSpeech.OnItListener
{
OnInit上的公共无效([GenerateDum]操作结果状态)
{
如果(状态==OperationResult.Success)
{
SetLanguage(Locale.Us);
saytext.SetPitch(1.5f);
saytext.SetSpeechRate(1.5f);
saytext.Speak(speechtext.Text,QueueMode.Flush,null,null);
}
}
}
}
问题在于TTS引擎初始化需要一些时间。如果初始化未结束,speak方法调用将失败。
如果您在单击按钮时“说”了什么,您可能不需要这样做,因为用户在按下按钮之前会花一些时间思考,初始化将结束。
如果您想在初始化完成后立即“说”些什么,请使用以下代码:
public class MainActivity : AppCompatActivity
{
private static TextView speechtext;
private static TextToSpeech saytext;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.layout1);
speechtext = FindViewById<TextView>(Resource.Id.textView1);
saytext = new TextToSpeech(this, new MyListener(), "com.google.android.tts");
}
class MyListener : Java.Lang.Object, TextToSpeech.IOnInitListener
{
public void OnInit([GeneratedEnum] OperationResult status)
{
if(status==OperationResult.Success)
{
saytext.SetLanguage(Locale.Us);
saytext.SetPitch(1.5f);
saytext.SetSpeechRate(1.5f);
saytext.Speak(speechtext.Text, QueueMode.Flush, null, null);
}
}
}
}
public类main活动:AppCompatActivity
{
私有静态文本视图speechtext;
私有静态文本到speech saytext;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.layout1);
speechtext=findviewbyd(Resource.Id.textView1);
saytext=newtexttospeech(这是新的MyListener(),“com.google.android.tts”);
}
类MyListener:Java.Lang.Object,TextToSpeech.OnItListener
{
OnInit上的公共无效([GenerateDum]操作结果状态)
{
如果(状态==OperationResult.Success)
{
SetLanguage(Locale.Us);
saytext.SetPitch(1.5f);
saytext.SetSpeechRate(1.5f);
saytext.Speak(speechtext.Text,QueueMode.Flush,null,null);
}
}
}
}