diff --git a/src/player.js b/src/player.js index 740cf23..a6ba9ed 100644 --- a/src/player.js +++ b/src/player.js @@ -6,7 +6,7 @@ * restriction, including without limitation the rights to use, copy, * modify, merge, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. @@ -21,401 +21,427 @@ * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE */ var MyPlayer = function(_args) { - var failed = false; - var detached = false; - var player = null; + var failed = false, + detached = false, + player = null, + args_default = { + autoplay: false, + css: { "height": "500px" }, + detachable: true, + element: "body", + id3: false, + list: [], + loop: true, + mode: "mp3url", + shuffle: false, + waveform: false + }, + args = (_args===undefined) ? args_default : $.extend({}, args_default, _args), + mimeTypeFromSrc = function(src) { + var extension = src.split(".").pop().toLowerCase(); - var defaultArgs = { - detachable: true, - element: 'body', - id3: false, - mode: 'mp3url', - waveform: false, - autoplay: false, - loop: true, - shuffle: false, - list: [] - }; + var types = { + "3g2": "video/3gpp2", + "3gp": "video/3gpp", + "7z": "application/x-7z-compressed", + "aac": "audio/aac", + "abw": "application/x-abiword", + "arc": "application/x-freearc", + "avi": "video/x-msvideo", + "azw": "application/vnd.amazon.ebook", + "bin": "application/octet-stream", + "bmp": "image/bmp", + "bz": "application/x-bzip", + "bz2": "application/x-bzip2", + "cda": "application/x-cdf", + "csh": "application/x-csh", + "css": "text/css", + "csv": "text/csv", + "doc": "application/msword", + "eot": "application/vnd.ms-fontobject", + "epub": "application/epub+zip", + "gif": "image/gif", + "gz": "application/gzip", + "htm": "text/html", + "html": "text/html", + "ico": "image/vnd.microsoft.icon", + "ics": "text/calendar", + "jar": "application/java-archive", + "jpeg": "image/jpeg", + "jpg": "image/jpeg", + "js": "text/javascript", + "json": "application/json", + "jsonld": "application/ld+json", + "mid": "audio/midi audio/x-midi", + "midi": "audio/midi audio/x-midi", + "mjs": "text/javascript", + "mp3": "audio/mpeg", + "mp4": "video/mp4", + "mpeg": "video/mpeg", + "mpkg": "application/vnd.apple.installer+xml", + "odp": "application/vnd.oasis.opendocument.presentation", + "ods": "application/vnd.oasis.opendocument.spreadsheet", + "odt": "application/vnd.oasis.opendocument.text", + "oga": "audio/ogg", + "ogv": "video/ogg", + "ogx": "application/ogg", + "opus": "audio/opus", + "otf": "font/otf", + "pdf": "application/pdf", + "php": "application/x-httpd-php", + "png": "image/png", + "ppt": "application/vnd.ms-powerpoint", + "rar": "application/vnd.rar", + "rtf": "application/rtf", + "sh": "application/x-sh", + "svg": "image/svg+xml", + "swf": "application/x-shockwave-flash", + "tar": "application/x-tar", + "tif": "image/tiff", + "tiff": "image/tiff", + "ts": "video/mp2t", + "ttf": "font/ttf", + "txt": "text/plain", + "vsd": "application/vnd.visio", + "wav": "audio/wav", + "weba": "audio/webm", + "webm": "video/webm", + "webp": "image/webp", + "woff": "font/woff", + "woff2": "font/woff2", + "xhtml": "application/xhtml+xml", + "xls": "application/vnd.ms-excel", + "xml": "application/xml", + "xul": "application/vnd.mozilla.xul+xml", + "zip": "application/zip" + }; - var mimeTypeFromSrc = function(src) { - var extension = src.split('.').pop().toLowerCase(); + return types[extension] || "application/octet-stream"; + }, + createAudio = function() { + var autoplay = args.autoplay ? 'autoplay="true"' : ''; + var styles = []; + var style; + var key; - return { - "aac": "audio/aac", - "abw": "application/x-abiword", - "arc": "application/x-freearc", - "avi": "video/x-msvideo", - "azw": "application/vnd.amazon.ebook", - "bin": "application/octet-stream", - "bmp": "image/bmp", - "bz": "application/x-bzip", - "bz2": "application/x-bzip2", - "cda": "application/x-cdf", - "csh": "application/x-csh", - "css": "text/css", - "csv": "text/csv", - "doc": "application/msword", - "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", - "eot": "application/vnd.ms-fontobject", - "epub": "application/epub+zip", - "gz": "application/gzip", - "gif": "image/gif", - "htm": "text/html", - "html": "text/html", - "ico": "image/vnd.microsoft.icon", - "ics": "text/calendar", - "jar": "application/java-archive", - "jpeg": "image/jpeg", - "jpg": "image/jpeg", - "js": "text/javascript", - "json": "application/json", - "jsonld": "application/ld+json", - "mid": "audio/midi audio/x-midi", - "midi": "audio/midi audio/x-midi", - "mjs": "text/javascript", - "mp3": "audio/mpeg", - "mp4": "video/mp4", - "mpeg": "video/mpeg", - "mpkg": "application/vnd.apple.installer+xml", - "odp": "application/vnd.oasis.opendocument.presentation", - "ods": "application/vnd.oasis.opendocument.spreadsheet", - "odt": "application/vnd.oasis.opendocument.text", - "oga": "audio/ogg", - "ogv": "video/ogg", - "ogx": "application/ogg", - "opus": "audio/opus", - "otf": "font/otf", - "png": "image/png", - "pdf": "application/pdf", - "php": "application/x-httpd-php", - "ppt": "application/vnd.ms-powerpoint", - "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", - "rar": "application/vnd.rar", - "rtf": "application/rtf", - "sh": "application/x-sh", - "svg": "image/svg+xml", - "swf": "application/x-shockwave-flash", - "tar": "application/x-tar", - "tif": "image/tiff", - "tiff": "image/tiff", - "ts": "video/mp2t", - "ttf": "font/ttf", - "txt": "text/plain", - "vsd": "application/vnd.visio", - "wav": "audio/wav", - "weba": "audio/webm", - "webm": "video/webm", - "webp": "image/webp", - "woff": "font/woff", - "woff2": "font/woff2", - "xhtml": "application/xhtml+xml", - "xls": "application/vnd.ms-excel", - "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", - "xml": "application/xml", - "xul": "application/vnd.mozilla.xul+xml", - "zip": "application/zip", - "3gp": "video/3gpp", - "3g2": "video/3gpp2", - "7z": "application/x-7z-compressed" - }[extension] || "application/octet-stream"; - }; - - var args = (typeof(_args)==='undefined') - ? defaultArgs - : $.extend({}, defaultArgs, _args); - - var createAudio = function() { - var autoplay = args.autoplay ? 'autoplay="true"' : ''; - - $(args.element).append( - '' - ); - - args.audio = args.element + ' audio'; - - $(args.audio).css({ - 'height': '500px', - }); - }; - - var readId3 = function() { - $(args.element).find('ul.mejs li').each(function(i, track){ - var url = $(track).attr('data-url'); - - $(track).prepend(' '); - - jsmediatags.read(url, { - onSuccess: function(tag) { - var tags = tag.tags; - var track_title; - - if(typeof(tags.title)!='undefined') - track_title = tags.title; - else - track_title = url; - - if(typeof(tags.artist)!='undefined') - track_title = tags.artist + ' - ' + track_title; - - $(args.element).find('ul.mejs li').each(function(j, _track){ - if(i==j) - $(_track).html(track_title); - }); + for (key in args.css) { + var value = args.css[key]; + styles.push(`${key}: ${value}`); } - }); - }); - }; - var playerCreated = function() { - restoreParams(); - - if(args.id3) - readId3(); - }; + style = styles.join(" "); + args.audio = $(``); + console.log("DEBUG: appending audio=", args.audio, " to element=", args.element, " ..."); + $(args.element).append(args.audio); + }, + readId3 = function() { + $(args.element).find('ul.mejs li').each(function(i, track){ + var url = $(track).attr('data-url'); - var createPlayer = function() { - player = new MediaElementPlayer( - "myplayer", { - success: playerCreated, - features: ['playpause', 'current', 'progress', 'duration', 'volume', 'playlist', 'prevtrack', 'nexttrack', 'shuffle', 'loop'] - }); + $(track).prepend(' '); - $(args.element).append( - '
' - ); - }; + jsmediatags.read(url, { + onSuccess: function(tag) { + var tags = tag.tags; + var track_title; - var param = function(name) { - var query = window.location.search.substring(1); - var vars = query.split("&"); + if(typeof(tags.title)!='undefined') { + track_title = tags.title; + } + else { + track_title = url; + } - for(var i=0; iUnsupported mode "'+args.mode+'"
'); - failed = true; - } + createPlayer(); + setupDetach(); + } + else { + $(args.element).append(`Unsupported mode ${args.mode}"
`); + failed = true; + } };