Android平台C# Shell应用开发TTS遇平台不支持异常求助
在Android平台开发C# Shell应用,尝试实现TTS(文本转语音)功能时,运行抛出平台不支持错误,完整错误信息:
System.PlatformNotSupportedException: Speech is not supported on this platform.
at System.Speech.Synthesis.SpeechSynthesizer..ctor () [0x0000b] in:0
at HI.Hi.Main () [0x00000] in <8252de4446134ebc80800bd83a60b532>:0
at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in:0
System.Speech.Synthesis.SpeechSynthesizer是Windows专属API,Android不支持,别用这个了。要在Android上实现TTS,有两种靠谱方法:
方法一:调用Android原生TTS API
如果是Xamarin.Android或.NET MAUI项目,直接用Android自带的TextToSpeech类:
- 编写TTS辅助类:
using Android.Speech.Tts; public class TtsHelper : Java.Lang.Object, ITextToSpeechOnInitListener { private TextToSpeech _tts; public void Init(Context context) { _tts = new TextToSpeech(context, this); } public void OnInit(OperationResult status) { if (status == OperationResult.Success) { // 初始化完成,可直接调用Speak方法 Speak("Android原生TTS测试"); } } public void Speak(string text) { _tts?.Speak(text, QueueMode.Flush, null, null); } public void Cleanup() { _tts?.Stop(); _tts?.Shutdown(); } }
- 在Activity或页面中使用:
var ttsHelper = new TtsHelper(); ttsHelper.Init(this); // 需要合成语音时调用:ttsHelper.Speak("要转换的文本内容");
同时在AndroidManifest.xml中添加必要权限:
<uses-permission android:name="android.permission.INTERNET" /> <!-- 部分TTS引擎需联网加载语音资源 -->
方法二:使用跨平台TTS库
如果是跨平台项目(如.NET MAUI),用现成的跨平台包更高效,比如Plugin.TextToSpeech:
- 安装NuGet包:
Install-Package Plugin.TextToSpeech
- 调用示例:
using Plugin.TextToSpeech; await CrossTextToSpeech.Current.SpeakAsync("跨平台TTS测试");
内容的提问来源于stack exchange,提问作者EternityZer0

