vue中音频wavesurfer.js的使用方法


Posted in Vue.js onFebruary 20, 2020

本文为大家分享了vue中音频wavesurfer.js的使用方法,供大家参考,具体内容如下

效果图

vue中音频wavesurfer.js的使用方法

首先引入wavesurfer.js

vue中音频wavesurfer.js的使用方法

其次 封装 wavesurfer.js`

<template>
 <div class="waveformOuter">
 <div id="waveform" ref="waveform" />
 <div class="play" @click="playMusic">
  <svg-icon v-if="!isPlaying" icon-class="play" class="icon myplay" />
  <svg-icon v-else icon-class="stop" class="icon mystop" />
  <p>{{ time }}</p>
 </div>
  <!-- <p>{{ url }}</p> -->
 </div>
</template>
<script>
import WaveSurfer from '@/assets/js/wavesurfer'
export default {
 props: ['url', 'toStopMusic', 'loadWave'],
 data() {
 return {
  isPlaying: false,
  time: '00:00',
  wavesurfer: null,
 }
 },
 watch: {
 'loadWave': function() {
  this.loadMusic()
 },
 'url': function() {
  // return this.wavesurfer.getCurrentTime()
  this.loadMusic(true)
  console.log('url---------------------------------------')
 },
 'toStopMusic': function() {
  console.log('toStopMusic---------------------------------------')
  if (this.wavesurfer) {
  this.wavesurfer.pause()
  }
 }
 },
 mounted() {
 this.$nextTick(() => {
  if (this.loadWave) {
  console.log('haha')
  this.loadMusic()
  }
 })
 },
 methods: {
 itemClick(node) {
  console.log(node.model.id)
 },
 buZero(num) {
  return num > 9 ? num : '0' + num
 },
 loadMusic(bool) {
  console.log('this.WaveSurfer--------------------------------------', WaveSurfer)
  if (this.wavesurfer) {
  if (bool) {
   this.time = '00:00'
   this.wavesurfer.load(this.url)
  }
  } else {
  this.wavesurfer = WaveSurfer.create({
   container: this.$refs.waveform,
   waveColor: 'violet',
   progressColor: 'purple'
  })
  this.wavesurfer.load(this.url)
  this.wavesurfer.on('ready', () => {
   // this.wavesurfer.play()
  })
  this.wavesurfer.on('audioprocess', (e) => {
   const times = Math.floor(e)
   this.time = this.buZero(Math.floor(times / 60)) + ':' + this.buZero(times % 60)
  })
  this.wavesurfer.on('finish', () => {
   this.isPlaying = false
  })
  }
 },
 stopMusic(){
  if (this.wavesurfer) {
   this.wavesurfer.stop();
  }
 },
 playMusic() {
  console.log("开始")
  console.log(this.wavesurfer)
  console.log("点击开始播放按钮",this.url)
  if (this.wavesurfer) {
  if (this.wavesurfer.isPlaying()) {
   this.isPlaying = false
  debugger
   this.wavesurfer.pause()
  } else {
   this.isPlaying = true
   this.wavesurfer.play()
  }
  }
 }
 }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.myplay{
 position: absolute;
 top: 50%;
 font-size: 17px;
 margin: -10px 0 0 -9px;
 left: 50%;
}
.mystop{
 position: absolute;
 top: 50%;
 margin: -15px 0 0 -18px;
 left: 50%;
 font-size: 25px;
}
#waveform{
 width: calc(100% - 150px);
 height:128px;
 float: left;
 margin-right: 22px;
 background: black;
}
.play{
 position: relative;
 width: 128px;
 height:128px;
 border-radius:3px;
 background-color:#EBEEF5;
 float: left;
 text-align: center;
}
.play p{
 margin-top: 85px;
 color: #3683FA;
}

.waveformOuter{
 margin-bottom: 20px;
 overflow: hidden;
}
</style>

再然后 在需要的组件中引入

<div class="luyin" v-if="this.isYinyin">
 <!-- {{this.isYinyin}} -->
 <my-wave-sufer ref="handleDialogClose" :url="luyinUrl" :load-wave="showDialog" :to-stop-music="!showDialog" />
</div>
isYinyin: false,//是否加载录音组件
showDialog: true, //是否初始化录音组件
luyinUrl: "", //录音url 
数据库录音格式 123.wav

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Vue.js 相关文章推荐
vue $router和$route的区别详解
Dec 02 Vue.js
Vue router安装及使用方法解析
Dec 02 Vue.js
详解vue中使用transition和animation的实例代码
Dec 12 Vue.js
如何在VUE中使用vue-awesome-swiper
Jan 04 Vue.js
antdesign-vue结合sortablejs实现两个table相互拖拽排序功能
Jan 08 Vue.js
使用vue3重构拼图游戏的实现示例
Jan 25 Vue.js
Vue 3自定义指令开发的相关总结
Jan 29 Vue.js
关于Vue Router的10条高级技巧总结
May 06 Vue.js
vue实现无缝轮播效果(跑马灯)
May 14 Vue.js
详解Vue的sync修饰符
May 15 Vue.js
vue-cropper组件实现图片切割上传
May 27 Vue.js
vue判断按钮是否可以点击
Apr 09 Vue.js
vue使用vant中的checkbox实现全选功能
Nov 17 #Vue.js
vue+vant实现购物车全选和反选功能
Nov 17 #Vue.js
在vue中通过render函数给子组件设置ref操作
Nov 17 #Vue.js
vue+iview分页组件的封装
Nov 17 #Vue.js
快速解决vue2+vue-cli3项目ie兼容的问题
Nov 17 #Vue.js
vue+iview实现分页及查询功能
Nov 17 #Vue.js
vue中echarts的用法及与elementui-select的协同绑定操作
Nov 17 #Vue.js
You might like
安装PHP可能遇到的问题“无法载入mysql扩展” 的解决方法
2007/04/16 PHP
PHP生成月历代码
2007/06/14 PHP
PHP中设置时区,记录日志文件的实现代码
2013/01/07 PHP
ThinkPHP框架设计及扩展详解
2014/11/25 PHP
jQuery动态添加、删除元素的方法
2014/01/09 Javascript
JavaScript学习笔记之内置对象
2015/01/22 Javascript
jQuery插件实现控制网页元素动态居中显示
2015/03/24 Javascript
深入理解jquery自定义动画animate()
2016/05/24 Javascript
jQuery和CSS仿京东仿淘宝列表导航菜单
2017/01/04 Javascript
详解windows下vue-cli及webpack 构建网站(二)导入bootstrap样式
2017/06/17 Javascript
js canvas实现适用于移动端的百分比仪表盘dashboard
2017/07/18 Javascript
JavaScript中数组常见操作技巧
2017/09/01 Javascript
详解10分钟学会vue滚动行为
2017/09/21 Javascript
微信小程序中使用wxss加载图片并实现动画效果
2018/08/13 Javascript
JS中‘hello’与new String(‘hello’)引出的问题详解
2018/08/14 Javascript
JS数组实现分类统计实例代码
2018/09/30 Javascript
vue组件之间通信方式实例总结【8种方式】
2019/02/22 Javascript
jQuery事件模型默认行为执行顺序及trigger()与 triggerHandler()比较实例分析
2020/04/30 jQuery
[01:08:33]OG vs VGJ.T 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
使用Python对SQLite数据库操作
2017/04/06 Python
PyQt5基本控件使用之消息弹出、用户输入、文件对话框的使用方法
2019/08/06 Python
python实现PID算法及测试的例子
2019/08/08 Python
numpy:找到指定元素的索引示例
2019/11/26 Python
Windows 下更改 jupyterlab 默认启动位置的教程详解
2020/05/18 Python
html2canvas把div保存图片高清图的方法示例
2018/03/05 HTML / CSS
Nike澳大利亚官网:Nike.com (AU)
2019/06/03 全球购物
室内设计专业个人的自我评价
2013/10/19 职场文书
协议书模板
2014/04/23 职场文书
护士节演讲稿开场白
2014/08/25 职场文书
2014领导班子四风剖析对照检查材料思想汇报
2014/09/20 职场文书
离婚协议书范本
2015/01/26 职场文书
工厂清洁工岗位职责
2015/02/14 职场文书
大一学生个人总结
2015/02/15 职场文书
2015年度护士个人工作总结
2015/04/09 职场文书
Tomcat项目启动失败的原因和解决办法
2022/04/20 Servers
Docker安装MySql8并远程访问的实现
2022/07/07 Servers