如何暂停或停止来自component.ts的HTML5音频元素

时间:2019-04-08 03:21:39

标签: angular typescript html5-audio

我希望能够创建一个函数来停止或暂停来自component.ts文件的HTML。我看不到任何使用document.getelement

暂停音频的内置方法
<audio controls id="audio-file" >
<source src="sample.wav" type="audio/wav">
</audio>
import { Component, OnInit, Inject } from '@angular/core';
import { DOCUMENT } from "@angular/platform-browser";
import { WINDOW } from "../../services/window.service";

@Component({
  selector: 'app-audio',
  templateUrl: './audio.component.html',
  styleUrls: ['./audio.component.css']
})
export class AudioComponent implements OnInit {
  private audio = this.document.getElementById("audio-file")
  constructor(
    @Inject(DOCUMENT) private document: Document,
    @Inject(WINDOW) private window: Window
  ) { }
  ngOnInit() {
  }
stopAudio(){
  this.document.getElementById('audio-file')
}
}

1 个答案:

答案 0 :(得分:1)

在音频元素上呼叫.pause()

stopAudio() {
  this.audio.pause();
}

https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement