Angular中的音频进度条不起作用。
创始人
2024-10-31 11:30:26
0

要修复 Angular 中的音频进度条,可以使用“AudioContext”和“AnalyserNode”。在每个刷新周期中,可以将音频数据读取到AnalyserNode中,然后使用此数据更新进度条。以下是示例代码:

在组件中定义以下变量:

audioContext: AudioContext;
analyser: AnalyserNode;
source: MediaElementAudioSourceNode;
requestAnimationFrameId: number;
progressBarWidth: number;

在 ngOnInit() 生命周期钩子中初始化变量:

ngOnInit() {
  this.audioContext = new AudioContext();
  this.analyser = this.audioContext.createAnalyser();
  this.source = this.audioContext.createMediaElementSource(this.audioPlayer.nativeElement);
  this.source.connect(this.analyser);
  this.analyser.connect(this.audioContext.destination);
  this.progressBarWidth = this.progressBar.nativeElement.offsetWidth;
}

在 ngAfterViewInit() 生命周期钩子中设置进度条更新函数:

ngAfterViewInit() {
  this.requestAnimationFrameId = requestAnimationFrame(() => this.updateProgressBar());
}

updateProgressBar() {
  const bufferLength = this.analyser.frequencyBinCount;
  const dataArray = new Uint8Array(bufferLength);
  this.analyser.getByteFrequencyData(dataArray);
  const currentPercentage = (this.audioPlayer.nativeElement.currentTime / this.audioPlayer.nativeElement.duration) * 100;
  const progressWidth = (currentPercentage / 100) * this.progressBarWidth;
  this.progressBar.nativeElement.style.width = `${progressWidth}px`;
  this.requestAnimationFrameId = requestAnimationFrame(() => this.updateProgressBar());
}

在 ngOnDestroy() 生命周期钩子中取消 requestAnimationFrame:

ngOnDestroy() {
  cancelAnimationFrame(this.requestAnimationFrameId);
}

相关内容

热门资讯

受玩家影响!wepoker有没... 受玩家影响!wepoker有没有插件,德普之星透视辅助,原来是有挂(有挂解密)进入游戏-大厅左侧-新...
于此同时!佛手在线大菠萝技巧,... 于此同时!佛手在线大菠萝技巧,德扑之星有辅助软件吗,好像确实有挂(有挂方法)一、德扑之星有辅助软件吗...
现有说明如下!wejoker辅... 现有说明如下!wejoker辅助软件视频,智星菠萝有挂吗,切实真的是有挂(有挂教程)暗藏猫腻,小编详...
黑科技技巧!哈糖大菠萝挂法,德... 黑科技技巧!哈糖大菠萝挂法,德普之星私人局透视,一贯是真的有挂(有挂教程)1、用户打开应用后不用登录...
昨日!We poker辅助器下... 昨日!We poker辅助器下载,德扑之星透视辅助插件,竟然存在有挂(有挂技巧);德扑之星透视辅助插...
值得注意的是!wepoker分... 值得注意的是!wepoker分析,德扑之星辅助工具如何设置,竟然是有挂(有挂功能)所有人都在同一条线...
今天上午!wpk俱乐部是真的吗... 今天上午!wpk俱乐部是真的吗,智星德州插件最新版本更新内容详解,果然确实有挂(有挂头条)1、许多玩...
为切实保障!菠萝德州透视脚本,... 为切实保障!菠萝德州透视脚本,德普之星透视辅助软件是真的吗,切实真的有挂(讲解有挂)1、不需要AI权...
推出新举措!德普之星辅助软件,... 推出新举措!德普之星辅助软件,德普之星怎么设置埋牌,切实是真的有挂(有挂细节)1、德普之星怎么设置埋...
近期!wepoker插件程序激... 近期!wepoker插件程序激活码,智星德州插件最新版本更新内容详解,总是确实有挂(有挂方法)1、智...