TTSExample.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import VolumeTTS from './VolumeTTS.js';
  2. /**
  3. * TTS使用示例和最佳实践
  4. */
  5. class TTSExample {
  6. constructor() {
  7. this.tts = null;
  8. this.init();
  9. }
  10. // 初始化TTS
  11. init() {
  12. try {
  13. // 创建TTS实例,可以传入自定义参数
  14. this.tts = new VolumeTTS({
  15. lang: "zh-CN",
  16. speed: 0.8, // 语速稍慢,提高识别率
  17. pitch: 1.0, // 音调正常
  18. volume: 1.0 // 音量最大
  19. });
  20. console.log('TTS实例创建成功');
  21. // 监听TTS状态变化
  22. this._monitorTTSStatus();
  23. } catch (error) {
  24. console.error('TTS初始化失败:', error);
  25. }
  26. }
  27. // 监听TTS状态
  28. _monitorTTSStatus() {
  29. // 定期检查TTS状态
  30. setInterval(() => {
  31. if (this.tts) {
  32. const status = this.tts.getStatus();
  33. console.log('TTS状态:', status);
  34. // 如果TTS不可用且重试次数未达到上限,尝试重新初始化
  35. if (!status.isInitialized && status.retryCount < status.maxRetries) {
  36. console.log('TTS不可用,尝试重新初始化...');
  37. this.tts.init();
  38. }
  39. }
  40. }, 10000); // 每10秒检查一次
  41. }
  42. // 基础播报示例
  43. speakBasic(text) {
  44. if (!this.tts) {
  45. console.error('TTS未初始化');
  46. return;
  47. }
  48. if (!text) {
  49. console.warn('播报文本为空');
  50. return;
  51. }
  52. console.log('开始播报:', text);
  53. this.tts.speak(text);
  54. }
  55. // 播报订单信息
  56. speakOrder(orderInfo) {
  57. if (!orderInfo) return;
  58. const text = `订单号${orderInfo.orderNo},商品${orderInfo.productName},数量${orderInfo.quantity}件`;
  59. this.speakBasic(text);
  60. }
  61. // 播报扫描结果
  62. speakScanResult(barcode, productName) {
  63. const text = `扫描到条码${barcode},商品${productName}`;
  64. this.speakBasic(text);
  65. }
  66. // 播报错误信息
  67. speakError(errorMsg) {
  68. const text = `错误:${errorMsg}`;
  69. this.speakBasic(text);
  70. }
  71. // 播报成功信息
  72. speakSuccess(successMsg) {
  73. const text = `成功:${successMsg}`;
  74. this.speakBasic(text);
  75. }
  76. // 播报数字(会自动转换为中文)
  77. speakNumber(number) {
  78. const text = `数字${number}`;
  79. this.speakBasic(text);
  80. }
  81. // 停止播报
  82. stop() {
  83. if (this.tts) {
  84. this.tts.stop();
  85. }
  86. }
  87. // 销毁TTS实例
  88. destroy() {
  89. if (this.tts) {
  90. this.tts.destroy();
  91. this.tts = null;
  92. }
  93. }
  94. // 获取TTS状态
  95. getStatus() {
  96. return this.tts ? this.tts.getStatus() : null;
  97. }
  98. // 检查TTS是否可用
  99. isAvailable() {
  100. return this.tts ? this.tts.isAvailable() : false;
  101. }
  102. // 设置自定义播报参数
  103. setCustomOptions(opts) {
  104. if (this.tts) {
  105. this.tts.setCustomOpts(opts);
  106. }
  107. }
  108. // 清除自定义参数
  109. clearCustomOptions() {
  110. if (this.tts) {
  111. this.tts.clearCustomOpts();
  112. }
  113. }
  114. }
  115. // 导出单例实例
  116. const ttsExample = new TTSExample();
  117. export default ttsExample;
  118. // 使用示例:
  119. /*
  120. // 在页面中使用
  121. import ttsExample from '@/utils/TTSExample.js';
  122. // 播报文本
  123. ttsExample.speakBasic('欢迎使用书嗨系统');
  124. // 播报订单信息
  125. ttsExample.speakOrder({
  126. orderNo: '12345',
  127. productName: '图书',
  128. quantity: 5
  129. });
  130. // 播报扫描结果
  131. ttsExample.speakScanResult('123456789', '图书名称');
  132. // 播报错误
  133. ttsExample.speakError('网络连接失败');
  134. // 播报成功
  135. ttsExample.speakSuccess('操作完成');
  136. // 播报数字(会自动转换为中文)
  137. ttsExample.speakNumber('123');
  138. // 停止播报
  139. ttsExample.stop();
  140. // 检查状态
  141. const status = ttsExample.getStatus();
  142. console.log('TTS状态:', status);
  143. // 销毁实例(页面销毁时调用)
  144. ttsExample.destroy();
  145. */