Mercurial > hg > ugly-duckling
view src/app/audio-file-open/audio-file-open.component.ts @ 19:953932e9ba82
Remove zone injection from app.component and move to where the event is emitted in audio-file-open.component.
author | Lucas Thompson <dev@lucas.im> |
---|---|
date | Fri, 28 Oct 2016 08:28:54 +0100 |
parents | 7e3ab6f8792f |
children | 5bdfcf493646 |
line wrap: on
line source
import { Component, OnInit, ViewChild, ElementRef, Output, EventEmitter, NgZone } from '@angular/core'; interface AudioContextConstructor { new(): AudioContext } interface WindowAudioContext { AudioContext?: AudioContextConstructor; webkitAudioContext?: AudioContextConstructor } @Component({ selector: 'app-audio-file-open', templateUrl: './audio-file-open.component.html', styleUrls: ['./audio-file-open.component.css'] }) export class AudioFileOpenComponent implements OnInit { @ViewChild('open') open: ElementRef; @Output() audioLoaded: EventEmitter<AudioBuffer>; private audioContext: AudioContext; constructor(private zone: NgZone) { this.audioLoaded = new EventEmitter<AudioBuffer>(); // TODO make a service which provides the AudioContext? const factory: WindowAudioContext = (window as WindowAudioContext); this.audioContext = new (factory.AudioContext || factory.webkitAudioContext)(); } ngOnInit() { } decodeAudio(files: FileList) { if (files.length > 0) { const reader: FileReader = new FileReader(); reader.onload = (event: any) => { this.audioContext.decodeAudioData(event.target.result, buffer => { this.zone.run(() => { this.audioLoaded.emit(buffer); }); }); }; reader.readAsArrayBuffer(files[0]); } } openAudioDialog() { this.open.nativeElement.click(); } }