首页 > 编程知识 正文

audio如何设置自动播放,delete unused android studio

时间:2023-05-03 20:54:10 阅读:285347 作者:4566

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-WEIXINvar 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-WEIXINvar 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>

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。