播放JavaScript中的流音频

编程入门 行业动态 更新时间:2024-10-07 18:30:19
本文介绍了播放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('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之类的应用程序中访问localhost:8000/a.mp3,并且音频传输正常.我只是对如何最好地在网络上做到这一点感到困惑.

I can access 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="localhost:8000/a.mp3"> </audio> <script> let music = document.getElementById('music'); music.play(); </script>

更多推荐

播放JavaScript中的流音频

本文发布于:2023-11-28 08:06:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641598.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:流音   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!