本文使用了安卓自带的tts语音引擎来播放语音,请确保系统有一个语音引擎。
使用网络获取数据,需要手机有网络。
1. 工具类
module.js
// 获取时间
function getTimeString() {
var data = new Date();
var s = data.getHours()
var f = data.getMinutes()
var m = data.getSeconds()
if (s < 10) {
s = "0" + s
}
if (f < 10) {
f = "0" + f
}
if (m < 10) {
m = "0" + m
}
var ss = String(s) + ":" + String(f) + ":" + String(m)
return ss
};
// 获取天气和最近下雨
// 空格拼接或者直接拼接会导致空格(拼接)前面的重复读,耗时半小时
function weatherForcast(cityCode){
var cityInfoStr="";
var forecastStr="";
var rainStr="";
// 同步请求
var res = http.get(`http://t.weather.itboy.net/api/weather/city/${cityCode}`);
if(res.statusCode != 200){
toast("请求失败: " + res.statusCode + " " + res.statusMessage);
return "请求失败";
}
// log("html = " + res.body.string());
let resData=res.body.json();
// log(resData.data.forecast)
let forecast=resData.data.forecast;
let cityInfo=resData.cityInfo;
cityInfoStr=cityInfo.parent+cityInfo.city+",更新时间:"+cityInfo.updateTime;
// log(cityInfoStr)
forecastStr="今天播报"+forecast[0].ymd+" "+forecast[0].high+" "+forecast[0].low+" "+forecast[0].week+" "+forecast[0].type;
// log(forecastStr)
for(let i=0;i<forecast.length;i++){
if(forecast[i].type.indexOf("雨") && i!=0){
// log(forecast[i].date+" "+forecast[i].type)
rainStr=forecast[i].ymd+" "+forecast[i].type;
break;
}
//log(forecast[i].date+" "+forecast[i].high+" "+forecast[i].low+" "+forecast[i].week+" "+forecast[i].type)
}
// 天气预报不做显示,只报当天,和最近第一个下雨的日期
return forecastStr+"."+(rainStr?'最近下雨':'')+rainStr+"."+cityInfoStr;
}
module.exports.tools={
getTimeString,
weatherForcast
}
2. 播报语音
播报语音.js
注意:该脚本会会自动调大音量,student一定要关闭设置音量选项
let { tools } = require("./mokuai.js");
// 获取当前音量
let old = device.getMusicVolume();
// 设置最大音量
device.setMusicVolume(15);
importClass(android.speech.tts.TextToSpeech.Engine);
importClass(java.util.Locale);
importClass(android.speech.tts.TextToSpeech)
importClass(android.speech.tts.TextToSpeech.OnInitListener)
// 播报内容
var str = "现在是北京时间" + tools.getTimeString();
var pitch = 1.0
var speechRate = 1.0
var obj = {
onInit: function(status) {
if (status == TextToSpeech.SUCCESS) {
if (tts.setLanguage(Locale.CHINESE) == TextToSpeech.SUCCESS && tts.setPitch(pitch) == TextToSpeech.SUCCESS && tts.setSpeechRate(speechRate) == TextToSpeech.SUCCESS) {
} else {
exit()
}
} else {}
}
}
tts = new TextToSpeech(context, TextToSpeech.OnInitListener(obj))
sleep(1000)
var a = tts.speak(str, TextToSpeech.QUEUE_ADD, null);
sleep(10000)
device.setMusicVolume(old)
3. 天气预报
使用中国天气的免费api响应格式\
请自行:查询中国天气网api的城市代码
天气预报.js
let { tools } = require("./mokuai.js");
importClass(android.speech.tts.TextToSpeech.Engine);
importClass(java.util.Locale);
importClass(android.speech.tts.TextToSpeech)
importClass(android.speech.tts.TextToSpeech.OnInitListener)
// 城市编码
let cityCode=101200105
var str =tools.weatherForcast(cityCode);
log(str)
var pitch = 1.0
var speechRate = 1.0
var obj = {
onInit: function(status) {
if (status == TextToSpeech.SUCCESS) {
if (tts.setLanguage(Locale.CHINESE) == TextToSpeech.SUCCESS && tts.setPitch(pitch) == TextToSpeech.SUCCESS && tts.setSpeechRate(speechRate) == TextToSpeech.SUCCESS) {
}else{
exit()
}
} else {}
}
}
tts = new TextToSpeech(context, TextToSpeech.OnInitListener(obj))
sleep(1000)
var a = tts.speak(str, TextToSpeech.QUEUE_ADD, null);
|