Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play sound in Angular 4

I'm working on an Electron app with Angular 4. I want to play sound on some specific action. Is there any module or code for that? It can be in the angular 4 or if electron is providing some service for that it should also work

As I want to play it on some action I can't use the HTML audio tag and audio() of javascript

I only want to play the sound of 2-3 seconds so no other functionalities are needed.

It can be of electron or Angular 4 any of them can work...

like image 873
hardiksa Avatar asked Jul 03 '17 10:07

hardiksa


People also ask

Is there an audio player for angular 3 and Angular 4?

An audio player for Ionic 3 and Angular 4. Works with HTML 5 audio or native audio using Cordova Media plugin. A library for playing audio using HTML 5 audio for Angular 7/8/9.

What is audio progress in angular?

audioObject.progress: The progress of the object as a percentage (0-1); Angular audio is designed to be powerful and awesome, so use it everywhere, even in projects where you hadn't even considered using sound in the first place. Its directives make sound a breeze.

What is ngaudio in angular?

A fast and angular way of handling audio. ngAudio is a lightweight package of directives and services that treat sound in an angular way. Adjust volume and time with getters and setters. Attach easily to the scope. Preloads sounds automatically.

How to manage the state of the AudioService in angular?

In a typical Angular application, you might use some state management library like NgRx. But in this application, you are going to use BehaviorSubject to manage the state of the application. You are going to update AudioService to manage the state since the state is dependent on audio playback.


2 Answers

just did this in a project am working (angular 4) and it worked

playAudio(){   let audio = new Audio();   audio.src = "../../../assets/audio/alarm.wav";   audio.load();   audio.play(); } this.playAudio(); 

make sure the path is correct and references an existing audio

like image 147
Nelson Bwogora Avatar answered Oct 19 '22 20:10

Nelson Bwogora


As per Robin's comment, i have checked that link we can use it using the audio() object in the ts file like this:

this.audio = new Audio(); this.audio.src = "../../../assets/sounds/button_1.mp3"; this.audio.load(); this.audio.play(); 
like image 23
hardiksa Avatar answered Oct 19 '22 19:10

hardiksa