繁体   English   中英

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

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

我正在尝试将Node / Express后端的音频文件提供给Angular前端。

服务器端代码是:

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

客户端代码:

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

这会使文件全部恢复正常。

我正在尝试将其加载到Audio对象中,就像我放入文件引用一样。

var audio = new Audio(testData);

但该对象为空。 从我的理解,我从快递回来一个文件流对象,但我找不到如何把它变成可播放的音频。

编辑:是因为快速下载()只适用于非二进制数据?

谢谢!

使用此文件传送文件: https//github.com/obastemur/mediaserver

代码如下:

服务器:

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

客户:

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

在Express中,文件不会在响应中返回。 相反,可以通过网址访问:

服务器

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

客户

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

您没有阅读音频文档。

mySound = new Audio([URLString]);

音频构造函数采用URL,而不是原始数据。 您不需要事先下载音频文件:

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

服务器代码按原样工作。

暂无
暂无

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

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