uniapp 通过scroll-view实现音频的文字内容超出显示区,滚动条自动根据音频时间百分比滑动。

data() {
    return {
      innerAudioContext: null, //音频实例对象

      windowHeight: 0, //可用窗口高
      scrollTop: 1, //滚动 初始值
      scrollHeight: 0, //滚动内容总高度
      reHeight: 0, //可滚动高度
      isGetReHeight: false, // 是否已经计算出可用高度
      percentPlayed: 0, //当前时间/总时间;得到的百分比
    };
  },

methods: {
    getwindowHeight() {
      let obj = uni.getWindowInfo();
      this.windowHeight = obj.windowHeight;
      console.log("this.windowHeight=>", this.windowHeight);
    },

    scroll(event) {
      console.log("监视滚动", event);
      this.scrollHeight = event.detail.scrollHeight;
      this.reHeight =
        this.scrollHeight - parseInt(0.5 * Number(this.windowHeight));
      console.log("可滚动高度=>", this.reHeight);
      this.isGetReHeight = true;
    },

    innerAudioContextAction() {
      // 设置音频实例与相关设置
      this.innerAudioContext = uni.createInnerAudioContext();
      console.log("innerAudioContext=>", this.innerAudioContext);
      console.log("??????", this.voiceArr[this.curIndex]);
      this.innerAudioContext.autoplay = true; //设置是否自动播放
      this.innerAudioContext.src = this.voiceArr[this.curIndex];
      this.pageTitle = this.titleArr[this.curIndex].title;
      console.log("src", this.innerAudioContext.src);
      this.innerAudioContext.play(); //播放

      this.innerAudioContext.onCanplay((re) => {
        console.log("onCanplay", re);
      });

      this.innerAudioContext.onTimeUpdate(() => {
        let currentTime = this.innerAudioContext.currentTime;
        let innerAudioContext = this.innerAudioContext.duration;
        console.log("音频播放进度更新事件", currentTime, innerAudioContext);

        this.percentPlayed =
          Number(this.innerAudioContext.currentTime) /
          Number(this.innerAudioContext.duration);
        console.log("结果=>", this.percentPlayed.toFixed(4));

        this.scrollTop = Number(this.reHeight * this.percentPlayed);
        console.log(
          "this.scrollTop=>",
          this.scrollTop,
          "this.reHeight=>",
          this.reHeight
        );
      });

      this.innerAudioContext.onError(function (res) {
        console.log(res);
      });

      this.innerAudioContext.onEnded(() => {
        this.innerAudioContext.stop();

        if (this.curIndex == this.voiceArr.length - 1) {
          console.log("放完了");
          console.log("this.curIndex=>", this.curIndex);
          this.innerAudioContext.stop();
        }

        if (this.curIndex != this.voiceArr.length - 1) {
          console.log("改前this.curIndex=>", this.curIndex);
          this.curIndex++;

          console.log("改后this.curIndex=>", this.curIndex);
          this.pageTitle = this.titleArr[this.curIndex].title;
          this.innerAudioContext.src = this.voiceArr[this.curIndex];
          console.log("再次修改后src", this.innerAudioContext.src);
          this.innerAudioContext.autoplay = true; //设置是否自动播放
          this.innerAudioContext.play();
        }
      });
    },

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值