Compare commits

..

No commits in common. "3979d358d0801c595cd0e1ba80f32becb9c177fb" and "19c568d27a5ff9ae34dec034015ba69339b3ab52" have entirely different histories.

5 changed files with 352 additions and 406 deletions

2
.gitignore vendored
View File

@ -1,5 +1,3 @@
dist/js/*.js dist/js/*.js
dist/css/*.css dist/css/*.css
dist/css/*.svg dist/css/*.svg
package-lock.json
node_modules

View File

@ -1,8 +0,0 @@
{
"js": {
"type": "terser",
"terser": {
"mangle": false
}
}
}

View File

@ -1,18 +0,0 @@
{
"name": "myplayer",
"version": "1.0.0",
"description": "A Web-MP3-Player with Playlist Support",
"main": "src/player.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://tk-sls.de/git/tk-sls.de/myplayer.git"
},
"author": "tilt <tilt@linuxfoo.de>",
"license": "MIT",
"dependencies": {
"minify": "^11.4.1"
}
}

View File

@ -12,8 +12,8 @@ if ! cd "$dir" ; then
elif ! mkdir -p "$jsdir" ; then elif ! mkdir -p "$jsdir" ; then
echo "ERROR: Could not create directory $jsdir; aborted." >&2 ; echo "ERROR: Could not create directory $jsdir; aborted." >&2 ;
exit 1 ; exit 1 ;
elif ! type minify > /dev/null ; then elif ! type yui-compressor > /dev/null ; then
echo "ERROR: Executable \"minify\" not found in PATH; aborted." >&2 ; echo "ERROR: Executable \"yui-compressor\" not found in PATH; aborted." >&2 ;
exit 1 ; exit 1 ;
fi fi
@ -47,7 +47,7 @@ do
done done
echo "INFO: Compressing player.js ..." >&2 echo "INFO: Compressing player.js ..." >&2
minify player.js >> "$jsdir"/myplayer.min.js yui-compressor player.js >> "$jsdir"/myplayer.min.js
chmod 644 "$jsdir"/myplayer.min.js chmod 644 "$jsdir"/myplayer.min.js

View File

@ -21,427 +21,401 @@
* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE */ * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE */
var MyPlayer = function(_args) { var MyPlayer = function(_args) {
var failed = false, var failed = false;
detached = false, var detached = false;
player = null, var 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 types = { var defaultArgs = {
"3g2": "video/3gpp2", detachable: true,
"3gp": "video/3gpp", element: 'body',
"7z": "application/x-7z-compressed", id3: false,
"aac": "audio/aac", mode: 'mp3url',
"abw": "application/x-abiword", waveform: false,
"arc": "application/x-freearc", autoplay: false,
"avi": "video/x-msvideo", loop: true,
"azw": "application/vnd.amazon.ebook", shuffle: false,
"bin": "application/octet-stream", list: []
"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"
};
return types[extension] || "application/octet-stream"; var mimeTypeFromSrc = function(src) {
}, var extension = src.split('.').pop().toLowerCase();
createAudio = function() {
var autoplay = args.autoplay ? 'autoplay="true"' : '';
var styles = [];
var style;
var key;
for (key in args.css) { return {
var value = args.css[key]; "aac": "audio/aac",
styles.push(`${key}: ${value}`); "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(
'<audio id="myplayer" controls="controls" ' + autoplay + '>'+
'<p>'+
'Your browser does not understand '+
'the &lt;audio&gt; tag. '+
'</p>'+
'</audio>'
);
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('<span class="ajaxLoader">&nbsp;</span>&nbsp;');
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);
});
} }
});
});
};
style = styles.join(" "); var playerCreated = function() {
args.audio = $(`<audio id="myplayer" controls="controls" style="${style}" ${autoplay}></audio>`); restoreParams();
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');
$(track).prepend('<span class="ajaxLoader">&nbsp;</span>&nbsp;'); if(args.id3)
readId3();
};
jsmediatags.read(url, { var createPlayer = function() {
onSuccess: function(tag) { player = new MediaElementPlayer(
var tags = tag.tags; "myplayer", {
var track_title; success: playerCreated,
features: ['playpause', 'current', 'progress', 'duration', 'volume', 'playlist', 'prevtrack', 'nexttrack', 'shuffle', 'loop']
});
if(typeof(tags.title)!='undefined') { $(args.element).append(
track_title = tags.title; '<p class="playerDetached" style="display: none;">'+
} 'The player is currently detached.'+
else { '</p>'
track_title = url; );
} };
if(typeof(tags.artist)!='undefined') { var param = function(name) {
track_title = tags.artist + ' - ' + track_title; var query = window.location.search.substring(1);
} var vars = query.split("&");
$(args.element).find('ul.mejs li').each(function(j, _track){ for(var i=0; i<vars.length; i++) {
if(i==j) { var pair = vars[i].split("=");
$(_track).html(track_title);
}
});
}
});
});
},
playerCreated = function(mediaElement, originalNode, instance) {
restoreParams();
if(args.shuffle) { if(pair[0] == name)
instance.shuffleCallback(); return pair[1];
} }
if(args.loop) { return false;
instance.loopCallback(); };
}
if(args.id3) { var setupDetach = function() {
readId3(); if(!args.detachable)
} return;
},
createPlayer = function() {
player = new MediaElementPlayer(
"myplayer", {
success: playerCreated,
features: [
'playpause',
'current',
'progress',
'duration',
'volume',
'playlist',
'prevtrack',
'nexttrack',
'shuffle',
'loop'
]
});
$(args.element).append( var popup = function(url) {
'<p class="playerDetached" style="display: none;">'+ player.pause();
'The player is currently detached.'+ $('.mejs-audio').hide();
'</p>' $('.playerDetached').show();
);
},
param = function(name) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for(i=0; i<vars.length; i++) { var playerWindow = window.open(
var pair = vars[i].split("="); url, 'myplayer',
'height='+ Math.max($(args.element).height(), 298)+','+
'width='+408+','+
'resizable=false'
);
if(pair[0] == name) { if(window.focus)
return pair[1]; playerWindow.focus();
}
}
return false; $("iframe",top.document).height(20);
},
setupDetach = function() {
if(!args.detachable) {
return;
}
var popup = function(url) { return false;
player.pause(); };
$('.mejs-audio').hide();
$('.playerDetached').show();
var playerWindow = window.open( if(param('popup')==false) {
url, 'myplayer', $('.mejs-controls').append(
'height='+ Math.max($(args.element).height(), 298)+','+ '<div id="popupButton" '+
'width='+408+','+ 'class="popupButton">'+
'resizable=false' '<button '+
); 'title="Open in separate window" '+
'type="button">'+
'</button>'+
'</div>'
);
if(window.focus) { $('.popupButton button').click(function(){
playerWindow.focus(); var currentTime = player.getCurrentTime();
} var volume = player.getVolume();
var paused = player.media.paused;
$("iframe",top.document).height(20); var trackNo = 0;
return false;
};
if(param('popup')==false) {
$('.mejs-controls').append(
'<div id="popupButton" '+
'class="popupButton">'+
'<button '+
'title="Open in separate window" '+
'type="button">'+
'</button>'+
'</div>'
);
$('.popupButton button').click(function(){
var currentTime = player.getCurrentTime();
var volume = player.getVolume();
var paused = player.media.paused;
var trackNo = 0;
$(args.element).find('ul.mejs li').each(function(i){
if($(this).hasClass('current')) {
trackNo = i;
}
});
popup(
$(location).attr('href')+'?'+
'popup&'+
'paused='+paused+'&'+
'currentTime='+currentTime+'&'+
'volume='+volume+'&'+
'trackNo='+trackNo
);
});
}
else {
var windowHeight = $(window).height();
window.onbeforeunload = function(){
$("iframe", opener.top.document).height(windowHeight);
$('.mejs-audio', opener.document).show();
$('.playerDetached', opener.document).hide();
};
}
},
restoreParams = function() {
if(!detached) {
return;
}
var paused = (param('paused') && param('paused')=='true');
var currentTime = param('currentTime') ? parseFloat(param('currentTime')) : 0.0;
var volume = param('volume') ? parseFloat(param('volume')) : 1.0;
var trackNo = param('trackNo') ? parseInt(param('trackNo')) : 0;
player.setVolume(volume);
$(args.element).find('ul.mejs li').each(function(i){ $(args.element).find('ul.mejs li').each(function(i){
if(i==trackNo) { if($(this).hasClass('current'))
player.playTrack($(this)); trackNo = i;
player.setCurrentTime(currentTime);
}
}); });
if(paused) { popup(
player.pause(); $(location).attr('href')+'?'+
} 'popup&'+
}; 'paused='+paused+'&'+
'currentTime='+currentTime+'&'+
'volume='+volume+'&'+
'trackNo='+trackNo
);
});
}
else {
var windowHeight = $(window).height();
if(param('popup')!==false) { window.onbeforeunload = function(){
detached = true; $("iframe", opener.top.document).height(windowHeight);
$('.mejs-audio', opener.document).show();
$('.playerDetached', opener.document).hide();
};
}
};
$('body').html( var restoreParams = function() {
'<div ' + if(!detached)
'id="myplayer" ' + return;
'style="' +
'width: ' + $(args.element).width() + 'px; '+
'height: ' + $(args.element).height() + ' px;'+
'"></div>'
);
args.element = '#myplayer'; var paused = (param('paused') && param('paused')=='true');
} var currentTime = param('currentTime') ? parseFloat(param('currentTime')) : 0.0;
var volume = param('volume') ? parseFloat(param('volume')) : 1.0;
var trackNo = param('trackNo') ? parseInt(param('trackNo')) : 0;
if(args.mode=='mp3url') { player.setVolume(volume);
console.log("args.mode=", args.mode);
var mp3url = (typeof args.mp3url === "undefined")
? $(location).attr('href').replace(/\?.*/, '').replace(/\/[^\/]+\/?$/, '')
: args.mp3url;
$.ajax({ $(args.element).find('ul.mejs li').each(function(i){
url: mp3url, if(i==trackNo) {
type: 'GET', player.playTrack($(this));
success: function(res) { player.setCurrentTime(currentTime);
var doc; }
var head; });
doc = document.createElement('html'); if(paused)
player.pause();
};
createAudio(); if(param('popup')!==false) {
doc.innerHTML = res; detached = true;
head = $(doc).find('a').each(function(idx,item) { $('body').html(
var src = $(item).attr('href'); '<div '+
'id="myplayer" '+
'style="'+
'width: '+$(args.element).width()+'px; '+
'height: '+$(args.element).height()+' px;'+
'"></div>'
);
if(src.match(/\.mp3$/)) { args.element = '#myplayer';
var url = mp3url+'/'+src; }
var title = item.innerHTML;
var type = "audio/mpeg";
args.audio.append(`<source src="${url}" type="${type}" title="${title}"/>`); if(args.mode=='mp3url') {
} var mp3url = (typeof args.mp3url === "undefined")
}); ? $(location).attr('href').replace(/\?.*/, '').replace(/\/[^\/]+\/?$/, '')
: args.mp3url;
createPlayer(); $.ajax({
setupDetach(); url: mp3url,
} type: 'GET',
}); success: function(res) {
} createAudio();
else if(args.mode=='m3u') {
var m3u = (typeof args.m3u === "undefined")
? $(location).attr('href').replace(/\?.*/, '').replace(/\/[^\/]+\/?$/, '/play.m3u')
: args.m3u;
createAudio(); var doc = document.createElement('html');
doc.innerHTML = res;
$.get(m3u, function(txt){ var head = $(doc).find('a').each(function(idx,item) {
var lines = txt.split("\n"); var src = $(item).attr('href');
var len = lines.length;
var i = 0;
while(i < len) { if(src.match(/\.mp3$/)) {
if(!lines[i].match(/^[ \t]*#/) && lines[i].match(/\.mp3$/)) { var url = mp3url+'/'+src;
var mp3 = lines[i]; var title = item.innerHTML;
var title = unescape(mp3.split('/').pop()); var type = "audio/mpeg";
var type = "audio/mpeg";
args.audio.append(`<source src="${mp3}" type="${type}" title="${title}"/>`); $(args.audio).append('<source src="'+url+'" type="' + type + '" title="'+title+'"/>');
} }
});
i++;
}
createPlayer(); createPlayer();
setupDetach(); setupDetach();
}); }
} });
else if(args.mode=='list') { }
var len = args.list.length; else if(args.mode=='m3u') {
var i = 0; var m3u = (typeof args.m3u === "undefined")
? $(location).attr('href').replace(/\?.*/, '').replace(/\/[^\/]+\/?$/, '/play.m3u')
: args.m3u;
createAudio(); createAudio();
while(i < len) { $.get(m3u, function(txt){
var item = args.list[i]; var lines = txt.split("\n");
var type = typeof item;
var src = '';
var title = '';
var type;
if(item !== null && type === 'object') { for(var i = 0, len = lines.length; i < len; i++)
if(item.url) { if(!lines[i].match(/^[ \t]*#/) && lines[i].match(/\.mp3$/)) {
src = item.url; var mp3 = lines[i];
} var title = unescape(mp3.split('/').pop());
else { var type = "audio/mpeg";
alert(`MyPlayer: item[${i}]: property "url" is missing.`);
i++;
continue;
}
if(item.title) { $(args.audio).append('<source src="'+mp3+'" type="' + type + '" title="'+title+'"/>');
title = item.title; }
}
else { console.log("DEBUG: mode==m3u audio=",$(args.audio));
title = url.replace(/(.*\/)?([^\/]+)/, '$2');
} createPlayer();
} setupDetach();
else if(type === 'string') { });
src = item; }
title = src; else if(args.mode=='list') {
} createAudio();
for(var i = 0, len = args.list.length; i < len; i++) {
var item = args.list[i];
var type = typeof item;
var src = '';
var title = '';
if(item !== null && type === 'object') {
if(item.url)
src = item.url;
else { else {
alert(`MyPlayer: item[${i}]: unsupported type=${type}.`); alert(
i++; 'MyPlayer: item[' + i + ']: '+
continue; 'property "url" is missing.'
);
continue;
} }
type = mimeTypeFromSrc(src); if(item.title)
title = item.title;
else
title = url.replace(/(.*\/)?([^\/]+)/, '$2');
}
else if(type === 'string') {
src = item;
title = src;
}
else {
alert(
'MyPlayer: item [' + i + ']: '+
'unsupported type "' + type + '"'
);
args.audio.append(`<source src="${src}" type="${type}" title="${type}"/>`); continue;
}
i++; var type = mimeTypeFromSrc(src);
}
createPlayer(); $(args.audio).append('<source src="'+src+'" type="' + type + '" title="'+title+'"/>');
setupDetach(); }
}
else { createPlayer();
$(args.element).append(`<p>Unsupported mode ${args.mode}"</p>`); setupDetach();
failed = true; }
} else {
$(args.element).append('<p>Unsupported mode "'+args.mode+'"</p>');
failed = true;
}
}; };