mirror of
https://github.com/jellyfin/jellyfin-web.git
synced 2024-11-18 11:28:23 -07:00
1340 lines
43 KiB
JavaScript
1340 lines
43 KiB
JavaScript
(function (document, setTimeout, clearTimeout, screen, localStorage, $, setInterval, window) {
|
|
|
|
function mediaPlayer() {
|
|
|
|
var self = this;
|
|
|
|
var testableAudioElement = document.createElement('audio');
|
|
var testableVideoElement = document.createElement('video');
|
|
var currentMediaElement;
|
|
var currentProgressInterval;
|
|
var positionSlider;
|
|
var isPositionSliderActive;
|
|
var currentTimeElement;
|
|
var currentItem;
|
|
var volumeSlider;
|
|
var muteButton;
|
|
var unmuteButton;
|
|
var startTimeTicksOffset;
|
|
var curentDurationTicks;
|
|
var isStaticStream;
|
|
var culturesPromise;
|
|
|
|
self.playing = '';
|
|
self.queue = [];
|
|
|
|
function requestFullScreen(element) {
|
|
// Supports most browsers and their versions.
|
|
var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
|
|
|
|
if (requestMethod) { // Native full screen.
|
|
requestMethod.call(element);
|
|
} else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
|
|
var wscript = new ActiveXObject("WScript.Shell");
|
|
if (wscript !== null) {
|
|
wscript.SendKeys("{F11}");
|
|
}
|
|
}
|
|
}
|
|
|
|
function isFullScreen() {
|
|
return document.fullscreenEnabled || document.mozFullscreenEnabled || document.webkitIsFullScreen || document.mozFullScreen ? true : false;
|
|
}
|
|
|
|
$(document).on('webkitfullscreenchange mozfullscreenchange fullscreenchange', function () {
|
|
|
|
if (isFullScreen()) {
|
|
$('.itemVideo').addClass('fullscreenVideo');
|
|
} else {
|
|
$('.itemVideo').removeClass('fullscreenVideo');
|
|
}
|
|
|
|
});
|
|
|
|
function replaceQueryString(url, param, value) {
|
|
var re = new RegExp("([?|&])" + param + "=.*?(&|$)", "i");
|
|
if (url.match(re))
|
|
return url.replace(re, '$1' + param + "=" + value + '$2');
|
|
else
|
|
return url + '&' + param + "=" + value;
|
|
}
|
|
|
|
function updateVolumeButtons(vol) {
|
|
|
|
if (vol) {
|
|
muteButton.show();
|
|
unmuteButton.hide();
|
|
} else {
|
|
muteButton.hide();
|
|
unmuteButton.show();
|
|
}
|
|
}
|
|
|
|
function getCurrentTicks(mediaElement) {
|
|
return Math.floor(10000000 * (mediaElement || currentMediaElement).currentTime) + startTimeTicksOffset;
|
|
}
|
|
|
|
function onPlaybackStopped() {
|
|
|
|
currentTimeElement.empty();
|
|
|
|
var endTime = this.currentTime;
|
|
|
|
this.currentTime = 0;
|
|
|
|
clearProgressInterval();
|
|
|
|
var position = Math.floor(10000000 * endTime) + startTimeTicksOffset;
|
|
|
|
ApiClient.reportPlaybackStopped(Dashboard.getCurrentUserId(), currentItem.Id, position);
|
|
|
|
MediaPlayer.queuePlayNext();
|
|
}
|
|
|
|
function startProgressInterval(itemId) {
|
|
|
|
clearProgressInterval();
|
|
|
|
var intervalTime = ApiClient.isWebSocketOpen() ? 10000 : 30000;
|
|
|
|
currentProgressInterval = setInterval(function () {
|
|
|
|
if (currentMediaElement) {
|
|
sendProgressUpdate(itemId);
|
|
}
|
|
|
|
}, intervalTime);
|
|
}
|
|
|
|
function sendProgressUpdate(itemId) {
|
|
|
|
ApiClient.reportPlaybackProgress(Dashboard.getCurrentUserId(), itemId, getCurrentTicks());
|
|
}
|
|
|
|
function clearProgressInterval() {
|
|
|
|
if (currentProgressInterval) {
|
|
clearTimeout(currentProgressInterval);
|
|
currentProgressInterval = null;
|
|
}
|
|
}
|
|
|
|
function changeStream(ticks, params) {
|
|
|
|
var element = currentMediaElement;
|
|
|
|
if (isStaticStream && params == null) {
|
|
|
|
element.currentTime = ticks / (1000 * 10000);
|
|
|
|
} else {
|
|
|
|
params = params || {};
|
|
|
|
var currentSrc = element.currentSrc;
|
|
|
|
currentSrc = replaceQueryString(currentSrc, 'starttimeticks', ticks);
|
|
|
|
if (params.AudioStreamIndex != null) {
|
|
currentSrc = replaceQueryString(currentSrc, 'AudioStreamIndex', params.AudioStreamIndex);
|
|
}
|
|
if (params.SubtitleStreamIndex != null) {
|
|
currentSrc = replaceQueryString(currentSrc, 'SubtitleStreamIndex', params.SubtitleStreamIndex);
|
|
}
|
|
if (params.MaxWidth != null) {
|
|
currentSrc = replaceQueryString(currentSrc, 'MaxWidth', params.MaxWidth);
|
|
}
|
|
if (params.VideoBitrate != null) {
|
|
currentSrc = replaceQueryString(currentSrc, 'VideoBitrate', params.VideoBitrate);
|
|
}
|
|
|
|
clearProgressInterval();
|
|
|
|
$(element).off('ended.playbackstopped').on("play.onceafterseek", function () {
|
|
|
|
$(this).off('play.onceafterseek').on('ended.playbackstopped', onPlaybackStopped);
|
|
startProgressInterval(currentItem.Id);
|
|
sendProgressUpdate(currentItem.Id);
|
|
|
|
});
|
|
startTimeTicksOffset = ticks;
|
|
|
|
element.src = currentSrc;
|
|
}
|
|
}
|
|
|
|
function onPositionSliderChange() {
|
|
|
|
isPositionSliderActive = false;
|
|
|
|
var newPercent = parseInt(this.value);
|
|
|
|
var newPositionTicks = (newPercent / 100) * currentItem.RunTimeTicks;
|
|
|
|
changeStream(newPositionTicks);
|
|
}
|
|
|
|
$(function () {
|
|
|
|
muteButton = $('#muteButton');
|
|
unmuteButton = $('#unmuteButton');
|
|
|
|
currentTimeElement = $('.currentTime');
|
|
|
|
volumeSlider = $('.volumeSlider').on('change', function () {
|
|
|
|
var vol = this.value;
|
|
updateVolumeButtons(vol);
|
|
currentMediaElement.volume = vol;
|
|
});
|
|
|
|
$(".jqueryuislider").slider({ orientation: "horizontal" });
|
|
|
|
positionSlider = $(".positionSlider").on('mousedown', function () {
|
|
|
|
isPositionSliderActive = true;
|
|
|
|
});
|
|
|
|
if ($.browser.mozilla) {
|
|
|
|
positionSlider.on('change', onPositionSliderChange);
|
|
|
|
} else {
|
|
|
|
positionSlider.on('change', function () {
|
|
|
|
isPositionSliderActive = true;
|
|
|
|
setCurrentTimePercent(parseInt(this.value), currentItem);
|
|
|
|
}).on('changed', onPositionSliderChange);
|
|
}
|
|
|
|
|
|
(function (el, timeout) {
|
|
var timer, trig = function () { el.trigger("changed"); };
|
|
el.bind("change", function () {
|
|
if (timer) {
|
|
clearTimeout(timer);
|
|
}
|
|
timer = setTimeout(trig, timeout);
|
|
});
|
|
})(positionSlider, 500);
|
|
|
|
$('#chaptersFlyout').on('click', '.mediaFlyoutOption', function () {
|
|
|
|
var ticks = parseInt(this.getAttribute('data-positionticks'));
|
|
|
|
changeStream(ticks);
|
|
|
|
hideFlyout($('#chaptersFlyout'));
|
|
});
|
|
|
|
$('#audioTracksFlyout').on('click', '.mediaFlyoutOption', function () {
|
|
|
|
if (!$(this).hasClass('selectedMediaFlyoutOption')) {
|
|
var index = parseInt(this.getAttribute('data-index'));
|
|
|
|
changeStream(getCurrentTicks(), { AudioStreamIndex: index });
|
|
}
|
|
|
|
hideFlyout($('#audioTracksFlyout'));
|
|
});
|
|
|
|
$('#subtitleFlyout').on('click', '.mediaFlyoutOption', function () {
|
|
|
|
if (!$(this).hasClass('selectedMediaFlyoutOption')) {
|
|
var index = parseInt(this.getAttribute('data-index'));
|
|
|
|
changeStream(getCurrentTicks(), { SubtitleStreamIndex: index });
|
|
}
|
|
|
|
hideFlyout($('#subtitleFlyout'));
|
|
});
|
|
|
|
$('#qualityFlyout').on('click', '.mediaFlyoutOption', function () {
|
|
|
|
if (!$(this).hasClass('selectedMediaFlyoutOption')) {
|
|
|
|
var maxWidth = parseInt(this.getAttribute('data-maxwidth'));
|
|
var videoBitrate = parseInt(this.getAttribute('data-videobitrate'));
|
|
|
|
changeStream(getCurrentTicks(), { MaxWidth: maxWidth, VideoBitrate: videoBitrate });
|
|
}
|
|
|
|
hideFlyout($('#qualityFlyout'));
|
|
});
|
|
});
|
|
|
|
function endsWith(text, pattern) {
|
|
|
|
text = text.toLowerCase();
|
|
pattern = pattern.toLowerCase();
|
|
|
|
var d = text.length - pattern.length;
|
|
return d >= 0 && text.lastIndexOf(pattern) === d;
|
|
}
|
|
|
|
function setCurrentTimePercent(percent, item) {
|
|
|
|
var position = (percent / 100) * curentDurationTicks;
|
|
setCurrentTime(position, item, false);
|
|
}
|
|
|
|
function setCurrentTime(ticks, item, updateSlider) {
|
|
|
|
// Convert to ticks
|
|
ticks = Math.floor(ticks);
|
|
|
|
var timeText = DashboardPage.getDisplayText(ticks);
|
|
|
|
if (curentDurationTicks) {
|
|
|
|
timeText += " / " + DashboardPage.getDisplayText(curentDurationTicks);
|
|
|
|
if (updateSlider) {
|
|
var percent = ticks / curentDurationTicks;
|
|
percent *= 100;
|
|
|
|
positionSlider.val(percent);
|
|
|
|
positionSlider.removeAttr('disabled');
|
|
}
|
|
} else {
|
|
positionSlider.attr('disabled', 'disabled');
|
|
}
|
|
|
|
currentTimeElement.html(timeText);
|
|
}
|
|
|
|
function playAudio(item, params) {
|
|
|
|
var baseParams = {
|
|
audioChannels: 2,
|
|
audioBitrate: 128000
|
|
};
|
|
|
|
$.extend(baseParams, params);
|
|
|
|
var mp3Url = ApiClient.getUrl('Audio/' + item.Id + '/stream.mp3', $.extend({}, baseParams, {
|
|
audioCodec: 'mp3'
|
|
}));
|
|
|
|
var aacUrl = ApiClient.getUrl('Audio/' + item.Id + '/stream.aac', $.extend({}, baseParams, {
|
|
audioCodec: 'aac'
|
|
}));
|
|
|
|
var webmUrl = ApiClient.getUrl('Audio/' + item.Id + '/stream.webm', $.extend({}, baseParams, {
|
|
audioCodec: 'Vorbis'
|
|
}));
|
|
|
|
var mediaStreams = item.MediaStreams || [];
|
|
|
|
for (var i = 0, length = mediaStreams.length; i < length; i++) {
|
|
|
|
var stream = mediaStreams[i];
|
|
|
|
if (stream.Type == "Audio") {
|
|
|
|
// Stream statically when possible
|
|
if (endsWith(item.Path, ".aac") && stream.BitRate <= 256000) {
|
|
aacUrl += "&static=true";
|
|
}
|
|
else if (endsWith(item.Path, ".mp3") && stream.BitRate <= 256000) {
|
|
mp3Url += "&static=true";
|
|
}
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
var html = '';
|
|
|
|
var requiresControls = $.browser.android || $.browser.iphone || $.browser.ipad;
|
|
|
|
// Can't autoplay in these browsers so we need to use the full controls
|
|
if (requiresControls) {
|
|
html += '<audio preload="auto" autoplay controls>';
|
|
} else {
|
|
html += '<audio preload="auto" style="display:none;" autoplay>';
|
|
}
|
|
html += '<source type="audio/mpeg" src="' + mp3Url + '" />';
|
|
html += '<source type="audio/aac" src="' + aacUrl + '" />';
|
|
html += '<source type="audio/webm" src="' + webmUrl + '" />';
|
|
html += '</audio';
|
|
|
|
var nowPlayingBar = $('#nowPlayingBar').show();
|
|
//show stop button
|
|
$('#stopButton', nowPlayingBar).show();
|
|
$('#playButton', nowPlayingBar).hide();
|
|
$('#pauseButton', nowPlayingBar).show();
|
|
$('#fullscreenButton', nowPlayingBar).hide();
|
|
|
|
if (requiresControls) {
|
|
$('#previousTrackButton', nowPlayingBar).hide();
|
|
$('#nextTrackButton', nowPlayingBar).hide();
|
|
$('#playlistButton', nowPlayingBar).hide();
|
|
} else {
|
|
$('#previousTrackButton', nowPlayingBar).show();
|
|
$('#nextTrackButton', nowPlayingBar).show();
|
|
$('#playlistButton', nowPlayingBar).show();
|
|
}
|
|
|
|
$('#qualityButton', nowPlayingBar).hide();
|
|
$('#audioTracksButton', nowPlayingBar).hide();
|
|
$('#subtitleButton', nowPlayingBar).hide();
|
|
$('#chaptersButton', nowPlayingBar).hide();
|
|
|
|
$('#mediaElement', nowPlayingBar).html(html);
|
|
var audioElement = $("audio", nowPlayingBar);
|
|
|
|
var initialVolume = localStorage.getItem("volume") || 0.5;
|
|
|
|
audioElement.each(function () {
|
|
this.volume = initialVolume;
|
|
});
|
|
|
|
volumeSlider.val(initialVolume);
|
|
updateVolumeButtons(initialVolume);
|
|
|
|
audioElement.on("volumechange", function () {
|
|
|
|
var vol = this.volume;
|
|
|
|
localStorage.setItem("volume", vol);
|
|
|
|
updateVolumeButtons(vol);
|
|
|
|
}).on("play.once", function () {
|
|
|
|
if (!requiresControls) {
|
|
audioElement.hide();
|
|
}
|
|
|
|
var duration = this.duration;
|
|
isStaticStream = duration && !isNaN(duration) && duration != Number.POSITIVE_INFINITY && duration != Number.NEGATIVE_INFINITY;
|
|
|
|
audioElement.off("play.once");
|
|
|
|
ApiClient.reportPlaybackStart(Dashboard.getCurrentUserId(), item.Id);
|
|
|
|
startProgressInterval(item.Id);
|
|
|
|
}).on("pause", function () {
|
|
|
|
$('#playButton', nowPlayingBar).show();
|
|
$('#pauseButton', nowPlayingBar).hide();
|
|
|
|
}).on("playing", function () {
|
|
|
|
$('#playButton', nowPlayingBar).hide();
|
|
$('#pauseButton', nowPlayingBar).show();
|
|
|
|
}).on("timeupdate", function () {
|
|
|
|
if (!isPositionSliderActive) {
|
|
|
|
setCurrentTime(getCurrentTicks(this), item, true);
|
|
}
|
|
|
|
}).on("ended.playbackstopped", onPlaybackStopped);
|
|
|
|
MediaPlayer.nowPlaying(item);
|
|
|
|
currentItem = item;
|
|
curentDurationTicks = item.RunTimeTicks;
|
|
|
|
return audioElement[0];
|
|
}
|
|
|
|
function playVideo(item, startPosition, user) {
|
|
|
|
//stop/kill videoJS
|
|
if (currentMediaElement) self.stop();
|
|
|
|
// Account for screen rotation. Use the larger dimension as the width.
|
|
var screenWidth = Math.max(screen.height, screen.width);
|
|
|
|
var baseParams = {
|
|
audioChannels: 2,
|
|
audioBitrate: 128000,
|
|
videoBitrate: 2000000,
|
|
maxWidth: Math.min(screenWidth, 1280),
|
|
StartTimeTicks: 0,
|
|
SubtitleStreamIndex: getInitialSubtitleStreamIndex(item.MediaStreams, user),
|
|
AudioStreamIndex: getInitialAudioStreamIndex(item.MediaStreams, user)
|
|
};
|
|
|
|
var videoStream = item.MediaStreams.filter(function (i) {
|
|
return i.Type == "Video";
|
|
})[0];
|
|
|
|
if (videoStream && videoStream.Width) {
|
|
|
|
if (videoStream.Width >= 1280) {
|
|
baseParams.videoBitrate = 2000000;
|
|
}
|
|
|
|
else if (videoStream.Width >= 720) {
|
|
baseParams.videoBitrate = 400000;
|
|
}
|
|
}
|
|
|
|
if (startPosition) {
|
|
baseParams.StartTimeTicks = startPosition;
|
|
}
|
|
|
|
var mp4VideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.mp4', $.extend({}, baseParams, {
|
|
videoCodec: 'h264',
|
|
audioCodec: 'aac',
|
|
profile: 'baseline',
|
|
level: 3
|
|
}));
|
|
|
|
var webmVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.webm', $.extend({}, baseParams, {
|
|
videoCodec: 'vpx',
|
|
audioCodec: 'Vorbis'
|
|
}));
|
|
|
|
var hlsVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.m3u8', $.extend({}, baseParams, {
|
|
videoCodec: 'h264',
|
|
audioCodec: 'aac',
|
|
profile: 'baseline',
|
|
level: 3
|
|
}));
|
|
|
|
var html = '';
|
|
|
|
// HLS must be at the top for safari
|
|
// Webm must be ahead of mp4 due to the issue of mp4 playing too fast in chrome
|
|
|
|
// Can't autoplay in these browsers so we need to use the full controls
|
|
if ($.browser.msie || $.browser.android || $.browser.iphone || $.browser.ipad) {
|
|
html += '<video class="itemVideo" autoplay controls preload="auto">';
|
|
} else {
|
|
html += '<video class="itemVideo" autoplay preload="auto">';
|
|
}
|
|
|
|
html += '<source type="application/x-mpegURL" src="' + hlsVideoUrl + '" />';
|
|
html += '<source type="video/webm" src="' + webmVideoUrl + '" />';
|
|
html += '<source type="video/mp4" src="' + mp4VideoUrl + '" />';
|
|
html += '</video';
|
|
|
|
var nowPlayingBar = $('#nowPlayingBar').show();
|
|
//show stop button
|
|
$('#stopButton', nowPlayingBar).show();
|
|
$('#playButton', nowPlayingBar).hide();
|
|
$('#pauseButton', nowPlayingBar).show();
|
|
$('#playlistButton', nowPlayingBar).hide();
|
|
$('#previousTrackButton', nowPlayingBar).hide();
|
|
$('#nextTrackButton', nowPlayingBar).hide();
|
|
$('#mediaElement', nowPlayingBar).html(html);
|
|
|
|
$('#qualityButton', nowPlayingBar).show();
|
|
|
|
if (item.MediaStreams.filter(function (i) {
|
|
return i.Type == "Audio";
|
|
}).length) {
|
|
$('#audioTracksButton', nowPlayingBar).show();
|
|
} else {
|
|
$('#audioTracksButton', nowPlayingBar).hide();
|
|
}
|
|
|
|
if (item.MediaStreams.filter(function (i) {
|
|
return i.Type == "Subtitle";
|
|
}).length) {
|
|
$('#subtitleButton', nowPlayingBar).show();
|
|
} else {
|
|
$('#subtitleButton', nowPlayingBar).hide();
|
|
}
|
|
|
|
if (item.Chapters.length) {
|
|
$('#chaptersButton', nowPlayingBar).show();
|
|
} else {
|
|
$('#chaptersButton', nowPlayingBar).hide();
|
|
}
|
|
|
|
if ($.browser.msie) {
|
|
$('#fullscreenButton', nowPlayingBar).hide();
|
|
} else {
|
|
$('#fullscreenButton', nowPlayingBar).show();
|
|
}
|
|
|
|
var videoElement = $("video", nowPlayingBar);
|
|
|
|
var initialVolume = localStorage.getItem("volume") || 0.5;
|
|
|
|
videoElement.each(function () {
|
|
this.volume = initialVolume;
|
|
});
|
|
|
|
volumeSlider.val(initialVolume);
|
|
updateVolumeButtons(initialVolume);
|
|
|
|
videoElement.on("volumechange", function () {
|
|
|
|
var vol = this.volume;
|
|
|
|
localStorage.setItem("volume", vol);
|
|
|
|
updateVolumeButtons(vol);
|
|
|
|
}).on("play.once", function () {
|
|
|
|
var duration = this.duration;
|
|
isStaticStream = duration && !isNaN(duration) && duration != Number.POSITIVE_INFINITY && duration != Number.NEGATIVE_INFINITY;
|
|
|
|
videoElement.off("play.once");
|
|
|
|
ApiClient.reportPlaybackStart(Dashboard.getCurrentUserId(), item.Id);
|
|
|
|
startProgressInterval(item.Id);
|
|
|
|
}).on("pause", function () {
|
|
|
|
$('#playButton', nowPlayingBar).show();
|
|
$('#pauseButton', nowPlayingBar).hide();
|
|
|
|
}).on("playing", function () {
|
|
|
|
$('#playButton', nowPlayingBar).hide();
|
|
$('#pauseButton', nowPlayingBar).show();
|
|
|
|
}).on("timeupdate", function () {
|
|
|
|
if (!isPositionSliderActive) {
|
|
|
|
setCurrentTime(getCurrentTicks(this), item, true);
|
|
}
|
|
|
|
}).on("ended.playbackstopped", onPlaybackStopped);
|
|
|
|
MediaPlayer.nowPlaying(item);
|
|
|
|
currentItem = item;
|
|
curentDurationTicks = item.RunTimeTicks;
|
|
|
|
return videoElement[0];
|
|
};
|
|
|
|
function getInitialAudioStreamIndex(mediaStreams, user) {
|
|
|
|
var audioStreams = mediaStreams.filter(function (stream) {
|
|
return stream.Type == "Audio";
|
|
});
|
|
|
|
if (user.Configuration.AudioLanguagePreference) {
|
|
|
|
for (var i = 0, length = audioStreams.length; i < length; i++) {
|
|
var mediaStream = audioStreams[i];
|
|
|
|
if (mediaStream.Type == "Audio" && mediaStream.Language == user.Configuration.AudioLanguagePreference) {
|
|
return mediaStream.Index;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
// Just use the first audio stream
|
|
return audioStreams.length ? audioStreams[0].Index : null;
|
|
}
|
|
|
|
function getInitialSubtitleStreamIndex(mediaStreams, user) {
|
|
|
|
var i, length, mediaStream;
|
|
|
|
// Find the first forced subtitle stream
|
|
for (i = 0, length = mediaStreams.length; i < length; i++) {
|
|
mediaStream = mediaStreams[i];
|
|
|
|
if (mediaStream.Type == "Subtitle" && mediaStream.IsForced) {
|
|
return mediaStream.Index;
|
|
}
|
|
|
|
}
|
|
|
|
// If none then look at user configuration
|
|
if (user.Configuration.AudioLanguagePreference) {
|
|
|
|
for (i = 0, length = mediaStreams.length; i < length; i++) {
|
|
mediaStream = mediaStreams[i];
|
|
|
|
if (mediaStream.Type == "Audio" && mediaStream.Language == user.Configuration.AudioLanguagePreference) {
|
|
|
|
if (user.Configuration.UseForcedSubtitlesOnly) {
|
|
|
|
if (mediaStream.IsForced) {
|
|
return mediaStream.Index;
|
|
}
|
|
|
|
} else {
|
|
return mediaStream.Index;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
self.canPlay = function (item) {
|
|
|
|
return self.canPlayMediaType(item.MediaType);
|
|
};
|
|
|
|
self.canPlayMediaType = function (mediaType) {
|
|
|
|
var media;
|
|
|
|
if (mediaType === "Video") {
|
|
media = testableVideoElement;
|
|
if (media.canPlayType) {
|
|
|
|
return media.canPlayType('video/mp4').replace(/no/, '') || media.canPlayType('video/mp2t').replace(/no/, '') || media.canPlayType('video/webm').replace(/no/, '') || media.canPlayType('application/x-mpegURL').replace(/no/, '') || media.canPlayType('video/ogv').replace(/no/, '');
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
if (mediaType === "Audio") {
|
|
media = testableAudioElement;
|
|
if (media.canPlayType) {
|
|
|
|
return media.canPlayType('audio/mpeg').replace(/no/, '') || media.canPlayType('audio/webm').replace(/no/, '') || media.canPlayType('audio/aac').replace(/no/, '');
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
self.play = function (items, startPosition) {
|
|
|
|
Dashboard.getCurrentUser().done(function (user) {
|
|
|
|
var item = items[0];
|
|
|
|
var videoType = (item.VideoType || "").toLowerCase();
|
|
|
|
if (videoType == "dvd") {
|
|
|
|
self.playWithWarning(items, startPosition, user, "dvdstreamconfirmed", "Dvd Folder Streaming");
|
|
return;
|
|
}
|
|
else if (videoType == "bluray") {
|
|
|
|
self.playWithWarning(items, startPosition, user, "bluraystreamconfirmed", "Blu-ray Folder Streaming");
|
|
return;
|
|
}
|
|
else if (videoType == "iso") {
|
|
|
|
var isoType = (item.IsoType || "").toLowerCase();
|
|
|
|
if (isoType == "dvd") {
|
|
|
|
self.playWithWarning(items, startPosition, user, "dvdisostreamconfirmed", "Dvd Iso Streaming");
|
|
return;
|
|
}
|
|
else if (isoType == "bluray") {
|
|
|
|
self.playWithWarning(items, startPosition, user, "blurayisostreamconfirmed", "Blu-ray Iso Streaming");
|
|
return;
|
|
}
|
|
}
|
|
|
|
self.playInternal(items, startPosition, user);
|
|
});
|
|
};
|
|
|
|
self.playWithWarning = function (items, startPosition, user, localStorageKeyName, header) {
|
|
|
|
if (localStorage.getItem(localStorageKeyName) == "1") {
|
|
self.playInternal(items, startPosition, user);
|
|
return;
|
|
}
|
|
|
|
Dashboard.confirm("This feature is expiremental. It may not work at all with some titles. Do you wish to continue?", header, function (result) {
|
|
|
|
if (result) {
|
|
|
|
localStorage.setItem(localStorageKeyName, "1");
|
|
self.playInternal(items, startPosition, user);
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
self.playInternal = function (items, startPosition, user) {
|
|
|
|
if (self.isPlaying()) {
|
|
self.stop();
|
|
}
|
|
|
|
var item = items[0];
|
|
|
|
var mediaElement;
|
|
|
|
if (item.MediaType === "Video") {
|
|
|
|
mediaElement = playVideo(item, startPosition, user);
|
|
} else if (item.MediaType === "Audio") {
|
|
|
|
mediaElement = playAudio(item);
|
|
}
|
|
|
|
startTimeTicksOffset = startPosition || 0;
|
|
|
|
if (!mediaElement) {
|
|
return;
|
|
}
|
|
|
|
currentMediaElement = mediaElement;
|
|
|
|
var nowPlayingBar = $('#nowPlayingBar').show();
|
|
|
|
if (items.length > 1) {
|
|
$('#previousTrackButton', nowPlayingBar)[0].disabled = false;
|
|
$('#nextTrackButton', nowPlayingBar)[0].disabled = false;
|
|
} else {
|
|
$('#previousTrackButton', nowPlayingBar)[0].disabled = true;
|
|
$('#nextTrackButton', nowPlayingBar)[0].disabled = true;
|
|
}
|
|
|
|
//display image and title
|
|
var imageTags = item.ImageTags || {};
|
|
var html = '';
|
|
|
|
var url = "";
|
|
|
|
if (imageTags.Primary) {
|
|
|
|
url = ApiClient.getImageUrl(item.Id, {
|
|
type: "Primary",
|
|
height: 80,
|
|
tag: item.ImageTags.Primary
|
|
});
|
|
}
|
|
else if (item.BackdropImageTags && item.BackdropImageTags.length) {
|
|
|
|
url = ApiClient.getImageUrl(item.Id, {
|
|
type: "Backdrop",
|
|
height: 80,
|
|
tag: item.BackdropImageTags[0]
|
|
});
|
|
} else if (imageTags.Thumb) {
|
|
|
|
url = ApiClient.getImageUrl(item.Id, {
|
|
type: "Thumb",
|
|
height: 80,
|
|
tag: item.ImageTags.Thumb
|
|
});
|
|
|
|
} else {
|
|
url = "css/images/items/detail/video.png";
|
|
}
|
|
|
|
var name = item.Name;
|
|
var seriesName = '';
|
|
|
|
if (item.IndexNumber != null) {
|
|
name = item.IndexNumber + " - " + name;
|
|
}
|
|
if (item.ParentIndexNumber != null) {
|
|
name = item.ParentIndexNumber + "." + name;
|
|
}
|
|
if (item.SeriesName || item.Album || item.ProductionYear) {
|
|
seriesName = item.SeriesName || item.Album || item.ProductionYear;
|
|
}
|
|
|
|
html += "<div><a href='itemdetails.html?id=" + item.Id + "'><img class='nowPlayingBarImage ' alt='' title='' src='" + url + "' style='height:36px;display:inline-block;' /></a></div>";
|
|
|
|
if (item.SeriesName || item.Album) {
|
|
html += '<div class="nowPlayingText">' + seriesName + '<br/>' + name + '</div>';
|
|
} else {
|
|
html += '<div class="nowPlayingText">' + name + '<br/>' + seriesName + '</div>';
|
|
}
|
|
|
|
$('.nowPlayingMediaInfo', nowPlayingBar).html(html);
|
|
};
|
|
|
|
self.playById = function (id, startPositionTicks) {
|
|
|
|
ApiClient.getItem(Dashboard.getCurrentUserId(), id).done(function (item) {
|
|
|
|
self.play([item], startPositionTicks);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
self.nowPlaying = function (item) {
|
|
self.playing = item;
|
|
};
|
|
|
|
self.toggleFullscreen = function () {
|
|
|
|
if (isFullScreen()) {
|
|
if (document.cancelFullScreen) { document.cancelFullScreen(); }
|
|
else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); }
|
|
else if (document.webkitCancelFullScreen) { document.webkitCancelFullScreen(); }
|
|
} else {
|
|
requestFullScreen(document.body);
|
|
}
|
|
};
|
|
|
|
self.canQueue = function (mediaType) {
|
|
return mediaType == "Audio";
|
|
};
|
|
|
|
self.queueAdd = function (item) {
|
|
self.queue.push(item);
|
|
};
|
|
|
|
self.queueRemove = function (elem) {
|
|
var index = $(elem).attr("data-queue-index");
|
|
|
|
self.queue.splice(index, 1);
|
|
|
|
$(elem).parent().parent().remove();
|
|
return false;
|
|
};
|
|
|
|
self.queuePlay = function (elem) {
|
|
var index = $(elem).attr("data-queue-index");
|
|
|
|
MediaPlayer.play(new Array(self.queue[index]));
|
|
self.queue.splice(index, 1);
|
|
};
|
|
|
|
self.queuePlayNext = function (item) {
|
|
if (typeof self.queue[0] != "undefined") {
|
|
MediaPlayer.play(new Array(self.queue[0]));
|
|
self.queue.shift();
|
|
}
|
|
};
|
|
|
|
self.queueAddNext = function (item) {
|
|
if (typeof self.queue[0] != "undefined") {
|
|
self.queue.unshift(item);
|
|
} else {
|
|
self.queueAdd(item);
|
|
}
|
|
};
|
|
|
|
self.inQueue = function (item) {
|
|
$.each(MediaPlayer.queue, function (i, queueItem) {
|
|
if (item.Id == queueItem.Id) {
|
|
return true;
|
|
}
|
|
});
|
|
return false;
|
|
};
|
|
|
|
self.playLast = function (itemId) {
|
|
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).done(function (item) {
|
|
self.queueAdd(item);
|
|
});
|
|
};
|
|
|
|
self.playNext = function (itemId) {
|
|
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).done(function (item) {
|
|
self.queueAddNext(item);
|
|
});
|
|
};
|
|
|
|
self.pause = function () {
|
|
currentMediaElement.pause();
|
|
};
|
|
|
|
self.unpause = function () {
|
|
currentMediaElement.play();
|
|
};
|
|
|
|
self.mute = function () {
|
|
currentMediaElement.volume = 0;
|
|
};
|
|
|
|
self.unmute = function () {
|
|
currentMediaElement.volume = volumeSlider.val();
|
|
};
|
|
|
|
self.stop = function () {
|
|
|
|
var elem = currentMediaElement;
|
|
|
|
elem.pause();
|
|
|
|
var jelem = $(elem).trigger('ended');
|
|
elem.src = "";
|
|
|
|
jelem.remove();
|
|
|
|
$('#nowPlayingBar').hide();
|
|
|
|
currentMediaElement = null;
|
|
};
|
|
|
|
self.isPlaying = function () {
|
|
return currentMediaElement;
|
|
};
|
|
|
|
function hideFlyout(flyout) {
|
|
flyout.hide().empty();
|
|
$(document.body).off("mousedown.hidesearchhints");
|
|
}
|
|
|
|
function showFlyout(flyout, button) {
|
|
|
|
$(document.body).off("mousedown.mediaflyout").on("mousedown.mediaflyout", function (e) {
|
|
|
|
var elem = $(e.target);
|
|
|
|
var flyoutId = flyout[0].id;
|
|
var safeItems = button + ',#' + flyoutId;
|
|
|
|
if (!elem.is(safeItems) && !elem.parents(safeItems).length) {
|
|
hideFlyout(flyout);
|
|
}
|
|
|
|
});
|
|
|
|
flyout.show();
|
|
}
|
|
|
|
function getChaptersFlyoutHtml(item) {
|
|
|
|
var html = '';
|
|
|
|
var currentTicks = getCurrentTicks();
|
|
|
|
for (var i = 0, length = item.Chapters.length; i < length; i++) {
|
|
|
|
var chapter = item.Chapters[i];
|
|
|
|
var isSelected = false;
|
|
|
|
if (currentTicks >= chapter.StartPositionTicks) {
|
|
|
|
var nextChapter = item.Chapters[i + 1];
|
|
|
|
isSelected = !nextChapter || currentTicks < nextChapter.StartPositionTicks;
|
|
}
|
|
|
|
if (isSelected) {
|
|
html += '<div data-positionticks="' + chapter.StartPositionTicks + '" class="mediaFlyoutOption selectedMediaFlyoutOption">';
|
|
} else {
|
|
html += '<div data-positionticks="' + chapter.StartPositionTicks + '" class="mediaFlyoutOption">';
|
|
}
|
|
|
|
var imgUrl;
|
|
|
|
if (chapter.ImageTag) {
|
|
|
|
imgUrl = ApiClient.getImageUrl(item.Id, {
|
|
maxwidth: 200,
|
|
tag: chapter.ImageTag,
|
|
type: "Chapter",
|
|
index: i
|
|
});
|
|
|
|
} else {
|
|
imgUrl = "css/images/media/chapterflyout.png";
|
|
}
|
|
|
|
html += '<img class="mediaFlyoutOptionImage" src="' + imgUrl + '" />';
|
|
|
|
html += '<div class="mediaFlyoutOptionContent">';
|
|
|
|
var name = chapter.Name || "Chapter " + (i + 1);
|
|
|
|
html += '<div class="mediaFlyoutOptionName">' + name + '</div>';
|
|
html += '<div class="mediaFlyoutOptionSecondaryText">' + DashboardPage.getDisplayText(chapter.StartPositionTicks) + '</div>';
|
|
|
|
html += '</div>';
|
|
|
|
html += "</div>";
|
|
}
|
|
|
|
return html;
|
|
}
|
|
|
|
function getAudioTracksHtml(item, cultures) {
|
|
|
|
var streams = item.MediaStreams.filter(function (i) {
|
|
return i.Type == "Audio";
|
|
});
|
|
|
|
var currentIndex = getParameterByName('AudioStreamIndex', currentMediaElement.currentSrc);
|
|
|
|
var html = '';
|
|
|
|
for (var i = 0, length = streams.length; i < length; i++) {
|
|
|
|
var stream = streams[i];
|
|
|
|
if (stream.Index == currentIndex) {
|
|
html += '<div data-index="' + stream.Index + '" class="mediaFlyoutOption selectedMediaFlyoutOption">';
|
|
} else {
|
|
html += '<div data-index="' + stream.Index + '" class="mediaFlyoutOption">';
|
|
}
|
|
|
|
html += '<img class="mediaFlyoutOptionImage" src="css/images/media/audioflyout.png" />';
|
|
|
|
html += '<div class="mediaFlyoutOptionContent">';
|
|
|
|
var language = null;
|
|
|
|
if (stream.Language && stream.Language != "und") {
|
|
|
|
var culture = cultures.filter(function (current) {
|
|
return current.ThreeLetterISOLanguageName.toLowerCase() == stream.Language.toLowerCase();
|
|
});
|
|
|
|
if (culture.length) {
|
|
language = culture[0].DisplayName;
|
|
}
|
|
}
|
|
|
|
html += '<div class="mediaFlyoutOptionName">' + (language || 'Unknown language') + '</div>';
|
|
|
|
var options = [];
|
|
|
|
if (stream.Codec) {
|
|
options.push(stream.Codec);
|
|
}
|
|
if (stream.Profile) {
|
|
options.push(stream.Profile);
|
|
}
|
|
|
|
if (stream.BitRate) {
|
|
options.push((parseInt(stream.BitRate / 1000)) + ' kbps');
|
|
}
|
|
|
|
if (stream.Channels) {
|
|
options.push(stream.Channels + ' ch');
|
|
}
|
|
|
|
if (options.length) {
|
|
html += '<div class="mediaFlyoutOptionSecondaryText">' + options.join(' • ') + '</div>';
|
|
}
|
|
|
|
options = [];
|
|
|
|
if (stream.IsDefault) {
|
|
options.push('Default');
|
|
}
|
|
if (stream.IsForced) {
|
|
options.push('Forced');
|
|
}
|
|
|
|
if (options.length) {
|
|
html += '<div class="mediaFlyoutOptionSecondaryText">' + options.join(' • ') + '</div>';
|
|
}
|
|
|
|
html += "</div>";
|
|
|
|
html += "</div>";
|
|
}
|
|
|
|
return html;
|
|
}
|
|
|
|
function getSubtitleTracksHtml(item, cultures) {
|
|
|
|
var streams = item.MediaStreams.filter(function (i) {
|
|
return i.Type == "Subtitle";
|
|
});
|
|
|
|
var currentIndex = getParameterByName('SubtitleStreamIndex', currentMediaElement.currentSrc);
|
|
|
|
var html = '';
|
|
|
|
for (var i = 0, length = streams.length; i < length; i++) {
|
|
|
|
var stream = streams[i];
|
|
|
|
if (stream.Index == currentIndex) {
|
|
html += '<div data-index="' + stream.Index + '" class="mediaFlyoutOption selectedMediaFlyoutOption">';
|
|
} else {
|
|
html += '<div data-index="' + stream.Index + '" class="mediaFlyoutOption">';
|
|
}
|
|
|
|
html += '<img class="mediaFlyoutOptionImage" src="css/images/media/subtitleflyout.png" />';
|
|
|
|
html += '<div class="mediaFlyoutOptionContent">';
|
|
|
|
var language = null;
|
|
|
|
if (stream.Language && stream.Language != "und") {
|
|
|
|
var culture = cultures.filter(function (current) {
|
|
return current.ThreeLetterISOLanguageName.toLowerCase() == stream.Language.toLowerCase();
|
|
});
|
|
|
|
if (culture.length) {
|
|
language = culture[0].DisplayName;
|
|
}
|
|
}
|
|
|
|
html += '<div class="mediaFlyoutOptionName">' + (language || 'Unknown language') + '</div>';
|
|
|
|
var options = [];
|
|
|
|
if (stream.Codec) {
|
|
options.push(stream.Codec);
|
|
}
|
|
|
|
if (options.length) {
|
|
html += '<div class="mediaFlyoutOptionSecondaryText">' + options.join(' • ') + '</div>';
|
|
}
|
|
|
|
options = [];
|
|
|
|
if (stream.IsDefault) {
|
|
options.push('Default');
|
|
}
|
|
if (stream.IsForced) {
|
|
options.push('Forced');
|
|
}
|
|
if (stream.IsExternal) {
|
|
options.push('External');
|
|
}
|
|
|
|
if (options.length) {
|
|
html += '<div class="mediaFlyoutOptionSecondaryText">' + options.join(' • ') + '</div>';
|
|
}
|
|
|
|
html += "</div>";
|
|
|
|
html += "</div>";
|
|
}
|
|
|
|
return html;
|
|
}
|
|
|
|
function getQualityFlyoutHtml(item) {
|
|
|
|
var html = '';
|
|
|
|
var videoStream = item.MediaStreams.filter(function (i) {
|
|
return i.Type == "Video";
|
|
})[0];
|
|
|
|
var currentVideoBitrate = getParameterByName('videoBitrate', currentMediaElement.currentSrc) || getParameterByName('VideoBitrate', currentMediaElement.currentSrc);
|
|
|
|
var maxAllowedWidth = Math.max(screen.height, screen.width);
|
|
|
|
var options = [];
|
|
|
|
// We have media info
|
|
if (videoStream && videoStream.Width) {
|
|
|
|
maxAllowedWidth = Math.min(videoStream.Width, maxAllowedWidth);
|
|
}
|
|
|
|
// Some 1080- videos are reported as 1912?
|
|
if (maxAllowedWidth >= 1910) {
|
|
options.push({ name: '1080p+', maxWidth: 1920, videoBitrate: 4000000 });
|
|
options.push({ name: '1080p', maxWidth: 1920, videoBitrate: 2500000 });
|
|
}
|
|
|
|
if (maxAllowedWidth >= 1280) {
|
|
options.push({ name: '720p+', maxWidth: 1280, videoBitrate: 2000000 });
|
|
options.push({ name: '720p', maxWidth: 1280, videoBitrate: 1000000 });
|
|
}
|
|
|
|
if (maxAllowedWidth >= 480) {
|
|
options.push({ name: '480p', maxWidth: 720, videoBitrate: 400000 });
|
|
}
|
|
|
|
for (var i = 0, length = options.length; i < length; i++) {
|
|
|
|
var option = options[i];
|
|
|
|
var cssClass = "mediaFlyoutOption";
|
|
|
|
if (option.videoBitrate == currentVideoBitrate) {
|
|
cssClass += " selectedMediaFlyoutOption";
|
|
}
|
|
|
|
html += '<div data-maxwidth="' + option.maxWidth + '" data-videobitrate="' + option.videoBitrate + '" class="' + cssClass + '">';
|
|
|
|
html += '<div class="mediaFlyoutOptionContent">';
|
|
|
|
html += '<div class="mediaFlyoutOptionName" style="padding:.5em;">' + option.name + '</div>';
|
|
|
|
html += "</div>";
|
|
|
|
html += "</div>";
|
|
}
|
|
|
|
return html;
|
|
}
|
|
|
|
self.showAudioTracksFlyout = function () {
|
|
|
|
var flyout = $('#audioTracksFlyout');
|
|
|
|
if (!flyout.is(':visible')) {
|
|
|
|
culturesPromise = culturesPromise || ApiClient.getCultures();
|
|
|
|
culturesPromise.done(function (cultures) {
|
|
|
|
showFlyout(flyout, '#audioTracksButton');
|
|
|
|
flyout.html(getAudioTracksHtml(currentItem, cultures)).scrollTop(0);
|
|
});
|
|
}
|
|
};
|
|
|
|
self.showChaptersFlyout = function () {
|
|
|
|
var flyout = $('#chaptersFlyout');
|
|
|
|
if (!flyout.is(':visible')) {
|
|
|
|
showFlyout(flyout, '#chaptersButton');
|
|
|
|
flyout.html(getChaptersFlyoutHtml(currentItem)).scrollTop(0);
|
|
}
|
|
};
|
|
|
|
self.showQualityFlyout = function () {
|
|
|
|
var flyout = $('#qualityFlyout');
|
|
|
|
if (!flyout.is(':visible')) {
|
|
|
|
showFlyout(flyout, '#qualityButton');
|
|
|
|
flyout.html(getQualityFlyoutHtml(currentItem)).scrollTop(0);
|
|
}
|
|
};
|
|
|
|
self.showSubtitleMenu = function () {
|
|
|
|
var flyout = $('#subtitleFlyout');
|
|
|
|
if (!flyout.is(':visible')) {
|
|
|
|
culturesPromise = culturesPromise || ApiClient.getCultures();
|
|
|
|
culturesPromise.done(function (cultures) {
|
|
|
|
showFlyout(flyout, '#subtitleButton');
|
|
|
|
flyout.html(getSubtitleTracksHtml(currentItem, cultures)).scrollTop(0);
|
|
});
|
|
|
|
}
|
|
};
|
|
}
|
|
|
|
window.MediaPlayer = new mediaPlayer();
|
|
|
|
})(document, setTimeout, clearTimeout, screen, localStorage, $, setInterval, window); |