简体   繁体   English

如何 stream 从服务器到客户端的媒体文件,例如我在环回 4 nodejs 中的 mp3 音频文件?

[英]How to stream a media file from server to client side for example an mp3 audio file in my case in loopback 4 nodejs?

I have loopback 4 - nodejs in the backend and Ionic 4 in the frontend of my mobile application.我的移动应用程序的后端有环回 4 - nodejs,前端有 Ionic 4。 I'm storing an mp3 file on server sid and I want to stream and play it on the client-side so basically audio streaming from loopback4.我在服务器 sid 上存储了一个 mp3 文件,我想 stream 并在客户端播放它,所以基本上是来自 loopback4 的音频流。 So basically I'm looking for server-side code in loopback-4 which is in typescript to audio-stream a file to client.所以基本上我正在寻找 typescript 中的 loopback-4 中的服务器端代码,以便将文件音频流式传输到客户端。 (I'm unable to use npmjs libraries since most of them are not typed and cannot be used in typescript) (我无法使用 npmjs 库,因为它们中的大多数都没有输入并且不能在 typescript 中使用)

As soon as I know about loopback4, it doesn't have nodejs stream implementation.一旦我知道 loopback4,它就没有 nodejs stream 实现。 I recommend you to use native NodeJS streams.我建议您使用本机 NodeJS 流。 Check out this repo https://github.com/noamtcohen/AudioStreamer查看这个 repo https://github.com/noamtcohen/AudioStreamer

Short answer: I was able to achieve this by simply serving static files ie my audio file from the server-side.简短的回答:我可以通过简单地提供 static 文件来实现这一点,即来自服务器端的音频文件。 Accessing it using the endpoint I made and calling it using the tag on the frontend.使用我创建的端点访问它并使用前端的标签调用它。

Long answer:长答案:

  1. In loopback 4, you can find a line of code in application.ts file where public directory from the root folder of the server project is served.在环回 4 中,您可以在 application.ts 文件中找到一行代码,其中提供了服务器项目根文件夹中的公共目录。

    this.static('/', path.join(__dirname, '../../public')); this.static('/', path.join(__dirname, '../../public'));

Similarly, you can serve your static files from whatever dir you want.同样,您可以从您想要的任何目录提供 static 文件。 In my case, I served my files from media folder which I added in the root directory of my node project.就我而言,我从我添加到节点项目的根目录中的媒体文件夹中提供了我的文件。

this.static('/', path.join(__dirname, '../media'));
  1. The second step is to expose an API endpoint which you would use to make a get request to the server.第二步是公开一个 API 端点,您可以使用该端点向服务器发出 get 请求。 You can do that inside index.ts file of the server project and the code right below app.start().您可以在服务器项目的 index.ts 文件和 app.start() 正下方的代码中执行此操作。

app.static('/media', 'media', { extensions: ['mp3'] }); app.static('/media', 'media', { extensions: ['mp3'] });

Here, adding the API endpoint and the directory in the root folder of the node project is mandatory.此处,必须添加 API 端点和节点项目根文件夹中的目录。

  1. Now, on the frontend you only have to add your complete url to access the static file from node project to the src attribute of html tag.现在,在前端,您只需添加完整的 url 即可从节点项目访问 static 文件到 html 标记的src属性。 Add controls attribute to the tag and html will handle everything for you.控件属性添加到标签,html 将为您处理一切。 You can play, pause, skip, etc.您可以播放、暂停、跳过等。

    <audio controls #audioElement id="id1" [src]="http://localhost:3000/media/audio-files/myAudiofile.mp3"> <音频控件#audioElement id="id1" [src]="http://localhost:3000/media/audio-files/myAudiofile.mp3">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM