Uniapp 小程序:语音播放与停息功能的实现及优化方案

打印 上一主题 下一主题

主题 860|帖子 860|积分 2580

界面部分

  1. //开启语音
  2. <button class="open" v-if="showPlay==false" @click="playText">这是开启播放的图片</button >
  3. //关闭语音
  4. <button class="close" v-if="showPlay==true" @click="stopText">这是关闭播放的图片</button >
复制代码
播放语音方法 playText

该方法用于处置惩罚语音播放的逻辑,包罗内容截断、分割、重试机制等。
  1. playText: function() {
  2.     // 检查是否正在等待响应,如果是则提示用户等待
  3.     // 切换显示为正在播放状态
  4.     this.showPlay = true;
  5.     // 初始化重试次数
  6.     let retryCount = 0;
  7.     // 设置最大重试次数
  8.     const maxRetryTimes = 3;
  9.     // 重试间隔时间(单位:毫秒)
  10.     const retryInterval = 2000;
  11.     // 假设最大允许的内容长度,可根据实际调整
  12.     const maxContentLength = 100;
  13.     // 截断内容的函数,如果内容长度超过最大允许长度,则截取前 maxContentLength 个字符
  14.     const truncateContent = (content) => {
  15.         if (content.length > maxContentLength) {
  16.             return content.slice(0, maxContentLength);
  17.         }
  18.         return content;
  19.     };
  20.     // 分割内容的函数,将长内容按合适的分割符号(。!,?)分割成多个部分
  21.     const splitContent = (content) => {
  22.         const parts = [];
  23.         let start = 0;
  24.         while (start < content.length) {
  25.             let end = start + maxContentLength;
  26.             if (end >= content.length) {
  27.                 parts.push(content.slice(start));
  28.                 break;
  29.             }
  30.             // 从后往前查找合适的分割符号(。!,?)
  31.             let symbolIndices = [content.lastIndexOf('。', end), content.lastIndexOf('!', end), content
  32.                .lastIndexOf('?', end), content.lastIndexOf(',', end)
  33.             ];
  34.             symbolIndices = symbolIndices.filter(index => index > start);
  35.             if (symbolIndices.length > 0) {
  36.                 end = Math.max(...symbolIndices);
  37.             }
  38.             parts.push(content.slice(start, end));
  39.             start = end + 1;
  40.         }
  41.         return parts;
  42.     };
  43.     // 播放分割后的内容部分的函数
  44.     const playParts = (parts) => {
  45.         let index = 0;
  46.         const playNextPart = () => {
  47.             if (index < parts.length) {
  48.                 const part = parts[index];
  49.                 const plugin = requirePlugin('WechatSI');
  50.                 // 调用文字转语音插件
  51.                 plugin.textToSpeech({
  52.                     lang: 'zh_CN',
  53.                     tts: true,
  54.                     content: part,
  55.                     success: (res) => {
  56.                         // 创建内部音频上下文并播放音频
  57.                         this.innerAudioContext = uni.createInnerAudioContext();
  58.                         this.innerAudioContext.src = res.filename;
  59.                         this.innerAudioContext.play();
  60.                         // 监听音频播放结束事件,播放下一部分
  61.                         this.innerAudioContext.onEnded(() => {
  62.                             index++;
  63.                             playNextPart();
  64.                         });
  65.                     },
  66.                     fail: (res) => {
  67.                         console.log('文字转语音失败', res);
  68.                         if (retryCount < maxRetryTimes) {
  69.                             retryCount++;
  70.                             console.log(`正在进行第${retryCount}次重试...`);
  71.                             // 重试播放
  72.                             setTimeout(() => playNextPart(), retryInterval);
  73.                         } else {
  74.                             console.log('已达到最大重试次数,文字转语音仍失败');
  75.                         }
  76.                     }
  77.                 });
  78.             } else {
  79.                 // 所有部分播放完毕,切换显示为可播放状态
  80.                 this.showPlay = false;
  81.             }
  82.         };
  83.         playNextPart();
  84.     };
  85.     // 截断内容
  86.     const truncatedContent = truncateContent(this.readContent);
  87.     // 分割内容
  88.     const splitContents = splitContent(this.readContent);
  89.     if (splitContents.length > 1) {
  90.         // 如果分割后的内容部分大于 1,则按部分播放
  91.         playParts(splitContents);
  92.     } else {
  93.         // 内容较短,直接播放
  94.         const retryFn = () => {
  95.             if (retryCount < maxRetryTimes) {
  96.                 retryCount++;
  97.                 console.log(`正在进行第${retryCount}次重试...`);
  98.                 const plugin = requirePlugin('WechatSI');
  99.                 // 调用文字转语音插件
  100.                 plugin.textToSpeech({
  101.                     lang: 'zh_CN',
  102.                     tts: true,
  103.                     content: truncatedContent,
  104.                     success: (res) => {
  105.                         // 创建内部音频上下文并播放音频
  106.                         this.innerAudioContext = uni.createInnerAudioContext();
  107.                         this.innerAudioContext.src = res.filename;
  108.                         this.innerAudioContext.play();
  109.                     },
  110.                     fail: (res) => {
  111.                         console.log('文字转语音失败', res);
  112.                         // 重试播放
  113.                         setTimeout(retryFn, retryInterval);
  114.                     }
  115.                 });
  116.             } else {
  117.                 console.log('已达到最大重试次数,文字转语音仍失败');
  118.             }
  119.         };
  120.         const plugin = requirePlugin('WechatSI');
  121.         // 调用文字转语音插件
  122.         plugin.textToSpeech({
  123.             lang: 'zh_CN',
  124.             tts: true,
  125.             content: truncatedContent,
  126.             success: (res) => {
  127.                 // 创建内部音频上下文并播放音频
  128.                 this.innerAudioContext = uni.createInnerAudioContext();
  129.                 this.innerAudioContext.src = res.filename;
  130.                 this.innerAudioContext.play();
  131.             },
  132.             fail: (res) => {
  133.                 console.log('文字转语音失败', res);
  134.                 // 重试播放
  135.                 setTimeout(retryFn, retryInterval);
  136.             }
  137.         });
  138.     }
  139. }
复制代码
停息语音方法 stopText

该方法用于停止语音播放并开释资源。
  1. stopText() {
  2.     // 切换显示为可播放状态
  3.     this.showPlay = false;
  4.     if (this.innerAudioContext) {
  5.         // 停止播放
  6.         this.innerAudioContext.stop();
  7.         // 释放资源
  8.         this.innerAudioContext = null;
  9.     }
  10. }
复制代码
页面埋伏和卸载时的处置惩罚

在页面埋伏和卸载时,调用 stopText 方法停止语音播放。
  1. onHide() {
  2.     this.stopText()
  3. },
  4. onUnload() {
  5.     this.stopText()
  6. }
复制代码
总结
这段代码实现了语音播放和停息的功能,通过界面上的按钮触发相应的操纵。在播放语音时,会对内容举行截断和分割处置惩罚,以适应笔墨转语音插件的要求。同时,为了进步稳定性,添加了重试机制。在页面埋伏和卸载时,会自动停止语音播放并开释资源。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

没腿的鸟

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表