使用Mediarecorder在打字稿中记录HLS音频流

发布于 2025-01-18 06:25:25 字数 2120 浏览 2 评论 0原文

我有一个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();
  }

代码,而我获得的错误是异常创建Mediarecorder:TypeError:typeError:无法构造'Mediarecorder':parameter 1不是类型'MediaStream' 。

我还尝试将音频播放器源用作媒体记录器的流,但是在这种情况下,我会发现一个错误,说媒体记录器无法启动录音机,因为源中没有曲目。

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

I have a HLS audio feed set as the source for the default audio player element. I want to be able to record the incoming stream for later use. I have tried using this MediaRecorder to capture the stream from the audio player source but am coming up against a wall where I am told that the source for the media recorder is not of type 'MediaStream'.

Code for initialising the stream to be played in the audio element

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

Code for initialising the stream recording

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

And the error i get is Exception while creating MediaRecorder: TypeError: Failed to construct 'MediaRecorder': parameter 1 is not of type 'MediaStream'. I am assuming this is becuase I am passing a HLS object into the media recorder, is there an attribute within that object that i could use instead?

I have also tried to use the audio player source as the stream into the media recorder, but in that case, I get an error saying that the media recorder could not start the recorder as there are no tracks listed in the source.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沧笙踏歌 2025-01-25 06:25:25

确实,您需要传递“ MediaStream”类型。
您可以使用

let audioStream = audioEl.captureStream()

Indeed you need to pass in a 'Mediastream' type.
You can create one using the captureStream method from your audio html media element then pass it to your MediaRecorder

let audioStream = audioEl.captureStream()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文