[英]How to mute and unmute sound in Angular2?
I have a sound in my Angular project, like this:我的 Angular 项目中有一个声音,如下所示:
introInfo() {
this.audio.src = '../assets/sound1.wav';
this.audio.load();
this.audio.play();
}
feedbackInfo() {
this.audio.src = '../assets/sound1.wav';
this.audio.load();
// auto-start
this.audio.play();
}
And I would like to be able to mute all sounds.我希望能够静音所有声音。 If I click the button in my template:
如果我单击模板中的按钮:
<img class="grow" id="mute" [src]='mute' (click)="muteF()"/>
How could I write my function muteF
?我怎么能写我的函数
muteF
? I would like to mute if I click the button.如果我点击按钮,我想静音。 If I click a second time, it must perform unmute.
如果我再次单击,它必须执行取消静音。
This works for me这对我有用
muteF() {
if (this.audio.volume !== 0) {
this.audio.volume = 0;
} else {
this.audio.volume = 1;
}
}
something like this probably (unless angular has audio-specific features, for example).可能是这样的(除非 angular 具有特定于音频的功能,例如)。
import { ElementRef, Inject, Injectable, ViewChild } from '@angular/core';
@Injectable()
export class MyService {
@ViewChild('audio') audio: ElementRef
constructor(/* DOCUMENT would be an option too, from @angular/platform-browser - `@Inject(DOCUMENT) private document: any` */) {}
introInfo() {
this.audio.nativeElement.load()
this.audio.nativeElement.play()
}
//...
}
then in the template然后在模板中
<audio #audio></audio>
You can use muted property such as您可以使用静音属性,例如
this.audio.muted = true;
Works for video element as well.也适用于视频元素。
Source: https://www.w3schools.com/tags/av_prop_muted.asp来源: https : //www.w3schools.com/tags/av_prop_muted.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.