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

Audio 使用Lame将原始音频转换为mp3,audio,firebase-storage,avaudioplayer,angular6,lame,Audio,Firebase Storage,Avaudioplayer,Angular6,Lame,我正在将文件作为Float32Array上传到Fire存储中,但为了播放它,我必须在将其存储到firebase之前将其转换为mp3、wav或ogg,或者在获得下载url之后。 我选择了1选项,使用Lame let mp3Encoder = new lamejs.Mp3Encoder(1, 44100, 128); let in16Array = Int16Array.from(audioData); //audioData is a Float32Array containing the rec

我正在将文件作为Float32Array上传到Fire存储中,但为了播放它,我必须在将其存储到firebase之前将其转换为mp3、wav或ogg,或者在获得下载url之后。 我选择了1选项,使用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' });
但是在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

有什么建议我下一步应该尝试什么吗?

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

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

读者很乐意在这里提供帮助,但如果你能说明你有什么问题,回答起来就容易多了。如果可以,请立即编辑此问题,我们可以为您重新打开此问题。“不工作”不是一个有用的问题描述。此外,请参阅thank you@glennsl。我希望我已经改进了我的问题该服务不会解决这样一个问题,即我必须先将音频转换为audio()能够识别的格式(mp3、wav或ogg),然后再将其转换为正确的格式,是的,这会起作用。