HarmonyOS slider组件在动态更新value的情况下, onChange回调中的value获取不正确

Slider({max: this.audioDuration, min: 0, value: this.playDuration})
  .blockColor('#8e9ab7')
  .trackColor('#d8e8f7')
  .selectedColor('#8e9ab7')
  .trackThickness(2)
  .blockSize({width: 10, height: 10})
  .layoutWeight(1)
  .onChange((value: number, mode: SliderChangeMode) => {
    if (mode == SliderChangeMode.Begin) {
      console.log('Begin')
      if (this.isPlaying) {
        this.avplayer?.pause()
      }
    } else if (mode == SliderChangeMode.End) {
      console.log('End')
      if (!this.isPlaying) {
        this.avplayer?.play()
      }
    } else if (mode == SliderChangeMode.Moving) {
      console.log('Moving')
      this.playDuration = value
      this.avplayer?.seek(value)
    } else if (mode == SliderChangeMode.Click) {
      console.log(`Click ${value}`)
      this.playDuration = value
      this.avplayer?.seek(value)
    }
  })

如上面代码所示,在播放音频并且根据播放进度更新slider的value,此时点击slider上的进度条,mode == SliderChangeMode.Click这种情况下获取的value并不是点击位置的value。

HarmonyOS
2024-12-25 13:18:26
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

Slider组件中的.onChange事件状态并不是if,else的关系。因此应改为

Slider({max: this.audioDuration, min: 0, value: this.playDuration, style: SliderStyle.OutSet})
  .blockColor('#8e9ab7')
  .trackColor('#d8e8f7')
  .selectedColor('#8e9ab7')
  .trackThickness(2)
  .blockSize({width: 10, height: 10})
  .layoutWeight(1)
  .onChange((value: number, mode: SliderChangeMode) => {
    if (mode == SliderChangeMode.Begin) {
      console.log('Begin')
      if (this.isPlaying) {
        this.avplayer?.pause()
      }
    }
    if (mode == SliderChangeMode.End) {
      console.log('End')
      if (!this.isPlaying) {
        this.avplayer?.play()
      }
    }
    if (mode == SliderChangeMode.Moving || SliderChangeMode.Click) {
      console.log(`Moving------${value}`)
      console.log(`Click------- ${value}`)
      this.playDuration = value
      this.avplayer?.seek(value)
    }
  })
分享
微博
QQ
微信
回复
2024-12-25 14:53:33
相关问题
HarmonyOS 获取Resourcevalue问题
630浏览 • 1回复 待解决
web页面栈不正确,如何处理?
327浏览 • 1回复 待解决
使用lineHeight行间距展示不正确
356浏览 • 1回复 待解决
HarmonyOS Resource获取value问题
245浏览 • 1回复 待解决
HarmonyOS Slider问题
245浏览 • 1回复 待解决