当前位置:   article > 正文

百度api语音_function vol

function vol

单纯的页面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大家会玩吧,算了,代码都贴整齐,先开始是做验证码输出的直接拿来修改的,

  1. response.setCharacterEncoding("UTF-8");
  2. response.setHeader("contentType", "text/html; charset=UTF-8");
  3. response.setContentType("audio/mp3");
  4. try {
  5. String imageUrl = "http://tts.baidu.com/text2audio?lan=zh&pid=101&ie=UTF-8&text=" + URLEncoder.encode(speekstr, "UTF-8") + "&spd="+spd;
  6. URL url = new URL(imageUrl);
  7. LOGGER.info(imageUrl);
  8. //打开网络输入流
  9. DataInputStream dis=new DataInputStream(url.openStream());
  10. int i = dis.available();
  11. byte[] data = new byte[i];
  12. dis.read(data);
  13. dis.close();
  14. OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
  15. outputStream.write(data);
  16. outputStream.flush();
  17. outputStream.close();
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
 

  1. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  2. <META HTTP-EQUIV="pragma" CONTENT="no-cache">
  3. <%
  4. response.setHeader("Cache-Control", "no-store");
  5. response.setHeader("Pragma","no-cache");
  6. response.setDateHeader ("Expires", 0);
  7. %>
  8. <script src="${root}/assets/js/jquery-1.9.1.js"></script>
  9. <%
  10. String root = request.getContextPath();
  11. request.setAttribute("root", root);
  12. %>
  13. <script>
  14. var audio;
  15. window.onload = function () {
  16. initAudio();
  17. }
  18. var initAudio = function () {
  19. audio = document.getElementById('audio');
  20. }
  21. function getCurrentTime(id) {
  22. alert(parseInt(audio.currentTime) + ':秒');
  23. }
  24. function downloades(text, id,i) {
  25. if(i==1){
  26. document.getElementById(id).innerHTML = '<a href="${root}/musicdiv/' + text + '/6" id="'+id+'"
  27. οnclick="movethis(this);" download="' + text + '.mp3" style="color: blueviolet">下载语音</a>';
  28. }else if(i=2){
  29. document.getElementById(id).innerHTML = '<a href="' + text + '" id="'+id+'" οnclick="movethis(this);"
  30. download="music.mp3" style="color: blueviolet">下载语音</a>';
  31. }
  32. }
  33. function movethis(obj) {
  34. obj.innerHTML = '';
  35. }
  36. function playOrPaused(text, obj) {
  37. if (audio.paused) {
  38. audio.src = text;
  39. audio.play();
  40. obj.innerHTML = '暂停';
  41. return;
  42. }
  43. audio.pause();
  44. obj.innerHTML = '播放';
  45. }
  46. function hideOrShowControls(id, obj) {
  47. if (audio.controls) {
  48. audio.removeAttribute('controls');
  49. obj.innerHTML = '显示控制框'
  50. return;
  51. }
  52. audio.controls = 'controls';
  53. obj.innerHTML = '隐藏控制框'
  54. return;
  55. }
  56. function vol(id, type, obj) {
  57. if (type == 'up') {
  58. var volume = audio.volume + 0.1;
  59. if (volume >= 1) {
  60. volume = 1;
  61. }
  62. audio.volume = volume;
  63. } else if (type == 'down') {
  64. var volume = audio.volume - 0.1;
  65. if (volume <= 0) {
  66. volume = 0;
  67. }
  68. audio.volume = volume;
  69. }
  70. document.getElementById('nowVol').innerHTML = returnFloat1(audio.volume);
  71. }
  72. function muted(id, obj) {
  73. if (audio.muted) {
  74. audio.muted = false;
  75. obj.innerHTML = '开启静音';
  76. } else {
  77. audio.muted = true;
  78. obj.innerHTML = '关闭静音';
  79. }
  80. }
  81. //保留一位小数点
  82. function returnFloat1(value) {
  83. value = Math.round(parseFloat(value) * 10) / 10;
  84. if (value.toString().indexOf(".") < 0) {
  85. value = value.toString() + ".0";
  86. }
  87. return value;
  88. }
  89. </script>

@RequestMapping(value = "/musicdiv/{speekstr}/{spd}", method = RequestMethod.GET)
@PathVariable("speekstr") String speekstr,
@PathVariable("spd") Integer spd
后台访问方式及参数

  1. <a href="javascript:void(0);" οnclick="playOrPaused('${root}/musicdiv/Chinese painting 国画/6',audio);"
  2. download="Chinese painting 国画.mp3" style="color: purple">播放</a>
  3. <a href="javascript:void(0);" οnclick="downloades('Chinese painting 国画','guohua',1)" style="color: green;">去下载</a>
  4. <span id="guohua"></span>
  5. <br>
  6. <span>Chinese painting 国画 .mp3</span>
  7. <br>
  8. <a href="javascript:void(0);" οnclick="getCurrentTime('firefox');" style="color: aqua">获取播放时间</a>
  9. <a href="javascript:void(0);" οnclick="hideOrShowControls('firefox',this);" style="color: aqua">隐藏控制框</a>
  10. <a href="javascript:void(0);" οnclick="muted('firefox',this);" style="color: aqua">开启静音</a>
  11. <input type="button" value="+" id="upVol" οnclick="vol('firefox' , 'up' , this )"
  12. style="color: aqua"/>音量<input type="button" value="-"
  13. οnclick="vol('firefox' , 'down' ,this )" style="color: aqua"/>
  14. <br>
  15. 当前音量:<span id="nowVol"> - </span>
  16. <br>
  17. <audio style="height: 20px;width: 200px;" src="${root}/musicdiv/欢迎您使用本网站语音/6"
  18. id="audio" controls="controls" download="ceshi.mp3"></audio>


生成文件tu

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号