使用Lame将原始音频转换为mp3

时间:2018-08-14 08:00:32

标签: audio firebase-storage avaudioplayer angular6 lame

我将文件以Float32Array的形式上传到Fire Storage中,但是要播放该文件,必须先将其转换为将其存储到Firebase的mp3,wav或ogg中,或者在获得下载网址之后。 我使用Lame选择了1选项。

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' });

但是在firebase中,它没有作为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

任何建议我下一步该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以使用简单的音频服务:

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();
  }
}

然后,您只需要预加载并设置音量

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

开心!玩:

this.audio.play();