zoukankan      html  css  js  c++  java
  • 小程序mpvue 项目 播放语音切换动图

    近期 小程序项目 ,个人简介模块有个语音介绍功能,播放语音时需要 切换gif 动图 来标识正在播放语音,暂停或停止播放时 切换为png 图片。

    刚开始 是定义了两个类,想要通过添加 移除 类名来实现。但是发现在vue中 获取 dom元素时,报错。getElementById  if undefined。

    最后发现 自己想多了。完全可以通过 vue 的 v-if 来实现。先定义一个 变量,在播放状态改变时 只需改变该变量的值即可。

    具体实现 如下:

    1,v-if 判断

     <span v-if="imgActive"  class="voicePlayImg text-xxl padding-left-sm "></span>
     <span v-else  class="voicePlayImgActive text-xxl padding-left-sm "></span>

    2,定义变量

    3,修改变量值

    playVoice(tempFilePath, index, data) {
          let that = this;
          if (that.innerAudioContext && index == that.playIndex && !that.isPause) {
            that.innerAudioContext.pause();
            that.innerAudioContext.onPause(res => {
              that.isPause = true;
              console.log("已暂停");
              this.imgActive = true
            });
            return;
          }
          if (that.innerAudioContext) {
            that.isPause = false;
            that.innerAudioContext.destroy();
          }
          that.innerAudioContext = mpvue.createInnerAudioContext();
          that.innerAudioContext.autoplay = false;
          that.innerAudioContext.src = tempFilePath;
          that.innerAudioContext.play();
          that.innerAudioContext.onPlay(() => {
            that.isPause = false;
            console.log("开始播放");
            this.imgActive = false
            
          });
          that.innerAudioContext.onStop(() => {
            console.log("停止播放");
            innerAudioContext.destroy();
            this.imgActive = true
           
          });
          that.innerAudioContext.onError(res => {
            innerAudioContext.destroy();
          });
          that.innerAudioContext.onEnded(res => {
            console.log("结束播放");
            console.log(res);
            this.imgActive = true
            
          });
          if (index && index > -1) {
            that.playIndex = index;
            that.datas[index].voice = 1;
            if (data.voice) {
              that
                .http({
                  url: api_url + "changeVoice",
                  data: {
                    id: data.id
                  }
                })
                .then(res => {
                  if (res.status) {
                    console.log("语音状态更新成功");
                  } else {
                    console.log("语音状态更新接口changeVoice出错失败");
                  }
                });
            }
          }
     },

    4,style

    .voicePlayImg{
      background: url('../../../../static/images/auto.png') no-repeat center/32rpx 32rpx; 
    }
    .voicePlayImgActive{
      background: url('../../../../static/images/auto.gif') no-repeat center/32rpx 32rpx; 
    }
  • 相关阅读:
    关于面试总结8-http协议相关面试题
    关于面试总结7-linux篇
    关于面试总结6-SQL经典面试题
    关于面试总结5-python笔试题(递归)
    关于面试总结4-python笔试题
    关于面试总结3-SQL查询
    关于面试总结2-SQL学生表
    关于面试总结1-SQL学生表
    浅谈多变量线性回归中的数据规范化
    浅谈KL散度
  • 原文地址:https://www.cnblogs.com/lpp-11-15/p/12192234.html
Copyright © 2011-2022 走看看