We have a lot of problems with our software (which is mostly used in schools) and firewalls: we use mp3s within our resources, and mp3s are blocked in a lot of school firewalls. I'm trying to write some js code to detect when this is happening, so we can alert the user (teachers) to speak to their IT department and get it unblocked.
I went down a road initially of trying to play an short mp3 of silence, and detect whether that had happened. But, you have to jump through so many hoops to play audio automatically, especially on iOS Safari - waiting for user interaction, etc, that the hoop-jumping stuff was just getting in the way of the tests, and making the code think mp3s were being blocked when they were actually just being prevented from being played, by the browser rather than the network.
So, i'm abandoning the idea of playing mp3s. What I want to do instead is to request a small mp3 file over the network, and then analyse the contents of the response, to make sure that it is actually an mp3 file, and not, say, an html page saying "Sorry, that content is blocked", or whatever other things firewalls may return when someone asks for a blocked file.
I can't actually think what the simplest way to do this is, which will work cross browser. I'm using jQuery, and I thought that I might be able to do something in the "success" block of a jQuery ajax call, to analyse the data that I get back, to check that it's an mp3 file.
The first thing I thought was to just pull the data into a div on the page, like so:
var url = "/assets/test_media_files/silence.mp3?t="+Math.random();
$("#content-test-file-data").load(url, function(){
console.log("loaded data");
});
This is working, and I have a load of binary data gibberish in the #content-test-file-data
div. But, how can I tell if this is an mp3 file? Is there a nicer way to do this? Do I need to request the data with a mime type, or something like that? thanks