Mercurial > hg > ugly-duckling
view src/app/audio-file-open/audio-file-open.component.ts @ 16:7e3ab6f8792f
Rough waveform loading, issues exist regarding communicating changes from child components (unless using the current workaround with explicit zone running)... investigating.
author | Lucas Thompson <dev@lucas.im> |
---|---|
date | Thu, 27 Oct 2016 17:08:57 +0100 |
parents | 0571cf863026 |
children | 953932e9ba82 |
line wrap: on
line source
import { Component, OnInit, ViewChild, ElementRef, Output, EventEmitter } 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() { 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.audioLoaded.emit(buffer); }); }; reader.readAsArrayBuffer(files[0]); } } openAudioDialog() { this.open.nativeElement.click(); } }