语音播报实时天气
一、 让文本变成声音
//Add System.Speech reference first
using System.Speech.Synthesis;
var reader = new SpeechSynthesizer();
reader.SpeakAsync("I'm a programer. Hello, world! ");
using System.Speech.Synthesis;
var reader = new SpeechSynthesizer();
reader.SpeakAsync("I'm a programer. Hello, world! ");
Hello, world! 你听到了……这里我用了SpeakAsync方法,也就是异步执行,不会阻塞主线程。你也可以直接调用Speak()方法,也就是在一个线程里面——突然想到可以利用Speak()方法来调试程序,把断点或者Log换成Speak(): 当别人辛苦的翻阅数百行的日志--而你的电脑用悠扬的语音告诉你:“This user's entity is null, here is a bug!”,高端大气上档次呀!
二、 获取本地实时天气
园子里面有很多获取天气的API文章,这里就不介绍了,给一个CSDN链接,还算比较全:天气预报API接口大全
我这里用的都是新浪的API,最简单快捷。获取本地的实时天气,分为两步:一、根据电脑公网IP 获取当前城市;二、根据城市获取天气信息。
var webClient = new WebClient() { Encoding = Encoding.UTF8 };
//Get location city
var location = webClient.DownloadString("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json");
var json = new JavaScriptSerializer().Deserialize<dynamic>(location);
//Read city from utf-8 format
var city = HttpUtility.UrlDecode(json["city"]);
//Get location city
var location = webClient.DownloadString("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json");
var json = new JavaScriptSerializer().Deserialize<dynamic>(location);
//Read city from utf-8 format
var city = HttpUtility.UrlDecode(json["city"]);
获取到的地理信息是json格式,反序列成dynamic动态类型,不需要再去创建个类去和json数据对应,C#获取json数据就和javascript中的操作差不多了,用了当然这样也就肯定没有VS的智能感知。取到的省市信息都是UTF-8编码的,所以要取出来的话,进行Decode。
//Get weather data(xml format)
string weather = webClient.DownloadString(string.Format(
"http://php.weather.sina.com.cn/xml.php?city={0}&password=DJOYnieT8234jlsK&day=0",
HttpUtility.UrlEncode(json["city"], Encoding.GetEncoding("GB2312"))));
//Console.WriteLine(weather);
var xml = new XmlDocument();
xml.LoadXml(weather);
string weather = webClient.DownloadString(string.Format(
"http://php.weather.sina.com.cn/xml.php?city={0}&password=DJOYnieT8234jlsK&day=0",
HttpUtility.UrlEncode(json["city"], Encoding.GetEncoding("GB2312"))));
//Console.WriteLine(weather);
var xml = new XmlDocument();
xml.LoadXml(weather);
这次取到的天气信息就是XML格式的了,也很方便。但需要注意的是此,构建URL的时候要把城市采用GB2312格式编码,WebClient需要指定UTF-8格式。天气信息取到了,下面就是编字符串,让它说话了,这里附上全部的代码,总共23行:

1 //Initialize Speaker
2 var reader = new SpeechSynthesizer();
3 reader.Speak("I'm a programer,Hello, World! ");
4
5 var webClient = new WebClient() { Encoding = Encoding.UTF8 };
6 //Get location city
7 var location = webClient.DownloadString("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json");
8 var json = new JavaScriptSerializer().Deserialize<dynamic>(location);
9 //Read city from utf-8 format
10 var city = HttpUtility.UrlDecode(json["city"]);
11 //Get weather data(xml format)
12 string weather = webClient.DownloadString(string.Format(
13 "http://php.weather.sina.com.cn/xml.php?city={0}&password=DJOYnieT8234jlsK&day=0",
14 HttpUtility.UrlEncode(json["city"], Encoding.GetEncoding("GB2312"))));
15 //Console.WriteLine(weather);
16 var xml = new XmlDocument();
17 xml.LoadXml(weather);
18 //Get weather detail
19 var root = xml.SelectSingleNode("/Profiles/Weather");
20 var detail = root["status1"].InnerText + "," + root["direction1"].InnerText
21 + root["power1"].InnerText.Replace("-", "到") + "级,"
22 + root["gm_s"].InnerText + root["yd_s"].InnerText;
23 reader.SpeakAsync("今天是" + DateTime.Now.ToShortDateString() + "," + city + " " + detail);
2 var reader = new SpeechSynthesizer();
3 reader.Speak("I'm a programer,Hello, World! ");
4
5 var webClient = new WebClient() { Encoding = Encoding.UTF8 };
6 //Get location city
7 var location = webClient.DownloadString("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json");
8 var json = new JavaScriptSerializer().Deserialize<dynamic>(location);
9 //Read city from utf-8 format
10 var city = HttpUtility.UrlDecode(json["city"]);
11 //Get weather data(xml format)
12 string weather = webClient.DownloadString(string.Format(
13 "http://php.weather.sina.com.cn/xml.php?city={0}&password=DJOYnieT8234jlsK&day=0",
14 HttpUtility.UrlEncode(json["city"], Encoding.GetEncoding("GB2312"))));
15 //Console.WriteLine(weather);
16 var xml = new XmlDocument();
17 xml.LoadXml(weather);
18 //Get weather detail
19 var root = xml.SelectSingleNode("/Profiles/Weather");
20 var detail = root["status1"].InnerText + "," + root["direction1"].InnerText
21 + root["power1"].InnerText.Replace("-", "到") + "级,"
22 + root["gm_s"].InnerText + root["yd_s"].InnerText;
23 reader.SpeakAsync("今天是" + DateTime.Now.ToShortDateString() + "," + city + " " + detail);
作者:Hans Huang
出处:http://www.cnblogs.com/Hans2Rose/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
出处:http://www.cnblogs.com/Hans2Rose/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
分类:
C#
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验