2017-08-08 50 views
4

我有一個聲音在我的角度項目,像這樣:如何在Angular2中靜音和取消靜音?

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(); 
} 

而且我希望能夠全部靜音。如果我點擊我的模板中的按鈕:

<img class="grow" id="mute" [src]='mute' (click)="muteF()"/> 

我怎麼能寫我的功能muteF?如果點擊按鈕,我想靜音。如果我再次點擊,它必須執行取消靜音。

+0

你嘗試設置音量爲0?類似於'this.audio.volume = 0;' –

+0

不管你喜歡,將'this.audio.muted'設置爲'true'。 – isherwood

+0

@RahulSingh我還沒有使用球員..我不知道我是否需要球員只是爲了一個功能 –

回答

0

可能是這樣的事情(除非角度具有音頻特定的功能,例如)。

組件

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() 
    } 
//... 
} 

然後在模板

模板

<audio #audio></audio> 
1

這對我的作品

muteF() { 
    if (this.audio.volume !== 0) { 
     this.audio.volume = 0; 
    } else { 
     this.audio.volume = 1; 
    } 
    } 
相關問題