1/** 2 * Returns the URL of a supported video source based on the user agent 3 * @param {string} base - media URL without file extension 4 * @returns {string} 5 */ 6function getVideoURI(base) 7{ 8 var extension = '.mp4'; 9 10 var videotag = document.createElement("video"); 11 12 if ( videotag.canPlayType && 13 videotag.canPlayType('video/ogg; codecs="theora, vorbis"') ) 14 { 15 extension = '.ogv'; 16 } 17 18 return base + extension; 19} 20 21/** 22 * Returns the URL of a supported audio source based on the user agent 23 * @param {string} base - media URL without file extension 24 * @returns {string} 25 */ 26function getAudioURI(base) 27{ 28 var extension = '.mp3'; 29 30 var audiotag = document.createElement("audio"); 31 32 if ( audiotag.canPlayType && 33 audiotag.canPlayType('audio/ogg') ) 34 { 35 extension = '.oga'; 36 } 37 38 return base + extension; 39} 40 41/** 42 * Returns the MIME type for a media URL based on the file extension. 43 * @param {string} url 44 * @returns {string} 45 */ 46function getMediaContentType(url) { 47 var extension = new URL(url, location).pathname.split(".").pop(); 48 var map = { 49 "mp4": "video/mp4", 50 "ogv": "video/ogg", 51 "mp3": "audio/mp3", 52 "oga": "audio/ogg", 53 }; 54 return map[extension]; 55} 56