SpeechPlus.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. function SpeechPlus(options) {
  2. this._init(options);
  3. }
  4. SpeechPlus.prototype = {
  5. _init: function (options) {
  6. var that = this;
  7. this.recogniz = document.getElementById(options.recogniz);
  8. debugger
  9. this.partial = document.getElementById(options.partial);
  10. this.volume = document.getElementById(options.volume);
  11. this.done = document.getElementById(options.done);
  12. this.done.addEventListener('touchstart', function () {
  13. plus.speech.startRecognize({
  14. engine: 'baidu',
  15. lang: 'zh-cn',
  16. 'userInterface': false,
  17. 'continue': true
  18. });
  19. that.recogniz.style.visibility = 'visible';
  20. }, false);
  21. this.done.addEventListener('touchend', function () {
  22. plus.speech.stopRecognize();
  23. that.recogniz.style.visibility = 'hidden';
  24. }, false);
  25. this.text = '';
  26. },
  27. initSpeech: function (callback) {
  28. var that = this;
  29. plus.speech.addEventListener('start', function () {
  30. console.log('Event: start');
  31. that.text = null;
  32. that.partial.innerText = '';
  33. }, false);
  34. plus.speech.addEventListener('volumeChange', function (e) {
  35. that.volume.style.width = 100 * e.volume + 'px';
  36. }, false);
  37. plus.speech.addEventListener('recognizing', function (e) {
  38. console.log('Event: recognizing');
  39. that.partial.innerText = e.partialResult;
  40. }, false);
  41. plus.speech.addEventListener('recognition', function (e) {
  42. console.log('Event: recognition');
  43. that.text ? (that.text += '\n') : that.text = '';
  44. that.text += e.result;
  45. that.partial.innerText = e.result;
  46. callback(that.text);
  47. }, false);
  48. plus.speech.addEventListener('end', function () {
  49. console.log('Event: end');
  50. if (!that.text || that.text == '') {
  51. plus.nativeUI.toast('没有识别到内容');
  52. }
  53. }, false);
  54. },
  55. doStart: function (recogniz) {
  56. plus.speech.startRecognize({
  57. engine: 'baidu',
  58. lang: 'zh-cn',
  59. 'userInterface': false,
  60. 'continue': true
  61. });
  62. recogniz.style.visibility = 'visible';
  63. },
  64. doEnd: function () {
  65. plus.speech.stopRecognize();
  66. this.recogniz.style.visibility = 'hidden';
  67. },
  68. startRecognizeSilent: function () {
  69. var options = {
  70. userInterface: false
  71. };
  72. plus.speech.startRecognize(options, function (s) {
  73. console.log('success: ' + s);
  74. }, function (e) {
  75. console.log('error: ' + JSON.stringify(e));
  76. });
  77. }
  78. }