我已经看到了很多关于如何在活动中使用Android TextToSpeak的例子,而且还成功地使其工作得很好。我还设法让它使用插件中的绑定服务来工作,但就我的目的而言,它似乎太复杂了。这是我的VoiceService课程:
public class VoiceService : IVoiceService, TextToSpeech.IOnInitListener
{
public event EventHandler FinishedSpeakingEventHandler;
private TextToSpeech _tts;
public void Init()
{
// Use a speech progress listener so we get notified when the service finishes speaking the prompt
var progressListener = new SpeechProgressListener();
progressListener.FinishedSpeakingEventHandler += OnUtteranceCompleted;
//_tts = new TextToSpeech(Application.Context, this);
_tts = new TextToSpeech(Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity, this);
_tts.SetOnUtteranceProgressListener(progressListener);
}
public void OnInit(OperationResult status)
{
// THIS EVENT NEVER FIRES!
Console.WriteLine("VoiceService TextToSpeech Initialised. Status: " + status);
if (status == OperationResult.Success)
{
}
}
public void Speak(string prompt)
{
if (!string.IsNullOrEmpty(prompt))
{
var map = new Dictionary<string, string> { { TextToSpeech.Engine.KeyParamUtteranceId, new Guid().ToString() } };
_tts.Speak(prompt, QueueMode.Flush, map);
Console.WriteLine("tts_Speak: " + prompt);
}
else
{
Console.WriteLine("tts_Speak: PROMPT IS NULL OR EMPTY!");
}
}
/// <summary>
/// When we finish speaking, call the event handler
/// </summary>
public void OnUtteranceCompleted(object sender, EventArgs e)
{
if (FinishedSpeakingEventHandler != null)
{
FinishedSpeakingEventHandler(this, new EventArgs());
}
}
public void Dispose()
{
//throw new NotImplementedException();
}
public IntPtr Handle { get; private set; }
}请注意,OnInit方法从未被调用过。
在我的视图模型中,我想这样做:
_voiceService.Init();
_voiceService.FinishedSpeakingEventHandler += _voiceService_FinishedSpeakingEventHandler;
... and then later ...
_voiceService.Speak(prompt);当我这样做时,我会在输出中得到以下消息:
10-13 08:13:59.734 I/TextToSpeech( 2298):成功地绑定到com.google.android.tts (当我创建新的TTS对象时发生)
和
10-13 08:14:43.924 W/TextToSpeech( 2298):通话失败:未绑定到TTS引擎(当我调用tts.Speak(提示))
如果我使用的是一个活动,我会创建一个意图,使这个工作,但我不知道如何在一个插件。
提前谢谢你,
大卫
发布于 2014-10-13 10:03:15
不要自己实现Handle,而是从Java.Lang.Object派生
public class VoiceService : Java.Lang.Object, IVoiceService, TextToSpeech.IOnInitListener并删除Dispose()和Handle实现。
更多信息在这里:wrappers/
此外,我建议您在实现服务时采用异步方法,这将使从视图模型调用它类似于
await MvxResolve<ITextToSpeechService>().SpeakAsync(text);https://stackoverflow.com/questions/26334725
复制相似问题