前言

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

所以这样解决

  // 音乐播放
audioPlay(){
let _this = this;
var audio = _this.music;
if(_this.bgmUrl){
audio.src = _this.bgmUrl;
//audio.play();
_this.esPlayMusic();
_this.timeupdate();
wx.ready(()=>{
})
typeof WeixinJSBridge !== 'undefined' && WeixinJSBridge.invoke('getNetworkType', {}, function (res) {
//audio.play();
_this.esPlayMusic();
_this.timeupdate();
});
}
},
timeupdate() {
let _this = this;
var audio = _this.music;
audio.addEventListener("timeupdate", function() {
var duration = this.duration;
var currentTime = this.currentTime;
var percentage = (currentTime / duration) * 100;
if (percentage == 100) {
_this.audioPlay();
typeof WeixinJSBridge !== 'undefined' && WeixinJSBridge.invoke('getNetworkType', {}, function (res) {
_this.audioPlay();
});
}
})
},

主要是这段代码

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

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

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

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

上代码了

// 异步加载音乐播放
esPlayMusic(){
let _this = this;
//let audio = document.getElementById("audioPlay");
var audio = _this.music;
//audio.play();
var playPromise = audio.play();
if (playPromise) {
playPromise.then(() => {
// 音频加载成功
// 音频的播放需要耗时
setTimeout(() => {
// 后续操作
//console.log("done");
}, audio.duration * 1000); // audio.duration 为音频的时长单位为秒 }).catch((e) => {
//console.log("Operation is too fast, audio play fails");
});
}
},

这样就解决了

关于 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. Linux目录权限管理

    Linux目录权限管理   实验目标: 通过本实验掌握centos7/rhel7目录权限的管理.包括配置目录的所属组.SGID.读/写/执行权限等. 实验步骤: 1.创建目录/home/instruc ...

  2. 分布式锁用Redis还是ZooKeeper?(转载)

    文章系网络转载,侵删. 来源:https://zhuanlan.zhihu.com/p/73807097 为什么用分布式锁?在讨论这个问题之前,我们先来看一个业务场景. 图片来自 Pexels 为什么 ...

  3. Java架构师 成长之路 -- 跳出程序员陷阱(转载)

    本人也是coding很多年,虽然很失败,但也总算有点失败的心得,不过我在中国,大多数程序员都是像我一样,在一直走着弯路. 如果想成为一个架构师,就必须走正确的路,否则离目标越来越远,正在辛苦工作的程序 ...

  4. 【STM32】STM32串口配置的一般步骤(库函数)

    STM32串口配置的一般步骤(库函数)(1)串口时钟使能:RCC_APBxPeriphClockCmd();    GPIO时钟使能:RCC_AHBxPeriphClockCmd();(2)引脚复用映 ...

  5. Flood Fill

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  6. List是有序的Set是无序的吗? List和Set对比

    import java.util.*; /* * List和Set对比 * */ public class ListVSSet { public static void main(String[] a ...

  7. codeforces 842C Ilya And The Tree (01背包+dfs)

    (点击此处查看原题) 题目分析 题意:在一个树中,有n个结点,记为 1~n ,其中根结点编号为1,每个结点都有一个值val[i],问从根结点到各个结点的路径中所有结点的值的gcd(最大公约数)最大是多 ...

  8. HDU 6662 Acesrc and Travel 换根DP,宇宙最傻记录

    #include<bits/stdc++.h> typedef long long ll; using namespace std; const int maxn=1e6+50; cons ...

  9. pwd命令和修改PS1环境变量在bash行的显示

    一.pwd:显示当前所在的位置 语法 pwd [选项] ... 描述       打印当前工作目录的完整文件名. -L,--logical              从环境使用PWD,即使它包含符号链 ...

  10. Jquery table相关--工时系统

    1.jquery 的弹出对话框,单击事件之后 if (confirm("确定要删除?")) { // //点击确定后操作 } 2.对某个table中的checkbox是否被选中的遍 ...