本文我们将在鸿蒙上引入在线语音播报功能。 需求分析 本章节我们基于上节课的内容(HTTP协议),在上节课的基础上进行延伸,方便我们去理解协议,以及引入在线语音播报的功能。 需求分析如下:文字输入城市获取城市的天气状况语音播放天气情况图标更换 控件介绍Video 用于播放视频文件并控制其播放状态的组件。 说明:该组件从APIVersion7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 权限列表:使用网络视频时,需要申请权限ohos。permission。INTERNET。 具体申请方式请参考权限申请声明:https:docs。openharmony。cnpagesv3。2Betazhcnapplicationdevsecurityaccesstokenguidelines。md子组件接口如下:Video(value:{src?:stringResource,currentProgressRate?:numberstringPlaybackSpeed,previewUri?:stringPixelMapResource,controller?:VideoController}) 参数如下: 协议介绍天气API 和上一期一样,本系统依然是使用HTTP来实现的,这里使用了天气API来实现,通过输入地区即可获得很多天气数据。数据分析如下: 未处理的数据如上,经过json分析后得到下图: 在这个基础上使用上节课的内容对文本进行拆分,比如我们要获得当前温度。tem:6, 其数据是这样的,我们观察前面和后面tem:6,tem1:13,使用掐头去尾法可以得到当前温度6。数据概括 本次不打算全部显示,这里挑选几个进行展示:当前温度当前天气当日温度区间温馨小提示语音转文字API 这里选择使用搜狗的语音转文字API,url如下:https:fanyi。sogou。comreventondcsynthesis?text一键三连speed1langzhCHSfromtranslatewebspeaker6 text要转换的文本;speed语速1?(我测试到15都还可以)越大,语速越慢;lan语言类型lanen英文lanzhCHS中文;from哪种方式请求的;speaker语音类型16的数字。 UI设计 天气图标 寻找资源如上,但本次内容只选择云、雨、晴三个经典天气来分析,将三个图标放入目录下。 使用image控件进行生成:Image(this。IMAGEURL)。width(200)。height(200)。objectFit(ImageFit。Fill) 输入框 同上期内容:TextInput({placeholder:请输入要查询的城市,controller:this。controller})。placeholderColor(Color。Grey)。placeholderFont({size:25,weight:400})。caretColor(Color。Blue)。width(90)。height(80)。margin(20)。fontSize(25)。fontColor(Color。Black)。onChange((value:string){this。INValuevalueconsole。log(JSON。stringify(this。INValue));}) 按钮和显示框Button(查询)。width(60)。height(60)。fontSize(30)。onClick((){});Blank()。height(20)Text(this。OutValue)。fontSize(25)。width(80)。height(300)。textAlign(TextAlign。Center)。border({width:1}) Vedio设计 在这里我们选择取巧的方式,使用vedio播放网络视频,只需要隐藏控制按钮,同时将控件的宽度和高度设置为1即可。 音频试听:https:tts。youdao。comfanyivoice?word一键三连lezhkeyfromspeakertarget 大家复制上面的url放进浏览器里面可用听到:Video({src:this。videoSrc,controller:this。vcontroller,})。controls(false)。onStart((){console。info(onStart)})。width(1)。height(1) 代码设计HTTPS获得数据部分 我这里将我注册的API放上来了,key部分隐藏了,不能直接使用,大家去天气API那里申请个帐号就行,免费使用2000次。httpRequest。request(填写http请求的url地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定https:v0。yiketianqi。comapi?appid56959347appsecretversinotallowv61unescape1citythis。INValue,{method:http。RequestMethod。GET,可选,默认为http。RequestMethod。GETconnectTimeout:60000,可选,默认为60sreadTimeout:60000,可选,默认为60s},(err,data){if(!err){data。result为http响应内容,可根据业务需要进行解析varGetReturndata。result。toString()console。log(JSON。stringify(GetReturn));数据拆分 该部分将返回的内容进行拆分为五个单元:天气当前温度温度区间空气质量letGetTQletGetdqletGetwdgletGetwddletGetKQZvarBeginNumGetReturn。indexOf(wea:)varLastNumGetReturn。lastIndexOf(,weaimg)varGetTQGetReturn。substring(BeginNum7,LastNum)varBeginNumGetReturn。indexOf(tem:)varLastNumGetReturn。lastIndexOf(,tem1)varGetdqGetReturn。substring(BeginNum7,LastNum)varBeginNumGetReturn。indexOf(tem1:)varLastNumGetReturn。lastIndexOf(,tem2)varGetwdgGetReturn。substring(BeginNum8,LastNum)varBeginNumGetReturn。indexOf(tem2:)varLastNumGetReturn。lastIndexOf(,win)varGetwddGetReturn。substring(BeginNum8,LastNum)varBeginNumGetReturn。indexOf(,airtips:)varLastNumGetReturn。lastIndexOf(,alarm:)varGetKQZGetReturn。substring(BeginNum14,LastNum)console。log(JSON。stringify(GetTQ));console。log(JSON。stringify(Getdq));console。log(JSON。stringify(Getwdg));console。log(JSON。stringify(Getwdd));console。log(JSON。stringify(GetKQZ));this。OutValue城市:this。INValuer天气:GetTQr温度:GetdqGetwddGetwdgr温馨提示:GetKQZ音频播放部分this。videoSrchttp:tts。youdao。comfanyivoice?wordlezhkeyfromspeakertargetthis。vcontroller。start() 将部分替换成想播报的内容就行。 演示效果注意:音频部分我尝试了模拟器和远程真机都不行,声音没法传递过来,但是实际效果是有的,这个我之前做过真机。 这里选择使用上海和安阳进行演示(任意城市都可以):