通过Web Service获取天气预报并朗读
环境:Visual Studio 2010 C#
1. 新建项目-->Windows窗体应用程序。输入解决方案名,假定为TTS。
2. 在界面设计里拉一个textBox和一个Button。textBox1用来装文本信息,点击button1触发获取天气预报事件并朗读。
3. 在“解决方案资源管理器”中,在解决方案TTS上右键-->添加服务引用,弹出如下界面:

点“高级”,出现如下界面:

再点“添加Web引用”,弹出如下界面:
在地址栏上输入http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

--------------------------------------分割线 start----------------------------------------------------
【题外话】
这是中国提供的天气预报的Web服务。在该站点的主页http://www.webxml.com.cn/zh_cn/web_services.aspx上,还能找到其它诸如查询手机号码归属地、航班时刻表、汇率、英汉翻译、QQ是否在线、验证码图片生成等Web服务。
另外介绍几个国外提供的Web Service站点(没试过,不知能不能用):
·专门关于地图的Web服务:
http://www.opengeospatial.org/standards/wms
·一个很丰富的Web服务站点:
http://www.programmableweb.com/apis/directory/1?sort=category
--------------------------------------分割线 end----------------------------------------------------
注意1:到现在的窗口标题已经为“添加Web引用”了。最开始是“添加服务引用”。这两种是不一样的。虽然在第一步的时候,在地址栏里输入以上地址,前往,也能查找到这个Web服务,但是添加到项目之后,后面用的时候就有问题,不能找到需要的类和方法(具体见后边代码)。也可能是我不会用“服务引用”。总之我只知道怎么用“Web引用”。
注意2:留意上图中的“Web引用名”,相当于添加的Web引用的命名空间。可以自己改。
点“添加引用”后,这个天气预报的Web Service就添加到我们的项目里了,可在“解决方案资源管理器”里看到,如图:

4. 接下来就是怎么使用weather web service了。
为button1添加响应函数button1_Click。具体见代码:
private void button1_Click(object sender, EventArgs e) { // get weather string city = "武汉"; TTS.cn.com.webxml.www.WeatherWebService wws = new cn.com.webxml.www.WeatherWebService(); string[] wwsArray = wws.getWeatherbyCityName(city); this.textBox1.Text = wwsArray[0] + " " + wwsArray[1] + " " + wwsArray[5] + " " + wwsArray[6] + "。" + Environment.NewLine + "\r\n" + wwsArray[10] + "。"+ Environment.NewLine + wwsArray[11]; }
从Web Service获取到的信息放到字符串数组wwsArray[]里,我选择了一些字符串放到textBox1里,wwsArray[0]表示“湖北”,wwsArray[1]表示“武汉”。你也可以把整个字符串数组放进textBox1,不过注意有些信息是城市的图片名,好像wwsArray[2]就是。
5. 添加朗读天气预报的功能。
首先在C盘下查找sapi.dll,我的如下图,在两个目录下都有这个dll:

记住这个文件的目录,随便用那个都行,两个是一样的。
然后在Visual Studio 2010中,在“解决方案资源管理器”中项目名TTS上右键-->添加引用,弹出如下图:

在浏览选项卡中选中我们之前在C盘里找到的sapi.dll,点确定。就添加进去了。可在项目中看到,如下图:

这货叫“SpeechLib”。
6. 在代码中使用SpeechLib库。
最终的完整代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using SpeechLib; namespace TTS { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // speak weather try { SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpVoice voice = new SpVoice(); if (this.textBox1.Text.Trim() == string.Empty) { // get weather string city = "武汉"; TTS.cn.com.webxml.www.WeatherWebService wws = new cn.com.webxml.www.WeatherWebService(); string[] wwsArray = wws.getWeatherbyCityName(city); this.textBox1.Text = wwsArray[0] + " " + wwsArray[1] + " " + wwsArray[5] + " " + wwsArray[6] + "。" + Environment.NewLine + "\r\n" + wwsArray[10] + "。"+ Environment.NewLine + wwsArray[11]; voice.Speak(this.textBox1.Text, spFlags); } else { voice.Speak(this.textBox1.Text, spFlags); } } catch (Exception err) { MessageBox.Show(err.Message); } } } }
代运行效果截图:

出现文本信息的同时能够听见一个女声在朗读,断句组词什么的还算智能。