C# 实现简单语音播报

Windows有文字转语音功能,C#提供了调用的类库Interop.SpeechLib.dll

使用方法很简单,在你的项目中添加Interop.SpeechLib.dll引用,在类中引用:

using SpeechLib;

**************************************************************************************************************

定义一个全局的变量

 private static SpVoice m_spVoice;

实现语音播放(文本播放)

public bool VoicePlayback (string PlayText)
{
	try
	{
		m_spVoice = new SpVoice();
		m_spVoice.Rate = 0;//语速
		m_spVoice.Volume = 100;//音量0-100
		m_spVoice.Speak(PlayText, SpeechVoiceSpeakFlags.SVSFlagsAsync);//开始
		return true;
	}
	catch (Exception e)
	{
		Console.Write(e);
		return false;
	}
}

实现语音播放暂停

public bool VoicePlaybackPause()
{
	try
	{
		m_spVoice.Pause();//暂停
		return true;
	}
	catch (Exception e)
	{
		Console.Write(e);
		return false;
	}
}

实现语音播放继续

public bool VoicePlaybackResume()
{
	try
	{
		m_spVoice.Resume();//继续
		return true;
	}
	catch (Exception e)
	{
		Console.Write(e);
		return false;
	}
}

实现语音播放停止

public bool VoicePlaybackStop()
{
	try
	{
		m_spVoice.Speak(string.Empty,SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);//停止
		return  true;
	}
	catch (Exception e)
	{
		Console.Write(e);
		return false;
	}
}

**************************************************************************************************************

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值