赞
踩
单纯的页面api,我是要做下载所以写了一个后台,
http://tts.baidu.com/text2audio?lan=zh&pid=101&ie=UTF-8&text=我爱我自己&spd=6 播放 这是页面api
后台的api写法 这里的speekstr是要发声的内容,中文的话要用URLEncoder.encode转码,spd是声音的大小,0到9
http://tts.baidu.com/text2audio?lan=zh&pid=101&ie=UTF-8&text=" + URLEncoder.encode(speekstr, "UTF-8") + "&spd="+spd
我是来打酱油的,喵咪,开放平台获取到的开发者access_token就是拿来读单词的,顺便把中文也一起读,哈哈,玩玩,不必当真,有的直接抄的,懒得该,反正不给别人用,
照画的,核心就这么多,页面js自己玩吧,页面audio.js大家会玩吧,算了,代码都贴整齐,先开始是做验证码输出的直接拿来修改的,
- response.setCharacterEncoding("UTF-8");
- response.setHeader("contentType", "text/html; charset=UTF-8");
- response.setContentType("audio/mp3");
- try {
- String imageUrl = "http://tts.baidu.com/text2audio?lan=zh&pid=101&ie=UTF-8&text=" + URLEncoder.encode(speekstr, "UTF-8") + "&spd="+spd;
- URL url = new URL(imageUrl);
- LOGGER.info(imageUrl);
- //打开网络输入流
- DataInputStream dis=new DataInputStream(url.openStream());
- int i = dis.available();
- byte[] data = new byte[i];
- dis.read(data);
- dis.close();
- OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
- outputStream.write(data);
- outputStream.flush();
- outputStream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
- <META HTTP-EQUIV="pragma" CONTENT="no-cache">
- <%
- response.setHeader("Cache-Control", "no-store");
- response.setHeader("Pragma","no-cache");
- response.setDateHeader ("Expires", 0);
- %>
- <script src="${root}/assets/js/jquery-1.9.1.js"></script>
- <%
- String root = request.getContextPath();
- request.setAttribute("root", root);
- %>
- <script>
- var audio;
- window.onload = function () {
- initAudio();
- }
- var initAudio = function () {
- audio = document.getElementById('audio');
- }
- function getCurrentTime(id) {
- alert(parseInt(audio.currentTime) + ':秒');
- }
- function downloades(text, id,i) {
- if(i==1){
- document.getElementById(id).innerHTML = '<a href="${root}/musicdiv/' + text + '/6" id="'+id+'"
- οnclick="movethis(this);" download="' + text + '.mp3" style="color: blueviolet">下载语音</a>';
- }else if(i=2){
- document.getElementById(id).innerHTML = '<a href="' + text + '" id="'+id+'" οnclick="movethis(this);"
- download="music.mp3" style="color: blueviolet">下载语音</a>';
- }
- }
- function movethis(obj) {
- obj.innerHTML = '';
- }
- function playOrPaused(text, obj) {
- if (audio.paused) {
- audio.src = text;
- audio.play();
- obj.innerHTML = '暂停';
- return;
- }
- audio.pause();
- obj.innerHTML = '播放';
- }
-
- function hideOrShowControls(id, obj) {
- if (audio.controls) {
- audio.removeAttribute('controls');
- obj.innerHTML = '显示控制框'
- return;
- }
- audio.controls = 'controls';
- obj.innerHTML = '隐藏控制框'
- return;
- }
- function vol(id, type, obj) {
- if (type == 'up') {
- var volume = audio.volume + 0.1;
- if (volume >= 1) {
- volume = 1;
-
- }
- audio.volume = volume;
- } else if (type == 'down') {
- var volume = audio.volume - 0.1;
- if (volume <= 0) {
- volume = 0;
- }
- audio.volume = volume;
- }
- document.getElementById('nowVol').innerHTML = returnFloat1(audio.volume);
- }
- function muted(id, obj) {
- if (audio.muted) {
- audio.muted = false;
- obj.innerHTML = '开启静音';
- } else {
- audio.muted = true;
- obj.innerHTML = '关闭静音';
- }
- }
- //保留一位小数点
-
- function returnFloat1(value) {
- value = Math.round(parseFloat(value) * 10) / 10;
- if (value.toString().indexOf(".") < 0) {
- value = value.toString() + ".0";
- }
- return value;
- }
- </script>
@RequestMapping(value = "/musicdiv/{speekstr}/{spd}", method = RequestMethod.GET)
@PathVariable("speekstr") String speekstr, @PathVariable("spd") Integer spd后台访问方式及参数
- <a href="javascript:void(0);" οnclick="playOrPaused('${root}/musicdiv/Chinese painting 国画/6',audio);"
- download="Chinese painting 国画.mp3" style="color: purple">播放</a>
- <a href="javascript:void(0);" οnclick="downloades('Chinese painting 国画','guohua',1)" style="color: green;">去下载</a>
- <span id="guohua"></span>
- <br>
- <span>Chinese painting 国画 .mp3</span>
- <br>
- <a href="javascript:void(0);" οnclick="getCurrentTime('firefox');" style="color: aqua">获取播放时间</a>
- <a href="javascript:void(0);" οnclick="hideOrShowControls('firefox',this);" style="color: aqua">隐藏控制框</a>
- <a href="javascript:void(0);" οnclick="muted('firefox',this);" style="color: aqua">开启静音</a>
- <input type="button" value="+" id="upVol" οnclick="vol('firefox' , 'up' , this )"
- style="color: aqua"/>音量<input type="button" value="-"
- οnclick="vol('firefox' , 'down' ,this )" style="color: aqua"/>
- <br>
- 当前音量:<span id="nowVol"> - </span>
- <br>
- <audio style="height: 20px;width: 200px;" src="${root}/musicdiv/欢迎您使用本网站语音/6"
- id="audio" controls="controls" download="ceshi.mp3"></audio>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。