我希望能够创建一个函数来停止或暂停来自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')
}
}
答案 0 :(得分:1)
在音频元素上呼叫.pause()
。
stopAudio() {
this.audio.pause();
}
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement