changeset 134:976087b98e72

Allow for opening Blobs directly.
author Lucas Thompson <dev@lucas.im>
date Mon, 20 Mar 2017 13:26:23 +0000
parents 4452f4b9f9a8
children 262995cfd3e6
files src/app/app.component.ts
diffstat 1 files changed, 10 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/app/app.component.ts	Mon Mar 20 13:25:22 2017 +0000
+++ b/src/app/app.component.ts	Mon Mar 20 13:26:23 2017 +0000
@@ -27,22 +27,27 @@
     );
   }
 
-  onFileOpened(file: File) {
+  onFileOpened(file: File | Blob) {
     this.canExtract = false;
     this.isProcessing = true;
     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}))
-      );
+      const url: string = file instanceof Blob ? URL.createObjectURL(file) :
+        URL.createObjectURL(new Blob([event.target.result], {type: mimeType}));
+      this.audioService.loadAudioFromUrl(url);
       // TODO use a rxjs/Subject instead?
-      this.audioService.decodeAudioData(event.target.result).then(audioBuffer => {
+      this.audioService.decodeAudioData(event.target.result)
+        .then(audioBuffer => {
         this.audioBuffer = audioBuffer;
         if (this.audioBuffer) {
           this.canExtract = true;
           this.isProcessing = false;
         }
+      }).catch(error => {
+        this.canExtract = false;
+        this.isProcessing = false;
+        console.warn(error);
       });
     };
     reader.readAsArrayBuffer(file);