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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Uni-app 是一款基于 Vue.js 开发的跨平台框架,可以用来开发小程序、App、H5 等多平台应用。在 Uni-app 中,我们可以使用内置的 API 创建文件Uni-app 提供了两种方式在应用中创建文件:使用 downloadFile API 下载文件和使用 FileSystemManager API 创建文件。 1. 使用 downloadFile API 下载文件: downloadFile API 可以用来下载远程文件并保存到本地。我们可以通过调用该 API 来创建文件。具体步骤如下: - 导入 downloadFile API:首先在需要使用该 API 的页面或组件中导入 downloadFile API。 - 调用 downloadFile 方法:通过调用 downloadFile 方法,传入远程文件的 URL 和本地文件路径,即可实现创建文件的功能。 2. 使用 FileSystemManager API 创建文件: FileSystemManager API 是 Uni-app 提供的文件系统管理 API,可以用来进行文件的创建、读取、写入等操作。我们可以通过调用该 API 的 writeFile 方法来创建文件。具体步骤如下: - 导入 FileSystemManager API:首先在需要使用该 API 的页面或组件中导入 FileSystemManager API。 - 调用 writeFile 方法:通过调用 writeFile 方法,传入文件路径和文件内容参数,即可实现创建文件的功能。 无论是使用 downloadFile API 还是 FileSystemManager API,都需要在调用之前获得相应的授权。Uni-app 提供了相关的权限请求 API 来获取相应的权限。 总结起来,Uni-app 中创建文件的主要步骤是导入相应的 API,然后通过调用相应的方法,传入参数来实现文件的创建功能。具体选择哪种方式,可以根据开发者的具体需求来决定。 ### 回答2: 在Uni-app中创建文件非常简单。首先,在项目的根目录下找到需要创建文件的位置。然后,可以通过以下两种方式来创建文件。 1. 使用HBuilderX工具创建文件。在项目根目录下,右键点击要创建文件的目录,选择“新建文件”,然后在弹出的对话框中输入文件名及文件类型,点击确定即可创建文件。例如,如果要创建一个名为"index.js"的JavaScript文件,选择文件类型为JavaScript,然后点击确定即可创建文件。 2. 使用代码创建文件。在项目根目录下找到要创建文件的位置,可以通过编写相应的代码来创建文件。例如,如果要在根目录下创建一个名为"index.js"的JavaScript文件,可以在根目录的main.js文件中添加以下代码: ```javascript // 引入文件系统模块 import fs from 'fs' // 要创建的文件路径 const filePath = uni.env.USER_DATA_PATH + '/index.js' // 要写入的文件内容 const fileContent = 'console.log("Hello, Uni-app!")' // 创建文件并写入内容 fs.writeFile({ filePath: filePath, data: fileContent, encoding: 'utf8', success: function () { console.log('文件创建成功') }, fail: function (error) { console.log('文件创建失败:' + error) } }) ``` 以上是使用uni-app创建文件的方法,根据具体需求选择合适的方式进行创建。创建文件后,可以在相应的位置编写代码或者进行其他操作。 ### 回答3: 要在uni-app中创建文件,可以通过uni.createFile()方法来实现。具体操作步骤如下: 1. 首先,在uni-app的项目中找到要创建文件的页面或组件。 2. 在该页面或组件中,使用uni.createFile()方法来创建文件。这个方法接收一个对象参数,里面包括文件的路径、文件名和文件内容等信息。 3. 在对象中设置文件的路径、文件名和文件内容等信息。可以使用相对路径或绝对路径来指定文件的位置。文件名可以包括后缀名,如.txt、.json等。文件内容可以是字符串或二进制数据等。 4. 调用uni.createFile()方法来创建文件,创建成功后,该方法会返回一个文件对象。 5. 可以通过文件对象的一些方法,如file.path、file.name等来获取文件的路径、文件名等信息。 需要注意的是,创建文件的路径和名称需要在uni-app的相关平台上是可读写的。且在一些平台上,需要用户授权才能进行文件的创建操作。 上述就是使用uni-app创建文件的基本步骤。根据实际需求,可以灵活地进行文件的创建和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值