mardi 10 février 2015

nodejs can't play audio from URL

I've googled now for half day and I guess the problem is that I don't know what the real problem is, so I fail to google it right!


Better to bring the code in:



<!doctype html>
<html>
<head><title>test</title></head>
<body>
<audio controls>
<source src="http://ift.tt/1CfMjzy" type="audio/mpeg">
</audio>
</body>
</html>


If you save this as an html file and open it in a browser (tested on Firefox / Windows) the audio will be played with no problem.


Now I was to start a nodejs application and created this proof-of-concept server:



var express = require('express'),
app = express(),
bodyparser = require('body-parser'),
methodOverride = require('method-override'),
morgan = require('morgan'),
port = Number(process.env.PORT || 8090);

app.use(express.static(__dirname + '/public'));
app.use(bodyparser.json());
app.use(bodyparser.json({'type':'application/vnd.api+json'}));
app.use(bodyparser.urlencoded({'extended':'true'}));
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(morgan('dev'));

app.get('*', function(req, res){
res.render('index');
});

app.listen(port, function(){
console.log('running port: ' + port);
});


When I run it, I get these errors:



HTTP load failed with status 404. Load of media resource http://ift.tt/1DgyUbJ failed.
All candidate resources failed to load. Media load paused.


I was thinking of it as a CORS problem, but then I proved to myself that it's a wrong guess very simply, by just brining a url to an mp3 file:



http://ift.tt/1CfMg6T


and it worked! If I provide a URL to an mp3 file, both simple html file and the one served by nodejs work fine. I then tried adding type to the audio source tag:



type="audio/mpeg"


which proved to have no effect here.


So what the real problem is please?


Aucun commentaire:

Enregistrer un commentaire