我正在使用xamarin。 和android项目。 我创建了一个新类并添加了这段代码:
using System;
using Android.Speech.Tts;
using System.Collections.Generic;
namespace App1
{
public class TextToSpeech_Android : Java.Lang.Object, ITextToSpeech, TextToSpeech.IOnInitListener
{
TextToSpeech speaker; string toSpeak;
public TextToSpeech_Android() { }
public void Speak(string text)
{
var c = Forms.Context;
toSpeak = text;
if (speaker == null)
{
speaker = new TextToSpeech(c, this);
}
else
{
var p = new Dictionary<string, string>();
speaker.Speak(toSpeak, QueueMode.Flush, p);
System.Diagnostics.Debug.WriteLine("spoke " + toSpeak);
}
}
#region IOnInitListener implementation
public void OnInit(OperationResult status)
{
if (status.Equals(OperationResult.Success))
{
System.Diagnostics.Debug.WriteLine("speaker init");
var p = new Dictionary<string, string>();
speaker.Speak(toSpeak, QueueMode.Flush, p);
}
else
{
System.Diagnostics.Debug.WriteLine("was quiet");
}
}
#endregion
}
}
我收到一些错误和警告:
在线:
var c = Forms.Context
表格不存在。表格做了什么以及如何解决?
在线:
public class TextToSpeech_Android : Java.Lang.Object, ITextToSpeech, TextToSpeech.IOnInitListener
ITextToSpeech不存在。
并且在两条线上:
speaker.Speak(toSpeak, QueueMode.Flush, p);
我收到了警告信息:
警告3'Android.Speech.Tts.TextToSpeech.Speak(字符串,Android.Speech.Tts.QueueMode,System.Collections.Generic.IDictionary)'已过时:'“已弃用”
我想在我的程序中做的是,当我运行程序时,我在程序中放入一个字符串的文本将以语音方式读取手机中字符串中的文本。
修复错误后如何在主要活动中使用该类?
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Speech.Tts;
namespace App1
{
[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate {
button.Text = string.Format("{0} clicks!", count++);
};
}
}
}
答案 0 :(得分:1)
您应该关注并阅读tutorial you're following.
的每个部分1)将以下语句添加到文件顶部(TextToSpeech_Android.cs)
using Xamarin.Forms;
2)创建 ITextToSpeech 界面
public interface ITextToSpeech
{
void Speak (string text);
}
3)该方法已弃用,这意味着它可能不存在于Android的未来版本中,因此您应该使用another method。