語音播報實時天氣


一、 讓文本變成聲音

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

   這次取到的天氣信息就是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);
Location Weather Detail Speaker

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM