Reputation: 51
I'm trying to serve audio files from a Node/Express back end, to an Angular front end.
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.
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!
Upvotes: 4
Views: 7064
Reputation: 51
Got a file being delivered using this: 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. 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');
Upvotes: 1
Reputation: 13672
You didn't read Audio documentation.
mySound = new Audio([URLString]);
Audio constructor takes an URL, not raw data. 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.
Upvotes: -1