简体   繁体   English

使用HTMLAudioElement进行角度变化检测

[英]Angular change detection with HTMLAudioElement

I'm trying to build a small audio player but can't seem to get nice change detection, for example for the audio track current time. 我正在尝试构建一个小型音频播放器,但似乎无法获得很好的更改检测,例如当前时间的音轨。 For example, I have the track playing fine in my component and in my template file I have 例如,我的轨道在我的组件中正常播放,在我的模板文件中,

{{ player.currentTime }}

But the time only seems to be updated when I perform other events such as clicking around or play/pause button for example. 但是时间似乎只在我执行其他事件(例如单击四处或播放/暂停按钮)时才更新。 It doesn't intuitively update live as the track is playing. 在播放曲目时,它不会直观地实时更新。 Seems like issue with change detection or something? 好像是变更检测问题之类的?

For working example, see ngx-admin-demo ( https://akveo.com/ngx-admin/#/pages/iot-dashboard ) and scroll down slightly to see the player. 有关工作示例,请参见ngx-admin-demo( https://akveo.com/ngx-admin/#/pages/iot-dashboard )并向下滚动以查看播放器。 If you press play and stop moving mouse you will see what I mean, it doesn't update progress bar or time unless you are performing action. 如果您按下播放并停止移动鼠标,您将明白我的意思,除非您执行操作,否则它不会更新进度条或时间。

Try something like this in your component to encourage change detection every second without the user having to perform some event to spur a change detection (Angular2+ only performs change detection on XHR requests, events and setTimeout/setInterval): 在您的组件中尝试这样的事情来鼓励每秒进行更改检测,而无需用户执行某些事件来刺激更改检测(Angular2 +仅对XHR请求,事件和setTimeout / setInterval执行更改检测):

@Component({
  selector: 'my-player',
  changeDetection: ChangeDetectionStrategy.OnPush,
  templateUrl: '/path/to/my-player.template.html'
})
class MyPlayer {
  constructor(private ref: ChangeDetectorRef) {
    setInterval(() => {
      this.ref.markForCheck();
    }, 1000);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM