前言

用vue 做音乐播放的时候,在本地可以打开播放,但在微信里面不能播放音乐

所以这样解决

  1. // 音乐播放
  2. audioPlay(){
  3. let _this = this;
  4. var audio = _this.music;
  5. if(_this.bgmUrl){
  6. audio.src = _this.bgmUrl;
  7. //audio.play();
  8. _this.esPlayMusic();
  9. _this.timeupdate();
  10. wx.ready(()=>{
  11. })
  12. typeof WeixinJSBridge !== 'undefined' && WeixinJSBridge.invoke('getNetworkType', {}, function (res) {
  13. //audio.play();
  14. _this.esPlayMusic();
  15. _this.timeupdate();
  16. });
  17. }
  18. },
  19. timeupdate() {
  20. let _this = this;
  21. var audio = _this.music;
  22. audio.addEventListener("timeupdate", function() {
  23. var duration = this.duration;
  24. var currentTime = this.currentTime;
  25. var percentage = (currentTime / duration) * 100;
  26. if (percentage == 100) {
  27. _this.audioPlay();
  28. typeof WeixinJSBridge !== 'undefined' && WeixinJSBridge.invoke('getNetworkType', {}, function (res) {
  29. _this.audioPlay();
  30. });
  31. }
  32. })
  33. },

主要是这段代码

  1. typeof WeixinJSBridge !== 'undefined' && WeixinJSBridge.invoke('getNetworkType', {}, function (res) {
  2. //audio.play();
  3. _this.esPlayMusic();
  4. _this.timeupdate();
  5. });

后来又因为   点击过快 出现报错了

The play() request was interrupted by a call to pause()

因为上一个用户的播报还没结束,这一个播报就要覆盖上来了,所以需要异步处理下

上代码了

  1. // 异步加载音乐播放
  2. esPlayMusic(){
  3. let _this = this;
  4. //let audio = document.getElementById("audioPlay");
  5. var audio = _this.music;
  6. //audio.play();
  7. var playPromise = audio.play();
  8. if (playPromise) {
  9. playPromise.then(() => {
  10. // 音频加载成功
  11. // 音频的播放需要耗时
  12. setTimeout(() => {
  13. // 后续操作
  14. //console.log("done");
  15. }, audio.duration * 1000); // audio.duration 为音频的时长单位为秒
  16.  
  17. }).catch((e) => {
  18. //console.log("Operation is too fast, audio play fails");
  19. });
  20. }
  21. },

这样就解决了

关于 Vue 微信客户端 不能播放音乐(报错和不能播放的问题)的更多相关文章

  1. 新版uni-app 在微信小工具调试遇到报错解决方案

    问题描述:我在运行到微信小程序是运行报错打不开微信小程序报错如下图 结局方案:将微信小程序安全设置开启如下图

  2. vue 使用webpack打包后路径报错以及 alias 的使用

    一.vue 使用webpack打包后路径报错(两步解决) 1. config文件夹 ==> index.js ==> 把assetsPublicPath的 '/ '改为 './' 2. b ...

  3. Vue学习笔记-vue-element-admin 按装报错再按装

    一  使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7  (Windows x86- ...

  4. vue 音乐播放器报错

    使用Vue报错[Vue warn]: Error in nextTick: "TypeError: fn.bind is not a function"页面进不去. 检查:看看da ...

  5. 谷歌浏览器不能播放audio 报错Uncaught (in promise) DOMException

    在2018年4月份发布的Chrome 66正式关掉了声音自动播放,也就是说<audio autopaly></audio> <video autoplay>< ...

  6. Vue入门之旅:一报错 Unknown ... make sure to provide the "name" option及error compiling template

    报错一: Unknown custom element: <custom-select> - did you register the component correctly? For r ...

  7. vue安装遇到的5个报错小结

    前言 这篇博文不会教你怎么安装vue,但会告知安装过程中可能遇到的5个问题 2017年我写过一篇安装vue的博客,详情:https://www.cnblogs.com/tu-0718/p/752109 ...

  8. Nuxt.js vue init nuxt-community/koa-template 初始化项目报错

    报错提示: Module build failed: Error: Plugin/Preset files are not allowed to export objects, only functi ...

  9. Vue 项目中的ESlint语法报错问题

    在项目中的""和;经常会报错,真的很纠结,今天看到一个解决方法,可以不用卸载删除 在项目根目录中,新建一个.prettierrc文件,来移除分号,和替换为单引号. { " ...

随机推荐

  1. Unity3D入门 UnityAPI常用方法和类

    时间函数: 这里只列举了一部分,更多的看Scripting API using System.Collections; using System.Collections.Generic; using ...

  2. 在vue中后台返回的文本包含标签时候解析为html代码

    1.数据格式: str=‘<p>11111</p>' 解析方式一: <p v-html="str">{{str}}</p> 解析方式 ...

  3. C++四种类型转换总结

    C风格的强制类型转换很简单,均用 Type b = (Type)a 形式转换.C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用,如下表: 转换类型操作符 作用 const_cast 去 ...

  4. 有人向你扔了一个bug,哈哈哈哈

    有人向你扔了一个bug. "26楼会议室的灯亮着.它应该是熄灭着的." bug的备注里写道"你应该能在5分钟内搞定,只要按一下开关就好了."你去了26楼的会议室 ...

  5. reids集群状态正常redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException: No reachable node in cluster

    重新启动redis集群时启动失败,报错: redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException: No reachab ...

  6. 使用mvn archetype:generate快速建立Maven项目目录结构

    1.mvn archetype:generate  按照提示进行选择,默认选的话可以直接按回车键 2.mvn archetype:generate -DgroupId=组织名,公司网址的反写+项目名 ...

  7. [Luogu5686] 和积和

    Description 给定两个下标从\(1\)到\(n\)编号的序列 \(a_i,b_i\),定义函数\(S(l,r)(1\le l\le r\le n)\)为: \[\sum_{i=l}^r a_ ...

  8. Python 入门 之 类的约束以及super()剖析

    Python 入门 之 类的约束以及super()剖析 1.类的约束 第一版: class WechatPay: def pay(self): print("微信支付") clas ...

  9. jupyter 配置远程登陆

    官方地址: https://jupyter-notebook.readthedocs.io/en/latest/public_server.html#notebook-server-security ...

  10. 01 Redis基础

    NoSQL 学名(not only sql) 特点: 存储结构与mysql这一种关系型数据库完全不同,nosql存储的是KV形式 nosql有很多产品,都有自己的api和语法,以及业务场景 产品种类: ...