jellyfin-web/dashboard-ui/scripts/mediaplayer.js

804 lines
26 KiB
JavaScript
Raw Normal View History

(function (document, setTimeout, clearTimeout, screen, localStorage, $, setInterval, window) {
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
function mediaPlayer() {
2013-05-23 18:47:07 -07:00
2013-04-10 14:03:28 -07:00
var self = this;
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var testableAudioElement = document.createElement('audio');
var testableVideoElement = document.createElement('video');
var currentMediaElement;
var currentProgressInterval;
2013-05-23 13:09:01 -07:00
var positionSlider;
var isPositionSliderActive;
var currentTimeElement;
var currentItem;
var volumeSlider;
var muteButton;
var unmuteButton;
var startTimeTicksOffset;
var curentDurationTicks;
var isStaticStream;
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}");
}
}
}
2013-05-24 07:35:15 -07:00
function isFullScreen() {
return document.fullscreenEnabled || document.mozFullscreenEnabled || document.webkitIsFullScreen ? true : false;
}
2013-05-24 07:35:15 -07:00
$(document).on('webkitfullscreenchange mozfullscreenchange fullscreenchange', function () {
if (isFullScreen()) {
$('.itemVideo').addClass('fullscreenVideo');
} else {
$('.itemVideo').removeClass('fullscreenVideo');
}
});
2013-05-23 13:09:01 -07:00
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 onPlaybackStopped() {
currentTimeElement.hide();
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 () {
2013-05-23 15:39:51 -07:00
if (currentMediaElement) {
sendProgressUpdate(itemId);
}
2013-04-08 21:26:02 -07:00
2013-05-23 13:09:01 -07:00
}, intervalTime);
}
2013-05-23 15:39:51 -07:00
function sendProgressUpdate(itemId) {
var position = Math.floor(10000000 * currentMediaElement.currentTime) + startTimeTicksOffset;
ApiClient.reportPlaybackProgress(Dashboard.getCurrentUserId(), itemId, position);
}
2013-05-23 13:09:01 -07:00
function clearProgressInterval() {
if (currentProgressInterval) {
clearTimeout(currentProgressInterval);
currentProgressInterval = null;
}
}
2013-05-23 18:47:07 -07:00
function onPositionSliderChange() {
isPositionSliderActive = false;
var element = currentMediaElement;
var newPercent = parseInt(this.value);
var newPositionTicks = (newPercent / 100) * currentItem.RunTimeTicks;
if (isStaticStream) {
element.currentTime = newPositionTicks / (1000 * 10000);
} else {
var currentSrc = element.currentSrc;
if (currentSrc.toLowerCase().indexOf('starttimeticks') == -1) {
currentSrc += "&starttimeticks=" + newPositionTicks;
} else {
currentSrc = replaceQueryString(currentSrc, 'starttimeticks', newPositionTicks);
}
clearProgressInterval();
$(element).off('ended.playbackstopped').on("play.onceafterseek", function () {
$(this).off('play.onceafterseek').on('ended.playbackstopped', onPlaybackStopped);
startProgressInterval(currentItem.Id);
sendProgressUpdate(currentItem.Id);
});
startTimeTicksOffset = newPositionTicks;
element.src = currentSrc;
}
}
2013-05-23 13:09:01 -07:00
$(function () {
muteButton = $('#muteButton');
unmuteButton = $('#unmuteButton');
currentTimeElement = $('.currentTime');
volumeSlider = $('.volumeSlider').on('change', function () {
var vol = this.value;
updateVolumeButtons(vol);
currentMediaElement.volume = vol;
});
2013-05-23 15:39:51 -07:00
$(".jqueryuislider").slider({ orientation: "horizontal" });
2013-05-23 18:47:07 -07:00
positionSlider = $(".positionSlider").on('mousedown', function () {
2013-05-23 13:09:01 -07:00
isPositionSliderActive = true;
2013-05-23 18:47:07 -07:00
});
2013-05-23 13:09:01 -07:00
2013-05-23 18:47:07 -07:00
if ($.browser.mozilla) {
2013-05-23 13:09:01 -07:00
2013-05-23 18:47:07 -07:00
positionSlider.on('change', onPositionSliderChange);
2013-05-23 13:09:01 -07:00
2013-05-23 18:47:07 -07:00
} else {
2013-05-23 13:09:01 -07:00
2013-05-23 18:47:07 -07:00
positionSlider.on('change', function () {
2013-05-23 13:09:01 -07:00
2013-05-23 18:47:07 -07:00
isPositionSliderActive = true;
2013-05-23 13:09:01 -07:00
2013-05-23 18:47:07 -07:00
setCurrentTimePercent(parseInt(this.value), currentItem);
2013-05-23 13:09:01 -07:00
2013-05-23 18:47:07 -07:00
}).on('changed', onPositionSliderChange);
}
2013-05-23 13:09:01 -07:00
(function (el, timeout) {
var timer, trig = function () { el.trigger("changed"); };
el.bind("change", function () {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(trig, timeout);
});
})(positionSlider, 500);
});
2013-04-30 12:13:06 -07:00
function endsWith(text, pattern) {
text = text.toLowerCase();
pattern = pattern.toLowerCase();
var d = text.length - pattern.length;
return d >= 0 && text.lastIndexOf(pattern) === d;
}
2013-05-23 13:09:01 -07:00
function setCurrentTimePercent(percent, item) {
2013-04-08 21:26:02 -07:00
2013-05-23 13:09:01 -07:00
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);
}
}
currentTimeElement.html(timeText);
}
function playAudio(item, params) {
2013-04-10 14:03:28 -07:00
var baseParams = {
audioChannels: 2,
audioBitrate: 128000
};
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
$.extend(baseParams, params);
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var mp3Url = ApiClient.getUrl('Audio/' + item.Id + '/stream.mp3', $.extend({}, baseParams, {
audioCodec: 'mp3'
}));
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var aacUrl = ApiClient.getUrl('Audio/' + item.Id + '/stream.aac', $.extend({}, baseParams, {
audioCodec: 'aac'
}));
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var webmUrl = ApiClient.getUrl('Audio/' + item.Id + '/stream.webm', $.extend({}, baseParams, {
audioCodec: 'Vorbis'
}));
2013-04-08 21:26:02 -07:00
2013-04-30 12:13:06 -07:00
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;
}
}
2013-04-10 14:03:28 -07:00
var html = '';
2013-05-23 13:09:01 -07:00
2013-05-24 13:31:01 -07:00
var requiresControls = $.browser.android || $.browser.iphone || $.browser.ipad;
2013-05-24 13:22:19 -07:00
2013-05-24 07:35:15 -07:00
// Can't autoplay in these browsers so we need to use the full controls
2013-05-24 13:22:19 -07:00
if (requiresControls) {
2013-05-24 07:35:15 -07:00
html += '<audio preload="auto" autoplay controls>';
} else {
html += '<audio preload="auto" style="display:none;" autoplay>';
}
2013-04-10 14:03:28 -07:00
html += '<source type="audio/mpeg" src="' + mp3Url + '" />';
html += '<source type="audio/aac" src="' + aacUrl + '" />';
html += '<source type="audio/webm" src="' + webmUrl + '" />';
html += '</audio';
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var nowPlayingBar = $('#nowPlayingBar').show();
//show stop button
$('#stopButton', nowPlayingBar).show();
2013-05-23 13:09:01 -07:00
$('#playButton', nowPlayingBar).hide();
$('#pauseButton', nowPlayingBar).show();
$('#playlistButton', nowPlayingBar).show();
$('#previousTrackButton', nowPlayingBar).show();
$('#nextTrackButton', nowPlayingBar).show();
2013-04-10 14:03:28 -07:00
$('#mediaElement', nowPlayingBar).html(html);
$('#fullscreenButton', nowPlayingBar).hide();
2013-04-08 21:26:02 -07:00
2013-05-23 13:09:01 -07:00
var audioElement = $("audio", nowPlayingBar);
2013-05-23 15:39:51 -07:00
var initialVolume = localStorage.getItem("volume") || 0.5;
2013-05-23 13:09:01 -07:00
audioElement.each(function () {
2013-05-23 15:39:51 -07:00
this.volume = initialVolume;
});
2013-05-23 15:39:51 -07:00
volumeSlider.val(initialVolume);
updateVolumeButtons(initialVolume);
2013-05-23 13:09:01 -07:00
audioElement.on("volumechange", function () {
2013-05-23 13:09:01 -07:00
var vol = this.volume;
2013-05-23 13:09:01 -07:00
localStorage.setItem("volume", vol);
2013-05-23 13:09:01 -07:00
updateVolumeButtons(vol);
2013-05-23 13:09:01 -07:00
}).on("play.once", function () {
2013-05-24 13:22:19 -07:00
if (!requiresControls) {
audioElement.hide();
}
2013-05-23 13:09:01 -07:00
var duration = this.duration;
isStaticStream = duration && !isNaN(duration) && duration != Number.POSITIVE_INFINITY && duration != Number.NEGATIVE_INFINITY;
2013-04-08 21:26:02 -07:00
2013-05-23 13:09:01 -07:00
currentTimeElement.show();
2013-05-24 07:35:15 -07:00
audioElement.off("play.once");
2013-05-23 13:09:01 -07:00
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) {
var ticks = startTimeTicksOffset + this.currentTime * 1000 * 10000;
2013-05-23 15:39:51 -07:00
2013-05-23 13:09:01 -07:00
setCurrentTime(ticks, item, true);
}
}).on("ended.playbackstopped", onPlaybackStopped);
MediaPlayer.nowPlaying(item);
currentItem = item;
curentDurationTicks = item.RunTimeTicks;
return audioElement[0];
2013-04-30 10:21:21 -07:00
}
function playVideo(item, startPosition) {
2013-04-10 14:03:28 -07:00
//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 screenHeight = Math.min(screen.height, screen.width);
var user = Dashboard.getCurrentUser();
var defaults = { languageIndex: null, subtitleIndex: null };
var userConfig = user.Configuration || {};
if (item.MediaStreams && item.MediaStreams.length) {
$.each(item.MediaStreams, function (i, stream) {
//get default subtitle stream
if (stream.Type == "Subtitle") {
if (userConfig.UseForcedSubtitlesOnly == true && userConfig.SubtitleLanguagePreference && !defaults.subtitleIndex) {
if (stream.Language == userConfig.SubtitleLanguagePreference && stream.IsForced == true) {
defaults.subtitleIndex = i;
}
} else if (userConfig.SubtitleLanguagePreference && !defaults.subtitleIndex) {
if (stream.Language == userConfig.SubtitleLanguagePreference) {
defaults.subtitleIndex = i;
}
} else if (userConfig.UseForcedSubtitlesOnly == true && !defaults.subtitleIndex) {
if (stream.IsForced == true) {
defaults.subtitleIndex = i;
}
2013-04-08 21:26:02 -07:00
}
2013-04-10 14:03:28 -07:00
} else if (stream.Type == "Audio") {
//get default language stream
if (userConfig.AudioLanguagePreference && !defaults.languageIndex) {
if (stream.Language == userConfig.AudioLanguagePreference) {
defaults.languageIndex = i;
}
2013-04-08 21:26:02 -07:00
}
}
2013-04-10 14:03:28 -07:00
});
}
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
var baseParams = {
audioChannels: 2,
audioBitrate: 128000,
videoBitrate: 1500000,
maxWidth: screenWidth,
maxHeight: screenHeight,
2013-04-16 21:38:42 -07:00
StartTimeTicks: 0,
SubtitleStreamIndex: null,
AudioStreamIndex: null
2013-04-10 14:03:28 -07:00
};
if (startPosition) {
baseParams.StartTimeTicks = startPosition;
2013-04-10 14:03:28 -07:00
}
2013-04-08 21:26:02 -07:00
var mp4VideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.mp4', $.extend({}, baseParams, {
videoCodec: 'h264',
audioCodec: 'aac',
profile: 'high',
videoBitrate: 2500000
}));
2013-02-27 10:53:42 -07:00
var tsVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.ts', $.extend({}, baseParams, {
videoCodec: 'h264',
audioCodec: 'aac',
profile: 'high',
videoBitrate: 2500000
}));
2013-02-20 18:33:05 -07:00
var webmVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.webm', $.extend({}, baseParams, {
videoCodec: 'vpx',
audioCodec: 'Vorbis',
videoBitrate: 2500000
}));
var hlsVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.m3u8', $.extend({}, baseParams, {
videoCodec: 'h264',
audioCodec: 'aac'
}));
2013-02-20 18:33:05 -07:00
var ogvVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.ogv', $.extend({}, baseParams, {
videoCodec: 'theora',
audioCodec: 'Vorbis'
}));
2013-04-10 14:03:28 -07:00
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
2013-04-17 18:32:38 -07:00
2013-05-24 07:35:15 -07:00
// 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" preload="auto" autoplay controls>';
} else {
html += '<video class="itemVideo" preload="auto" autoplay>';
}
html += '<source type="application/x-mpegURL" src="' + hlsVideoUrl + '" />';
html += '<source type="video/webm" src="' + webmVideoUrl + '" />';
html += '<source type="video/mp4" src="' + mp4VideoUrl + '" />';
html += '<source type="video/mp2t" src="' + tsVideoUrl + '" />';
html += '<source type="video/ogg" src="' + ogvVideoUrl + '" />';
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);
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;
2013-04-08 21:26:02 -07:00
});
2013-02-20 18:33:05 -07:00
volumeSlider.val(initialVolume);
updateVolumeButtons(initialVolume);
2013-02-27 10:53:42 -07:00
videoElement.on("volumechange", function () {
var vol = this.volume;
localStorage.setItem("volume", vol);
2013-05-10 05:18:07 -07:00
updateVolumeButtons(vol);
}).on("play.once", function () {
2013-02-20 18:33:05 -07:00
var duration = this.duration;
isStaticStream = duration && !isNaN(duration) && duration != Number.POSITIVE_INFINITY && duration != Number.NEGATIVE_INFINITY;
2013-02-20 18:33:05 -07:00
currentTimeElement.show();
2013-02-20 18:33:05 -07:00
videoElement.off("play.once");
ApiClient.reportPlaybackStart(Dashboard.getCurrentUserId(), item.Id);
2013-05-23 13:09:01 -07:00
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) {
var ticks = startTimeTicksOffset + this.currentTime * 1000 * 10000;
setCurrentTime(ticks, item, true);
}
}).on("ended.playbackstopped", onPlaybackStopped);
MediaPlayer.nowPlaying(item);
currentItem = item;
curentDurationTicks = item.RunTimeTicks;
return videoElement[0];
2013-04-10 14:03:28 -07:00
}
2013-04-10 14:03:28 -07:00
self.canPlay = function (item) {
2013-05-23 18:47:07 -07:00
return self.canPlayMediaType(item.MediaType);
};
self.canPlayMediaType = function (mediaType) {
2013-04-10 14:03:28 -07:00
var media;
2013-03-31 18:52:07 -07:00
2013-05-23 18:47:07 -07:00
if (mediaType === "Video") {
2013-04-10 14:03:28 -07:00
media = testableVideoElement;
if (media.canPlayType) {
2013-04-10 14:03:28 -07:00
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/, '');
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
return false;
2013-04-08 21:26:02 -07:00
}
2013-04-08 21:16:32 -07:00
2013-05-23 18:47:07 -07:00
if (mediaType === "Audio") {
2013-04-10 14:03:28 -07:00
media = testableAudioElement;
if (media.canPlayType) {
2013-04-08 21:16:32 -07:00
2013-05-23 18:47:07 -07:00
return media.canPlayType('audio/mpeg').replace(/no/, '') || media.canPlayType('audio/webm').replace(/no/, '') || media.canPlayType('audio/aac').replace(/no/, '');
2013-04-10 14:03:28 -07:00
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
return false;
2013-04-08 21:16:32 -07:00
}
2013-04-08 21:26:02 -07:00
return false;
2013-04-10 14:03:28 -07:00
};
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
self.play = function (items, startPosition) {
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
if (self.isPlaying()) {
self.stop();
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
var item = items[0];
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
var mediaElement;
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
if (item.MediaType === "Video") {
2013-04-08 21:16:32 -07:00
2013-04-30 10:21:21 -07:00
mediaElement = playVideo(item, startPosition);
2013-04-10 14:03:28 -07:00
} else if (item.MediaType === "Audio") {
2013-04-08 21:16:32 -07:00
2013-04-30 10:21:21 -07:00
mediaElement = playAudio(item);
2013-04-10 14:03:28 -07:00
}
2013-04-08 21:16:32 -07:00
2013-05-23 13:09:01 -07:00
startTimeTicksOffset = startPosition || 0;
2013-04-10 14:03:28 -07:00
if (!mediaElement) {
return;
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
currentMediaElement = mediaElement;
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
var nowPlayingBar = $('#nowPlayingBar').show();
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
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;
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
//display image and title
var imageTags = item.ImageTags || {};
var html = '';
var url = "";
if (item.BackdropImageTags && item.BackdropImageTags.length) {
url = ApiClient.getImageUrl(item.Id, {
type: "Backdrop",
height: 36,
tag: item.BackdropImageTags[0]
});
} else if (imageTags.Thumb) {
url = ApiClient.getImageUrl(item.Id, {
type: "Thumb",
height: 36,
tag: item.ImageTags.Thumb
});
} else if (imageTags.Primary) {
url = ApiClient.getImageUrl(item.Id, {
type: "Primary",
height: 36,
tag: item.ImageTags.Primary
});
} else {
url = "css/images/items/detail/video.png";
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
var name = item.Name;
var seriesName = '';
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
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;
}
2013-04-08 21:16:32 -07:00
html += "<div><a href='itemdetails.html?id=" + item.Id + "'><img class='nowPlayingBarImage ' alt='' title='' src='" + url + "' style='height:36px;display:inline-block;' /></a></div>";
2013-04-10 14:03:28 -07:00
if (item.Type == "Movie")
html += '<div>' + name + '<br/>' + seriesName + '</div>';
else
html += '<div>' + seriesName + '<br/>' + name + '</div>';
2013-05-23 13:09:01 -07:00
$('.nowPlayingMediaInfo', nowPlayingBar).html(html);
2013-04-10 14:03:28 -07:00
};
2013-04-30 10:21:21 -07:00
self.playById = function (id, startPositionTicks) {
ApiClient.getItem(Dashboard.getCurrentUserId(), id).done(function (item) {
self.play([item], startPositionTicks);
});
};
2013-05-23 13:09:01 -07:00
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);
}
};
2013-05-23 13:09:01 -07:00
self.canQueue = function (mediaType) {
return mediaType == "Audio";
};
2013-05-23 13:09:01 -07:00
self.queueAdd = function (item) {
self.queue.push(item);
};
2013-05-23 13:09:01 -07:00
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);
2013-04-30 10:21:21 -07:00
};
2013-05-23 13:09:01 -07:00
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;
};
2013-04-30 12:13:06 -07:00
self.playLast = function (itemId) {
2013-05-23 13:09:01 -07:00
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).done(function (item) {
self.queueAdd(item);
});
2013-04-30 10:21:21 -07:00
};
self.playNext = function (itemId) {
2013-05-23 13:09:01 -07:00
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();
2013-04-30 10:21:21 -07:00
};
2013-04-10 14:03:28 -07:00
self.stop = function () {
2013-03-31 18:52:07 -07:00
2013-04-10 14:03:28 -07:00
var elem = currentMediaElement;
2013-03-31 18:52:07 -07:00
elem.pause();
2013-02-20 18:33:05 -07:00
var jelem = $(elem).trigger('ended');
elem.src = "";
2013-03-26 14:32:01 -07:00
jelem.remove();
2013-02-27 10:53:42 -07:00
2013-04-10 14:03:28 -07:00
$('#nowPlayingBar').hide();
2013-03-26 14:32:01 -07:00
2013-04-10 14:03:28 -07:00
currentMediaElement = null;
};
2013-03-26 14:32:01 -07:00
2013-04-10 14:03:28 -07:00
self.isPlaying = function () {
return currentMediaElement;
};
}
2013-03-26 14:32:01 -07:00
2013-04-10 14:03:28 -07:00
window.MediaPlayer = new mediaPlayer();
2013-04-08 21:26:02 -07:00
})(document, setTimeout, clearTimeout, screen, localStorage, $, setInterval, window);