chrisguttandin/angular-audio-context

Example to play simple audio

aareeph opened this issue · 5 comments

Hi

Is there any example code to play a simple audio file using this library?

Thanks
Arif

Hi Arif,

generally there is nothing special about the AudioContext that this library provides. It can be used as the native one which comes with a modern browser. MDN has a lot of documentation about it: https://developer.mozilla.org/docs/Web/API/Web_Audio_API

Here is a little example component which would play a beep sound. I hope this helps.

import { Component, OnInit } from '@angular/core';
import { AudioContext } from 'angular-audio-context';

@Component({
    selector: 'my-beep',
    template: ''
})
export class BeepComponent implements OnInit {

    constructor (
        private _audioContext: AudioContext
    ) { }

    public ngOnInit () {
        const oscillatorNode = this._audioContext.createOscillator();

        oscillatorNode.connect(this._audioContext.destination);
        oscillatorNode.start();
    }

}

Hi @chrisguttandin,

Thanks for your response and providing example. It worked like a charm for me :)

What about loading a file? With http?

Also you really should add this to the main docs. When people find your library this is the main thing they want to do. !

Hi @simeyla,

I wish all my libraries would be perfectly documented. But unfortunately I'm doing most of this in my free time and my free time is of course limited.

However I added the BeepComponent as an example to the readme. I hope this helps.

As you said loading a file could be done with the http package of Angular. You can set the responseType to 'arraybuffer'. Please feel free to open another issue if you run into any problems with that.

@chrisguttandin great thanks :-) I now have a nice sound of coins playing for me everytime someone makes a purchase on our website :-)