-2

I'm uploading the file in Fire Storage as an Float32Array, but in order to play it, I have to have it converted either before storing it to firebase to mp3, wav or ogg or after i get the download url. I chose the 1 option, using Lame.

let mp3Encoder = new lamejs.Mp3Encoder(1, 44100, 128);
let in16Array = Int16Array.from(audioData); //audioData is a Float32Array containing the recorded audio buffer
var mp3Tmp = mp3Encoder.encodeBuffer(in16Array); //encode mp3

//Push encode buffer to mp3Data variable
this.mp3Data.push(mp3Tmp);

//Get end part of mp3
//mp3Tmp = mp3Encoder.flush();

//Write last data to the output data, mp3Data contains now the complete mp3Data
this.mp3Data.push(mp3Tmp);

const blob = new Blob([this.mp3Data], { type: 'audio/mp3' });

However in firebase it is not uploaded as an mp3

const id = this.i + Math.random().toString(36).substring(2) + ".mp3" ;
this.ref = this.afStorage.ref(id);
this.task = this.ref.put(blob); //code for uploading

Any advice what should i try next?

codgrl
  • 11
  • 3
  • Readers are very happy to help here, but it makes it much easier to answer if you can show _what_ you are having trouble with. Do please edit this question now if you can do that, and we can get the question re-opened for you. – halfer Aug 17 '18 at 14:21
  • "is not working" is not a useful problem description. – glennsl Aug 19 '18 at 14:48
  • Also, please see [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – glennsl Aug 19 '18 at 14:49
  • thank you @glennsl . I hope i have improved my question – codgrl Aug 20 '18 at 11:31

1 Answers1

0

You can have a simple audio service:

import { Injectable } from '@angular/core';
@Injectable()
export class AudioService {
  map = new Map();
  constructor() { }
  preload(src: string): HTMLAudioElement {
    if (this.map.has(src)) {
      return this.map.get(src);
    }
    const audio = new Audio();
    audio.src = src;
    audio.load();
    this.map.set(src, audio);
    return audio;
  }
  play(src: string) {
    return this.preload(src).play();
  }
  pause(src: string) {
    return this.preload(src).pause();
  }
}

Then you just need to preload and set the volumne

this.audio = this.audioService.preload('assets/audio.mp3');
this.audio.volume = this.isMuted ? 1 : 0;

And be happy! To play:

this.audio.play();
Fabio Picheli
  • 939
  • 11
  • 27
  • The service won't solve the problem that i first have to convert my audio in a format which Audio() would recognize (mp3, wav or ogg ) after having it in the right format, yes, this would work. – codgrl Aug 20 '18 at 07:37