uniapp播放声音出错的解决办法:1、打开相应的代码文件,然后直接执行对象play方法,替换原来的src路径;2、更新开发工具版本即可。
本教程操作环境:windows7系统、uni-app2.5.1版本,DELL G3电脑。
uni-app 语音文件播放问题,innerAudioContext 无法销毁 !!!
uni-app 语音createInnerAudioContext 对象 无法销毁 还闪退!!!
今天打包发现语音无法播放了,于是找了下原因,记得这个问题已经处理过了,所以很快的找到了问题,项目用错了,用的原来的进行打包,这个错误真的是不可原谅。
特此今天记录一下语音无法播放的错误。 语音播放 用的内置的 uni.createInnerAudioContext(); 来创建的,播放就报错,还闪退。
贴下代码,我是这么写的。当调取到当前的方法,直接就将代码贴上吭,这是错误的,可别直接复制,兄dei。
<script> const innerAudioContext = uni.createInnerAudioContext();data() { return {```}; }, methods: { hechengAudio(audioPram) { // innerAudioContext.stop(); var dd = audioPram.replace(/<\/?.+?>/g, ""); audioPram = dd.replace(/ /g, ""); //dds为得到后的内容 if (audioPram != this.startAudio) { this.startPage = false; } var url = this.getMp3 + 'cuid=' + uni.getSystemInfoSync().version + '&lan=zh&ctp=1&tok=' + this.$token() + '&tex=' + audioPram + '&vol=5&per=0&spd=6&pit=5&aue=3'; // console.log(JSON.stringify(resSave)); console.log(url); // #ifdef APP-PLUS // 下面这两句是重点, 拿本本记下.............. if (innerAudioContext != undefined) { innerAudioContext.stop(); } innerAudioContext = uni.createInnerAudioContext(); // 上面这两句是重点, 拿本本记下.............. this.pageValue = this.value; console.log('src是空的吗 -----------' + url) innerAudioContext.stop(); innerAudioContext.src = url; innerAudioContext.play();// if (url != '') {// innerAudioContext.play();// } else {// console.log('src是空的吗 -----------' + url)// uni.showToast({// title: 'src是空的 不能执行',// mask: true,// duration: 2000,// icon: "none"// });// // return// } innerAudioContext.onPlay(() => { console.log('开始播放') }) innerAudioContext.onStop(() => { console.log('i am onStop') //播放停止,销毁该实例 // innerAudioContext.stop() }) innerAudioContext.onEnded(() => { console.log('i am onEnded') //播放结束,销毁该实例 // innerAudioContext.stop() console.log('已执行destory()') }) innerAudioContext.onError((res) => { console.log(result.errMsg) console.log(result.errCode) // innerAudioContext.stop() }) // #endif // #ifdef MP-WEIXIN var that = this; uni.downloadFile({ url: url, success(res) { if (innerAudioContext != undefined) { innerAudioContext.stop(); } innerAudioContext = uni.createInnerAudioContext(); that.pageValue = that.value; innerAudioContext.src = res.tempFilePath; innerAudioContext.play(); innerAudioContext.onPlay(() => { console.log('开始播放') }) innerAudioContext.onStop(() => { console.log('i am onStop') innerAudioContext.stop() //播放停止,销毁该实例 innerAudioContext.stop() }) innerAudioContext.onEnded(() => { console.log('i am onEnded') //播放结束,销毁该实例 innerAudioContext.stop() console.log('已执行destory()') }) innerAudioContext.onError((res) => { console.log(result.errMsg) console.log(result.errCode) innerAudioContext.stop() }) } }) // #endif }, }, }</script>
看到上面的两行代码 ,记住千万不要销毁了,直接 执行对象play 方法就好。 这样用的都是用一个对象,他这样会默认替换原来的src 路径,可以直接播放.
还有一个原因是,开发工具版本太老,我现在版本是 1.6.2 ,主要是这个版本流程,问题少。
下面代码可以复制
<script> const innerAudioContext = uni.createInnerAudioContext();data() { return {```}; }, methods: { hechengAudio(audioPram) { // innerAudioContext.stop(); var dd = audioPram.replace(/<\/?.+?>/g, ""); audioPram = dd.replace(/ /g, ""); //dds为得到后的内容 if (audioPram != this.startAudio) { this.startPage = false; } var url = this.getMp3 + 'cuid=' + uni.getSystemInfoSync().version + '&lan=zh&ctp=1&tok=' + this.$token() + '&tex=' + audioPram + '&vol=5&per=0&spd=6&pit=5&aue=3'; // console.log(JSON.stringify(resSave)); console.log(url); // #ifdef APP-PLUS this.pageValue = this.value; console.log('src是空的吗 -----------' + url) innerAudioContext.stop(); innerAudioContext.src = url; innerAudioContext.play(); innerAudioContext.onPlay(() => { console.log('开始播放') }) innerAudioContext.onStop(() => { console.log('i am onStop') //播放停止,销毁该实例 // innerAudioContext.stop() }) innerAudioContext.onEnded(() => { console.log('i am onEnded') //播放结束,销毁该实例 // innerAudioContext.stop() console.log('已执行destory()') }) innerAudioContext.onError((res) => { console.log(result.errMsg) console.log(result.errCode) // innerAudioContext.stop() }) // #endif // #ifdef MP-WEIXIN var that = this; uni.downloadFile({ url: url, success(res) { if (innerAudioContext != undefined) { innerAudioContext.stop(); } innerAudioContext = uni.createInnerAudioContext(); that.pageValue = that.value; innerAudioContext.src = res.tempFilePath; innerAudioContext.play(); innerAudioContext.onPlay(() => { console.log('开始播放') }) innerAudioContext.onStop(() => { console.log('i am onStop') innerAudioContext.stop() //播放停止,销毁该实例 innerAudioContext.stop() }) innerAudioContext.onEnded(() => { console.log('i am onEnded') //播放结束,销毁该实例 innerAudioContext.stop() console.log('已执行destory()') }) innerAudioContext.onError((res) => { console.log(result.errMsg) console.log(result.errCode) innerAudioContext.stop() }) } }) // #endif }, }, }</script>
推荐:《uniapp教程》
以上是如何解决uniapp播放声音出错问题的详细内容。更多信息请关注PHP中文网其他相关文章!

本文讨论了有关移动和网络平台的调试策略,突出显示了Android Studio,Xcode和Chrome DevTools等工具,以及在OS和性能优化的一致结果的技术。

文章讨论了用于Uniapp开发的调试工具和最佳实践,重点关注Hbuilderx,微信开发人员工具和Chrome DevTools等工具。

本文讨论了跨多个平台的Uniapp应用程序的端到端测试。它涵盖定义测试方案,选择诸如Appium和Cypress之类的工具,设置环境,写作和运行测试,分析结果以及集成

本文讨论了针对Uniapp应用程序的各种测试类型,包括单元,集成,功能,UI/UX,性能,跨平台和安全测试。它还涵盖了确保跨平台兼容性,并推荐Jes等工具

本文讨论了UNIAPP开发中的共同绩效抗模式,例如过度的全球数据使用和效率低下的数据绑定,并提供策略来识别和减轻这些问题,以提高应用程序性能。

本文讨论了通过压缩,响应式设计,懒惰加载,缓存和使用WebP格式来优化Uniapp中的图像,以更好地进行Web性能。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver Mac版
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),