VolumeTTS.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. class VolumeTTS {
  2. ttsParams = {
  3. lang: "zh-CN",
  4. speed: 1, // 将 0-10 转换为 0-1
  5. pitch: 1, // 将 0-10 转换为 0-1
  6. volume: 1,
  7. };
  8. TTSModule;
  9. customOpts = null; // 自定义的opts参数
  10. platform = uni.getSystemInfoSync().platform;
  11. constructor(opts) {
  12. let audioConfig = uni.getStorageSync("audioConfig");
  13. if (opts) {
  14. this.ttsParams = opts;
  15. } else if (audioConfig) {
  16. this.ttsParams = {
  17. lang: "zh-CN",
  18. speed: audioConfig.audioSpeed / 10,
  19. pitch: audioConfig.audioVolume,
  20. volume: 1,
  21. };
  22. }
  23. // #ifdef APP-PLUS
  24. this.init();
  25. // #endif
  26. }
  27. init() {
  28. this.TTSModule = uni.requireNativePlugin("nrb-tts-plugin");
  29. this.TTSModule &&
  30. this.TTSModule.init(
  31. {
  32. lang: "zh",
  33. country: "CN",
  34. },
  35. (res) => {
  36. if (res.success == 0) {
  37. console.log("TTS初始化成功");
  38. }
  39. }
  40. );
  41. }
  42. //加一个text的预处理,将text中的数字替换成 零一二三四五六七八九十
  43. preprocessText(text) {
  44. // 将text中的数字替换成 零一二三四五六七八九十
  45. const digitMap = {
  46. 0: "零",
  47. 1: "一",
  48. 2: "二",
  49. 3: "三",
  50. 4: "四",
  51. 5: "五",
  52. 6: "六",
  53. 7: "七",
  54. 8: "八",
  55. 9: "九",
  56. };
  57. return text.replace(/\d+/g, (match) => {
  58. // 逐字符替换所有数字,包括10
  59. return match
  60. .split("")
  61. .map((digit) => digitMap[digit] || digit)
  62. .join("");
  63. });
  64. }
  65. /**
  66. * 设置自定义的opts参数
  67. * @param {Object} opts - 自定义的opts参数
  68. */
  69. setCustomOpts(opts) {
  70. this.customOpts = opts;
  71. }
  72. /**
  73. * 获取当前的自定义opts参数
  74. * @returns {Object|null} 当前的自定义opts参数
  75. */
  76. getCustomOpts() {
  77. return this.customOpts;
  78. }
  79. /**
  80. * 清除自定义的opts参数,恢复默认行为
  81. */
  82. clearCustomOpts() {
  83. this.customOpts = null;
  84. }
  85. speak(text, customOpts = null) {
  86. text = this.preprocessText(text);
  87. // #ifdef H5
  88. // H5端使用Web Speech API
  89. if ("speechSynthesis" in window) {
  90. const utterance = new SpeechSynthesisUtterance(text);
  91. utterance.lang = this.ttsParams.lang;
  92. utterance.rate = this.ttsParams.speed;
  93. utterance.pitch = this.ttsParams.pitch;
  94. utterance.volume = this.ttsParams.volume;
  95. speechSynthesis.speak(utterance);
  96. }
  97. // #endif
  98. // #ifdef APP-PLUS
  99. let opts;
  100. // 优先级:方法参数传入的customOpts > setCustomOpts设置的customOpts > 默认opts
  101. if (customOpts) {
  102. opts = customOpts;
  103. } else if (this.customOpts) {
  104. opts = this.customOpts;
  105. } else {
  106. // 使用默认的opts逻辑
  107. opts = this.platform == "ios"
  108. ? {
  109. rate: this.ttsParams.speed,
  110. lang: "zh-CN",
  111. volume: 1,
  112. }
  113. : {
  114. pitch: this.ttsParams.pitch,
  115. speechRate: this.ttsParams.speed,
  116. queueMode: 1,
  117. };
  118. }
  119. this.TTSModule.speak(text, opts, (e) => {
  120. console.log(e, "读取成功");
  121. });
  122. // #endif
  123. }
  124. stop() {
  125. if (this.TTSModule) {
  126. this.TTSModule.stop();
  127. }
  128. }
  129. destroy() {
  130. // 停止当前的语音合成
  131. this.stop();
  132. // #ifdef H5
  133. // H5端取消所有语音合成
  134. if ("speechSynthesis" in window) {
  135. speechSynthesis.cancel();
  136. }
  137. // #endif
  138. // 清理TTSModule引用
  139. this.TTSModule = null;
  140. // 清理自定义opts
  141. this.customOpts = null;
  142. // 重置参数
  143. this.ttsParams = {
  144. lang: "zh-CN",
  145. speed: 1,
  146. pitch: 1,
  147. volume: 1,
  148. };
  149. }
  150. }
  151. export default VolumeTTS;