java语音播报天气_语音播报实时天气

一、 让文本变成声音

.Net里面自带了一个语音类库:System.Speech,调用系统的语音功能,就能实现string到语音的转换,很简单(记得先把电脑音量调大一下):

//Add System.Speech reference first 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 cityvar location = webClient.DownloadString("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json");

var json = new JavaScriptSerializer().Deserialize(location);

//Read city from utf-8 formatvar city = HttpUtility.UrlDecode(json["city"]);

获取到的地理信息是json格式,反序列成dynamic动态类型,不需要再去创建个类去和json数据对应,C#获取json数据就和

javascript中的操作差不多了,用了当然这样也就肯定没有VS的智能感知。取到的省市信息都是UTF-8编码的,所以要取出来的话,进行

Decode。

48304ba5e6f9fe08f3fa1abda7d326ab.png

//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);

48304ba5e6f9fe08f3fa1abda7d326ab.png

这次取到的天气信息就是XML格式的了,也很方便。但需要注意的是此,构建URL的时候要把城市采用GB2312格式编码,WebClient需要指定UTF-8格式。天气信息取到了,下面就是编字符串,让它说话了,这里附上全部的代码,总共23行:

//Initialize Speaker

var reader = new SpeechSynthesizer();

reader.Speak("I'm a programer,Hello, World! ");

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(location);

//Read city from utf-8 format

var city = HttpUtility.UrlDecode(json["city"]);

//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);

//Get weather detail

var root = xml.SelectSingleNode("/Profiles/Weather");

var detail = root["status1"].InnerText + "," + root["direction1"].InnerText

+ root["power1"].InnerText.Replace("-", "到") + "级,"

+ root["gm_s"].InnerText + root["yd_s"].InnerText;

reader.SpeakAsync("今天是" + DateTime.Now.ToShortDateString() + "," + city + " " + detail);Location Weather Detail Speaker

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值