播放JavaScript中的流音频 [英] Play streaming audio in JavaScript

查看:1045
本文介绍了播放JavaScript中的流音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用node-lame编码原始PCM输入流,并且在Node.JS中有以下代码可以成功输出二进制MP3块:

I'm currently using node-lame to encode a raw PCM input stream, and I have the following code in Node.JS that successfully outputs binary MP3 chunks:

server.on('request', (req, res) => {
  encoded.pipe(res);
});

我尝试使用以下代码在前端界面中请求此代码:

I try to request this code inside of my front-end interface with code like the following:

var audio = new Audio('http://localhost:8000/a.mp3'); // the above
audio.play();

但是,由于音频源是一个连续的输入流,因此内容不断下载而没有结束:

However, as the audio source is a continuous input stream, the content just keeps getting downloaded without end:

相反,我希望能够在下载块时播放它们.

Instead, I want to be able to play the chunks as they are downloaded.

我可以在VLC或Quicktime Player之类的应用程序中访问http://localhost:8000/a.mp3,并且音频传输正常.我只是对如何最好地在网络上做到这一点感到困惑.

I can access http://localhost:8000/a.mp3 in an application like VLC or Quicktime Player, and the audio delivery works fine; I'm just stumped as to how to best do this on the web.

谢谢.

推荐答案

此代码对我们有效:

<audio id="music" preload="all">
   <source src="http://localhost:8000/a.mp3">
</audio>
<script>
  let music = document.getElementById('music');
  music.play();
</script>

这篇关于播放JavaScript中的流音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆