[英]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.