在 typescript 中使用MediaRecorder录制HLS音频流

bsxbgnwa  于 2022-11-26  发布在  TypeScript
关注(0)|答案(1)|浏览(253)

我有一个HLS音频源设置为默认音频播放器元素的源。我希望能够记录传入的流供以后使用。我曾尝试使用此MediaRecorder从音频播放器源捕获流,但遇到了一堵墙,告诉我媒体记录器的源不是“MediaStream”类型。
用于初始化音频元素中要播放的流的代码

initStream() {
    // get the audio player
    let audioEl = this.audioPlayer.nativeElement;

    // create the HLS object
    this.hls = new Hls();

    // stream source URL
    var source = "https://as-hls-uk-live.akamaized.net/pool_904/live/uk/bbc_radio_one/bbc_radio_one.isml/bbc_radio_one-audio%3d128000.norewind.m3u8"

    // assign the source to the HLS object
    this.hls.loadSource(source);

    //attach the HLS to the audio player
    this.hls.attachMedia(audioEl);

    console.log(`Created audioPlayer${this.playerId}`);
  }

用于初始化流记录的代码

initRecording(){
    this.recorder = null;
    this.recordedChunks = [];
    try {
      this.recorder = new MediaRecorder(this.hls, {mimeType: "audio/ogg"});
    } catch (e) {
      console.error('Exception while creating MediaRecorder: ' + e);
      return;
    }
    this.recorder.ondataavailable = (event) => {
      console.log(' Recorded chunk of size ' + event.data.size + "B");
      this.recordedChunks.push(event.data);
    };

    this.recorder.start();
  }

我得到的错误是Exception while creating MediaRecorder: TypeError: Failed to construct 'MediaRecorder': parameter 1 is not of type 'MediaStream'.,我假设这是因为我将一个HLS对象传递到媒体记录器中,在该对象中是否有一个属性可以使用?
我也曾尝试使用音频播放器源作为流到媒体记录器,但在这种情况下,我得到一个错误说,媒体记录器无法启动记录器,因为没有轨道列在源中。

initRecording(){
    let audioEl = this.audioPlayer.nativeElement;
    var audioStream = audioEl.src;
    this.recorder = null;
    this.recordedChunks = [];
    try {
      this.recorder = new MediaRecorder(audioStream, {mimeType: "audio/ogg"});
    } catch (e) {
      console.error('Exception while creating MediaRecorder: ' + e);
      return;
    }
    this.recorder.ondataavailable = (event) => {
      console.log(' Recorded chunk of size ' + event.data.size + "B");
      this.recordedChunks.push(event.data);
    };

    this.recorder.start();
  }
huus2vyu

huus2vyu1#

实际上,您需要传入一个“Mediastream”类型。您可以使用captureStream方法从音频html媒体元素创建一个类型,然后将其传递给MediaRecorder

let audioStream = audioEl.captureStream()

相关问题