收藏
回答

音频设置倍速无效,默认写死playbackRate是2可以两倍速,但是手动切换不可以?

onToggleSpeed() {

if (!this.myAudio || this._destroyed) return;


const speeds = this.data.speedOptions;

console.log('speedOptions:', speeds);

console.log('this.data.playbackRate:', this.data.playbackRate, typeof this.data.playbackRate);

console.log('myAudio.playbackRate:', this.myAudio.playbackRate);


const currentIndex = speeds.indexOf(Number(this.data.playbackRate));

console.log('currentIndex:', currentIndex);


const nextIndex = (currentIndex + 1) % speeds.length;

const newRate = speeds[nextIndex];

console.log('newRate:', newRate);


this.myAudio.playbackRate = Number(newRate);

console.log('设置后 myAudio.playbackRate:', this.myAudio.playbackRate);


this.setData({ playbackRate: Number(newRate) });


console.log('倍速切换完成');

},


回答关注问题邀请回答
收藏

2 个回答

  • 社区技术运营专员--阳光
    社区技术运营专员--阳光
    04-14

    请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    04-14
    有用
    回复
  • 智能回答 智能回答 本次回答由AI生成
    04-13
    有用
    回复 1
    • 吕邦
      吕邦
      04-13
      没有报错,<view class="viewAudio" style="background-image:url({{coverImgUrl}})">
       <view class="viewBtn">
        <view class="viewBtn1">
         <t-image wx:if="{{indexPlay > 0}}" t-class="pic2" catch:tap="onPrev"
          src="https://admin.yingwuhao.com/static/miniprogram/reading/Group_198.png" />
        </view>
        <view class="viewBtn2" catch:tap="onPauseOrPlay">
         <t-image t-class="pic2" class="{{isPlay ? '' : 'hide'}}"
          src="https://admin.yingwuhao.com/static/miniprogram/reading/Group_196.png" />
         <t-image t-class="pic2" class="{{isPlay ? 'hide' : ''}}"
          src="https://admin.yingwuhao.com/static/miniprogram/reading/Group_199.png" />
        </view>
        <view class="viewBtn1">
         <t-image wx:if="{{indexPlay < list.length - 1}}" t-class="pic2" catch:tap="onNext"
          src="https://admin.yingwuhao.com/static/miniprogram/reading/Group_197.png" />
        </view>
        <view class="viewSpeed" catch:tap="onToggleSpeed">
         {{playbackRate}}x
        </view>
       </view>


       <view class="viewAudioInfo">
        <view class="viewBarOutter" catchtap="clickBar">
         <view class="viewBar">
          <view class="viewTime" style="left:{{(561 + (isLandscape ? 66 : 0) ) * (timeNow/timeTotal)}}rpx;">
           {{timeNowStr}}
          </view>
         </view>
        </view>




       </view>
      </view>Component({
        properties: {
          list: {
            type: Object,
            value: [],
          },
          autoPlay: {
            type: Boolean,
            value: false,
          },
          // 音频标题
          title: {
            type: String,
            value: '',
          },
          // 专辑名称
          epname: {
            type: String,
            value: '',
          },
          // 歌手名称
          singer: {
            type: String,
            value: '鹦鹉号阅读',
          },
          // 背景封面
          coverImgUrl: {
            type: String,
            value: 'https://admin.yingwuhao.com/static/miniprogram/index/Group_216.png',
          },
          isLandscape: {
            type: Boolean,
            value: false,
          },
          startTime: {
            type: Number,
            value: 0,
          },
        },


        data: {
          coverImgUrl: '',
          list: [],
          indexPlay: 0,
          isPlay: false,
          src: '',
          timeNow: 0,
          timeNowStr: '',
          timeTotal: '',
          progress: 0,
          playbackRate: 1,
          speedOptions: [0.5, 0.75, 1, 1.25, 1.5, 2],
        },


        lifetimes: {
          attached: function () {
            this.init();
          },
          detached: function () {
            this.destory();
          },
        },


        methods: {
          clickBar(e) {
            if (!this.myAudio || this._destroyed) {
              return;
            }
            console.log(e);
            var next = (e.detail.x - 70) / (694 - (this.data.isLandscape ? 33 : 66));
            if (next > 1) {
              next = 1;
            }


            if (next < 0) {
              next = 0;
            }


            this.myAudio.seek(this.data.timeTotal * next);
            console.log(next);
          },
          myAudio: {},
          // 创建audio
          init() {
            console.log('init');
            this._destroyed = false;
            this.myAudio = wx.getBackgroundAudioManager();


            var fixTimeNow = (time) => {
              if (time == 0 || time == undefined) {
                return '00:00:00';
              }
              var hours = Math.floor(time / 3600);
              var minutes = Math.floor((time % 3600) / 60);
              var seconds = time % 60;
              var fixZero = (num) => {
                num = Math.ceil(num);
                if (num < 10) {
                  return '0' + num;
                }
                return '' + num;
              };
              return fixZero(hours) + ':' + fixZero(minutes) + ':' + fixZero(seconds);
            };
            this._onCanplay = () => {
              if (!this.myAudio || this._destroyed) {
                return;
              }
              // 设置倍速
              this.myAudio.playbackRate = this.data.playbackRate;
              // 显示界面解析
              console.log('音频onCanplay');
              var timeNowStr = fixTimeNow(this.myAudio.currentTime);
              this.setData({
                timeNowStr,
                timeNow: this.myAudio.currentTime,
                timeTotal: this.myAudio.duration,
              });


              if (this.properties.autoPlay) {
                this.myAudio.play();
              } else {
                this.myAudio.pause();
              }
            };
            this.myAudio.onCanplay(this._onCanplay);
            this._onTimeUpdate = () => {
              if (!this.myAudio || this._destroyed) {
                return;
              }
              var timeNowStr = fixTimeNow(this.myAudio.currentTime);
              this.setData({
                timeNowStr,
                timeNow: this.myAudio.currentTime,
                timeTotal: this.myAudio.duration,
              });
              this.triggerEvent('updateTime', { value: this.myAudio.currentTime });
            };
            this.myAudio.onTimeUpdate(this._onTimeUpdate);
            this._onEnded = () => {
              if (!this.myAudio || this._destroyed) {
                return;
              }
              console.log('音频onEnded');
              var timeNowStr = fixTimeNow(this.myAudio.duration);
              this.setData({
                timeNowStr,
                timeNow: this.myAudio.duration,
                isPlay: false,
              });
            };
            this.myAudio.onEnded(this._onEnded);
            this._onPlay = () => {
              if (!this.myAudio || this._destroyed) {
                return;
              }
              console.log('音频onPlay');
              // 播放时设置倍速
              this.myAudio.playbackRate = this.data.playbackRate;
              console.log('onPlay playbackRate:', this.data.playbackRate);
              this.setData({
                isPlay: true,
              });
            };
            this.myAudio.onPlay(this._onPlay);
            this.myAudio.playbackRate = 2;
            this._onPause = () => {
              if (!this.myAudio || this._destroyed) {
                return;
              }
              console.log('音频onPause111', this.myAudio);
              this.setData({
                isPlay: false,
              });
            };
            this.myAudio.onPause(this._onPause);


            var list = this.properties.list.map((item) => decodeURI(item));
            var audioTitle = this.properties.title || this.properties.epname || '音频播放';
            this.setData({
              list,
              coverImgUrl: this.properties.coverImgUrl,
            });
            this.myAudio.src = list[0];
            this.myAudio.title = audioTitle;
            this.myAudio.epname = this.properties.epname;
            this.myAudio.singer = this.properties.singer;
            this.myAudio.coverImgUrl = this.properties.coverImgUrl;
            this.myAudio.playbackRate = this.data.playbackRate;
            this.myAudio.seek(this.properties.startTime);
          },
          // 播放/暂停
          onPauseOrPlay() {
            if (!this.myAudio || this._destroyed) {
              return;
            }
            if (this.data.isPlay) {
              this.myAudio.pause();
            } else {
              this.myAudio.playbackRate = this.data.playbackRate;
              this.myAudio.play();
            }
          },
          // 切换倍速
          onToggleSpeed() {
            if (!this.myAudio || this._destroyed) return;


            const speeds = this.data.speedOptions;
            console.log('speedOptions:', speeds);
            console.log('this.data.playbackRate:', this.data.playbackRate, typeof this.data.playbackRate);
            console.log('myAudio.playbackRate:', this.myAudio.playbackRate);


            const currentIndex = speeds.indexOf(Number(this.data.playbackRate));
            console.log('currentIndex:', currentIndex);


            const nextIndex = (currentIndex + 1) % speeds.length;
            const newRate = speeds[nextIndex];
            console.log('newRate:', newRate);


            this.myAudio.playbackRate = Number(newRate);
            console.log('设置后 myAudio.playbackRate:', this.myAudio.playbackRate);


            this.setData({ playbackRate: Number(newRate) });


            console.log('倍速切换完成');
          },
          // 上一曲(已修复倍速)
          onPrev() {
            if (!this.myAudio || this._destroyed) {
              return;
            }
            if (this.data.indexPlay > 0) {
              var next = this.data.indexPlay - 1;
              this.myAudio.src = this.data.list[next];
              this.myAudio.playbackRate = this.data.playbackRate;
              this.myAudio.play();
              this.setData({
                indexPlay: next,
              });
            }
          },
          // 下一曲(已修复倍速)
          onNext() {
            if (!this.myAudio || this._destroyed) {
              return;
            }
            if (this.data.indexPlay < this.data.list.length - 1) {
              var next = this.data.indexPlay + 1;
              this.myAudio.src = this.data.list[next];
              this.myAudio.playbackRate = this.data.playbackRate;
              this.myAudio.play();
              this.setData({
                indexPlay: next,
              });
            }
          },
          // 销毁audio
          destory() {
            this._destroyed = true;
            if (this.myAudio && this.myAudio.stop) {
              console.log('销毁audio');
              this.myAudio.offCanplay && this.myAudio.offCanplay(this._onCanplay);
              this.myAudio.offTimeUpdate && this.myAudio.offTimeUpdate(this._onTimeUpdate);
              this.myAudio.offEnded && this.myAudio.offEnded(this._onEnded);
              this.myAudio.offPlay && this.myAudio.offPlay(this._onPlay);
              this.myAudio.offPause && this.myAudio.offPause(this._onPause);
              this.myAudio = null;
            }
          },
        },
      });
      04-13
      回复
登录 后发表内容