简体   繁体   English

Express,Node,Angular将音频文件发送到前端

[英]Express, Node, Angular sending audio file to front end

I'm trying to serve audio files from a Node/Express back end, to an Angular front end. 我正在尝试将Node / Express后端的音频文件提供给Angular前端。

The server side code is: 服务器端代码是:

var file = "/filepath.wav";
res.download(file, 'testAudio.wav');

The client side code: 客户端代码:

var testData = null;

this.getBasicAudio = function(){
  console.log("Requesting File");
  $http.get('/getBasicAudio', "FilePathRequest")
  //Success, return the file
  .success(function(data){
    console.log("File retrive Successful");
    testData = data;
    return true;
  })
  //Error, file not retrived 
  .error(function(data){
    console.log("File retrive Failed");
    return false;
  });
};

This returns the file all ok. 这会使文件全部恢复正常。

I'm trying to load this into a Audio object, as if I was putting in the file reference. 我正在尝试将其加载到Audio对象中,就像我放入文件引用一样。

var audio = new Audio(testData);

But the object is null. 但该对象为空。 From what I understand I'm getting back a filestream object from express, but I can't find how to turn this into playable audio. 从我的理解,我从快递回来一个文件流对象,但我找不到如何把它变成可播放的音频。

edit: Is it because express download() only works with non-binary data?? 编辑:是因为快速下载()只适用于非二进制数据?

Thanks! 谢谢!

Got a file being delivered using this: https://github.com/obastemur/mediaserver 使用此文件传送文件: https//github.com/obastemur/mediaserver

Code looks like: 代码如下:

Server: 服务器:

http.createServer(function(req,res){
  ms.pipe(req,res,"../../Node/TWLW/audio/examples/testAudio.mp3");
}).listen(1337, '127.0.0.1');

Client: 客户:

var audio = new Audio('http://127.0.0.1:1337/');
audio.play();

In Express the files doesn't get returned in the response. 在Express中,文件不会在响应中返回。 Instead can be accessed by the url: 相反,可以通过网址访问:

Server 服务器

app.get('/music.mp3',function(req,res){
  ms.pipe(req, res, '../../Node/TWLW/audio/examples/testAudio.mp3');
});

Client 客户

var audio = new Audio('http://127.0.0.1:8080/testAudio.mp3');

You didn't read Audio documentation. 您没有阅读音频文档。

mySound = new Audio([URLString]); mySound = new Audio([URLString]);

Audio constructor takes an URL, not raw data. 音频构造函数采用URL,而不是原始数据。 You don't need to download the audio file beforehand: 您不需要事先下载音频文件:

var audio = new Audio('/getBasicAudio');
audio.play(); // your audio file should play

The server code works as it is. 服务器代码按原样工作。

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

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