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