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

そして、私はすべての音をミュートできるようにしたいと思います。テンプレート内のボタンをクリックした場合:muteFどうすればいいですか?ボタンをクリックするとミュートしたい。もう一度クリックすると、ミュートを解除する必要があります。

+0

に動作しますが0にボリュームを設定してみてくださいましたか? 'this.audio.volume = 0;'のようなもの –

+0

'this.audio.muted'を' true'に設定します。 – isherwood

+0

@RahulSingh私はまだプレーヤーを使用していません..私は1つの機能のためだけにプレーヤーが必要かどうかわかりません –

答えて

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; 
    } 
    } 
関連する問題