Mercurial > hg > ugly-duckling
view src/app/app.component.ts @ 31:f6ea31a3b1a3
Encapsulate audio playing and decoding logic in a ng2 service, provided by the root module.
author | Lucas Thompson <dev@lucas.im> |
---|---|
date | Wed, 30 Nov 2016 10:21:27 +0000 |
parents | 1ff1b5bdeb9e |
children | f6e58c2accb0 |
line wrap: on
line source
import {Component} from '@angular/core'; import {AudioPlayerService} from "./services/audio-player.service"; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { audioBuffer: AudioBuffer; // TODO consider revising constructor(private audioService: AudioPlayerService) {} onFileOpened(file: File) { const reader: FileReader = new FileReader(); const mimeType = file.type; reader.onload = (event: any) => { this.audioService.loadAudioFromUrl( URL.createObjectURL(new Blob([event.target.result], {type: mimeType})) ); // TODO use a rxjs/Subject instead? this.audioService.decodeAudioData(event.target.result).then(audioBuffer => { this.audioBuffer = audioBuffer; }); }; reader.readAsArrayBuffer(file); } }