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

721 lines
22 KiB
JavaScript
Raw Normal View History

2016-08-01 13:06:30 -07:00
define(['datetime', 'userdataButtons', 'itemHelper', 'events', 'browser', 'paper-icon-button-light'], function (datetime, userdataButtons, itemHelper, events, browser) {
var currentPlayer;
var currentTimeElement;
var nowPlayingImageElement;
var nowPlayingTextElement;
var nowPlayingUserData;
var unmuteButton;
var muteButton;
var volumeSlider;
2016-06-24 10:30:29 -07:00
var volumeSliderContainer;
2016-06-01 23:08:47 -07:00
var unpauseButtons;
var pauseButtons;
var positionSlider;
2015-07-26 14:02:23 -07:00
var toggleRepeatButton;
2016-05-07 10:47:41 -07:00
var toggleRepeatButtonIcon;
var lastPlayerState;
function getNowPlayingBarHtml() {
var html = '';
2015-12-14 08:43:03 -07:00
html += '<div class="nowPlayingBar hide">';
2014-06-21 22:52:31 -07:00
2016-08-01 13:06:30 -07:00
html += '<div class="nowPlayingBarTop">';
2016-06-13 12:02:48 -07:00
html += '<div class="nowPlayingBarPositionContainer sliderContainer">';
html += '<input type="range" is="emby-slider" pin step=".1" min="0" max="100" value="0" class="nowPlayingBarPositionSlider"/>';
2015-06-27 12:53:36 -07:00
html += '</div>';
html += '<div class="nowPlayingBarInfoContainer">';
2015-05-01 11:37:01 -07:00
html += '<div class="nowPlayingImage"></div>';
2015-06-27 12:53:36 -07:00
html += '<div class="nowPlayingBarText"></div>';
html += '</div>';
2015-05-01 11:37:01 -07:00
2015-06-17 18:41:22 -07:00
// The onclicks are needed due to the return false above
2015-06-27 12:53:36 -07:00
html += '<div class="nowPlayingBarCenter">';
2015-05-01 11:37:01 -07:00
2016-06-14 20:12:32 -07:00
html += '<button is="paper-icon-button-light" class="previousTrackButton mediaButton autoSize"><i class="md-icon">skip_previous</i></button>';
2014-06-21 22:52:31 -07:00
2016-06-14 20:12:32 -07:00
html += '<button is="paper-icon-button-light" class="unpauseButton mediaButton autoSize"><i class="md-icon">play_arrow</i></button>';
html += '<button is="paper-icon-button-light" class="pauseButton mediaButton autoSize"><i class="md-icon">pause</i></button>';
2014-06-24 14:45:21 -07:00
2016-06-14 20:12:32 -07:00
html += '<button is="paper-icon-button-light" class="stopButton mediaButton autoSize"><i class="md-icon">stop</i></button>';
html += '<button is="paper-icon-button-light" class="nextTrackButton mediaButton autoSize"><i class="md-icon">skip_next</i></button>';
2015-06-27 12:53:36 -07:00
html += '<div class="nowPlayingBarCurrentTime"></div>';
html += '</div>';
2015-06-27 12:53:36 -07:00
html += '<div class="nowPlayingBarRight">';
2016-06-14 20:12:32 -07:00
html += '<button is="paper-icon-button-light" class="muteButton mediaButton autoSize"><i class="md-icon">volume_up</i></button>';
html += '<button is="paper-icon-button-light" class="unmuteButton mediaButton autoSize"><i class="md-icon">volume_off</i></button>';
2016-06-24 10:30:29 -07:00
html += '<div class="sliderContainer nowPlayingBarVolumeSliderContainer hide" style="width:100px;vertical-align:middle;display:inline-flex;">';
2016-06-13 12:02:48 -07:00
html += '<input type="range" is="emby-slider" pin step="1" min="0" max="100" value="0" class="nowPlayingBarVolumeSlider"/>';
html += '</div>';
2016-06-14 20:12:32 -07:00
html += '<button is="paper-icon-button-light" class="toggleRepeatButton mediaButton autoSize"><i class="md-icon">repeat</i></button>';
2015-07-26 14:02:23 -07:00
html += '<div class="nowPlayingBarUserDataButtons">';
html += '</div>';
2016-06-14 20:12:32 -07:00
html += '<button is="paper-icon-button-light" class="unpauseButton mediaButton autoSize"><i class="md-icon">play_arrow</i></button>';
html += '<button is="paper-icon-button-light" class="pauseButton mediaButton autoSize"><i class="md-icon">pause</i></button>';
html += '<button is="paper-icon-button-light" class="remoteControlButton mediaButton autoSize"><i class="md-icon">tablet_android</i></button>';
html += '<button is="paper-icon-button-light" class="playlistButton mediaButton autoSize"><i class="md-icon">queue_music</i></button>';
2015-06-27 12:53:36 -07:00
2016-08-01 13:06:30 -07:00
html += '</div>';
2015-06-27 12:53:36 -07:00
html += '</div>';
html += '</div>';
return html;
}
2015-12-14 08:43:03 -07:00
function slideDown(elem) {
if (elem.classList.contains('hide')) {
return;
}
var onfinish = function () {
elem.classList.add('hide');
};
2016-08-06 07:07:44 -07:00
if (!browser.animate || browser.slow) {
2015-12-14 08:43:03 -07:00
onfinish();
return;
2015-12-27 11:47:48 -07:00
}
2015-12-14 08:43:03 -07:00
requestAnimationFrame(function () {
var keyframes = [
2016-08-22 13:47:16 -07:00
{ transform: 'none', offset: 0 },
{ transform: 'translateY(100%)', offset: 1 }];
2015-12-14 08:43:03 -07:00
var timing = { duration: 200, iterations: 1, fill: 'both', easing: 'ease-out' };
elem.animate(keyframes, timing).onfinish = onfinish;
});
}
function slideUp(elem) {
if (!elem.classList.contains('hide')) {
return;
}
elem.classList.remove('hide');
2016-08-06 07:07:44 -07:00
if (!browser.animate || browser.slow) {
2016-08-01 13:06:30 -07:00
return;
}
requestAnimationFrame(function () {
var keyframes = [
2016-08-22 13:47:16 -07:00
{ transform: 'translateY(100%)', offset: 0 },
{ transform: 'none', offset: 1 }];
2016-08-01 13:06:30 -07:00
var timing = { duration: 200, iterations: 1, fill: 'both', easing: 'ease-out' };
elem.animate(keyframes, timing);
});
}
function slideUpToFullScreen(elem) {
if (!elem.classList.contains('hide')) {
return;
}
elem.classList.remove('hide');
2016-08-06 07:07:44 -07:00
if (!browser.animate || browser.slow) {
2015-12-14 08:43:03 -07:00
return;
2015-12-27 11:47:48 -07:00
}
2015-12-14 08:43:03 -07:00
requestAnimationFrame(function () {
var keyframes = [
2016-08-01 13:06:30 -07:00
{ transform: 'none', offset: 0 },
{ transform: 'translateY(-100%)', offset: 1 }];
2015-12-14 08:43:03 -07:00
var timing = { duration: 200, iterations: 1, fill: 'both', easing: 'ease-out' };
elem.animate(keyframes, timing);
});
}
2016-06-01 23:08:47 -07:00
function onPauseClick() {
if (currentPlayer) {
currentPlayer.pause();
}
}
function onUnpauseClick() {
if (currentPlayer) {
currentPlayer.unpause();
}
}
function bindEvents(elem) {
2016-02-06 14:11:30 -07:00
currentTimeElement = elem.querySelector('.nowPlayingBarCurrentTime');
2016-02-05 10:04:46 -07:00
nowPlayingImageElement = elem.querySelector('.nowPlayingImage');
2016-02-06 14:11:30 -07:00
nowPlayingTextElement = elem.querySelector('.nowPlayingBarText');
nowPlayingUserData = elem.querySelector('.nowPlayingBarUserDataButtons');
2016-06-01 23:08:47 -07:00
unmuteButton = elem.querySelector('.unmuteButton');
unmuteButton.addEventListener('click', function () {
if (currentPlayer) {
currentPlayer.unMute();
}
2016-06-01 23:08:47 -07:00
});
2016-06-01 23:08:47 -07:00
muteButton = elem.querySelector('.muteButton');
muteButton.addEventListener('click', function () {
if (currentPlayer) {
currentPlayer.mute();
}
});
2016-06-01 23:08:47 -07:00
elem.querySelector('.stopButton').addEventListener('click', function () {
if (currentPlayer) {
2016-06-01 23:08:47 -07:00
currentPlayer.stop();
}
});
2016-06-01 23:08:47 -07:00
var i, length;
pauseButtons = elem.querySelectorAll('.pauseButton');
for (i = 0, length = pauseButtons.length; i < length; i++) {
pauseButtons[i].addEventListener('click', onPauseClick);
}
unpauseButtons = elem.querySelectorAll('.unpauseButton');
for (i = 0, length = unpauseButtons.length; i < length; i++) {
unpauseButtons[i].addEventListener('click', onUnpauseClick);
}
2016-06-01 23:08:47 -07:00
elem.querySelector('.nextTrackButton').addEventListener('click', function () {
if (currentPlayer) {
currentPlayer.nextTrack();
}
});
2016-06-01 23:08:47 -07:00
elem.querySelector('.previousTrackButton').addEventListener('click', function () {
if (currentPlayer) {
currentPlayer.previousTrack();
}
});
elem.querySelector('.remoteControlButton').addEventListener('click', function () {
2015-07-03 04:51:45 -07:00
showRemoteControl();
});
elem.querySelector('.playlistButton').addEventListener('click', function () {
2016-05-11 07:36:28 -07:00
showRemoteControl(2);
2015-07-03 04:51:45 -07:00
});
2016-06-01 23:08:47 -07:00
toggleRepeatButton = elem.querySelector('.toggleRepeatButton');
toggleRepeatButton.addEventListener('click', function () {
2015-07-26 14:02:23 -07:00
2015-07-30 07:34:46 -07:00
if (currentPlayer) {
var state = lastPlayerState || {};
2015-07-26 14:02:23 -07:00
switch ((state.PlayState || {}).RepeatMode) {
case 'RepeatAll':
currentPlayer.setRepeatMode('RepeatOne');
break;
case 'RepeatOne':
currentPlayer.setRepeatMode('RepeatNone');
break;
2015-07-30 07:34:46 -07:00
default:
currentPlayer.setRepeatMode('RepeatAll');
break;
2015-07-26 14:02:23 -07:00
}
}
2016-06-01 23:08:47 -07:00
});
2015-07-26 14:02:23 -07:00
2016-06-14 20:12:32 -07:00
toggleRepeatButtonIcon = toggleRepeatButton.querySelector('i');
2016-05-07 10:47:41 -07:00
2016-06-24 10:30:29 -07:00
volumeSlider = elem.querySelector('.nowPlayingBarVolumeSlider');
volumeSliderContainer = elem.querySelector('.nowPlayingBarVolumeSliderContainer');
2016-06-01 23:08:47 -07:00
2016-06-24 10:30:29 -07:00
if (AppInfo.hasPhysicalVolumeButtons) {
volumeSliderContainer.classList.add('hide');
} else {
volumeSliderContainer.classList.remove('hide');
}
2016-06-24 10:30:29 -07:00
volumeSlider.addEventListener('change', function () {
2016-06-24 10:30:29 -07:00
if (currentPlayer) {
currentPlayer.setVolume(this.value);
}
2016-06-24 10:30:29 -07:00
});
2016-06-24 10:30:29 -07:00
positionSlider = elem.querySelector('.nowPlayingBarPositionSlider');
positionSlider.addEventListener('change', function () {
2016-06-24 10:30:29 -07:00
if (currentPlayer && lastPlayerState) {
2014-04-22 10:25:54 -07:00
2016-06-24 10:30:29 -07:00
var newPercent = parseFloat(this.value);
var newPositionTicks = (newPercent / 100) * lastPlayerState.NowPlayingItem.RunTimeTicks;
2015-06-27 12:53:36 -07:00
2016-06-24 10:30:29 -07:00
currentPlayer.seek(Math.floor(newPositionTicks));
}
2015-06-27 12:53:36 -07:00
2016-06-24 10:30:29 -07:00
});
2015-06-27 12:53:36 -07:00
2016-06-24 10:30:29 -07:00
positionSlider.getBubbleText = function (value) {
2015-06-27 12:53:36 -07:00
2016-06-24 10:30:29 -07:00
var state = lastPlayerState;
2015-06-27 12:53:36 -07:00
2016-06-24 10:30:29 -07:00
if (!state || !state.NowPlayingItem || !state.NowPlayingItem.RunTimeTicks) {
return '--:--';
}
var ticks = state.NowPlayingItem.RunTimeTicks;
ticks /= 100;
ticks *= value;
2015-06-27 12:53:36 -07:00
2016-06-24 10:30:29 -07:00
return datetime.getDisplayRunningTime(ticks);
};
}
2016-05-11 07:36:28 -07:00
function showRemoteControl(tabIndex) {
2016-05-11 07:36:28 -07:00
if (tabIndex) {
Dashboard.navigate('nowplaying.html?tab=' + tabIndex);
} else {
Dashboard.navigate('nowplaying.html');
}
}
2015-12-14 08:43:03 -07:00
var nowPlayingBarElement;
function getNowPlayingBar() {
if (nowPlayingBarElement) {
return Promise.resolve(nowPlayingBarElement);
}
return new Promise(function (resolve, reject) {
2016-08-22 13:47:16 -07:00
require(['appfooter-shared', 'itemShortcuts', 'css!css/nowplayingbar.css', 'emby-slider'], function (appfooter, itemShortcuts) {
2016-08-22 13:47:16 -07:00
var parentContainer = appfooter.element;
nowPlayingBarElement = parentContainer.querySelector('.nowPlayingBar');
2015-12-14 08:43:03 -07:00
if (nowPlayingBarElement) {
resolve(nowPlayingBarElement);
return;
}
2016-08-22 13:47:16 -07:00
parentContainer.insertAdjacentHTML('beforeend', getNowPlayingBarHtml());
nowPlayingBarElement = parentContainer.querySelector('.nowPlayingBar');
2015-06-27 16:18:09 -07:00
2016-08-06 07:07:44 -07:00
if (browser.safari && browser.slow) {
2015-12-14 08:43:03 -07:00
// Not handled well here. The wrong elements receive events, bar doesn't update quickly enough, etc.
nowPlayingBarElement.classList.add('noMediaProgress');
}
itemShortcuts.on(nowPlayingBarElement);
2015-12-14 08:43:03 -07:00
bindEvents(nowPlayingBarElement);
resolve(nowPlayingBarElement);
});
});
}
2015-03-17 21:09:31 -07:00
2014-04-19 10:43:12 -07:00
function showButton(button) {
2016-06-01 23:08:47 -07:00
button.classList.remove('hide');
2014-04-19 10:43:12 -07:00
}
2015-03-17 21:09:31 -07:00
2014-04-19 10:43:12 -07:00
function hideButton(button) {
2016-06-01 23:08:47 -07:00
button.classList.add('hide');
2014-04-19 10:43:12 -07:00
}
2015-07-08 17:20:01 -07:00
var lastUpdateTime = 0;
function updatePlayerState(event, state) {
2015-12-14 08:43:03 -07:00
if (!state.NowPlayingItem) {
hideNowPlayingBar();
return;
}
2015-12-14 08:43:03 -07:00
if (nowPlayingBarElement) {
updatePlayerStateInternal(event, state);
return;
}
getNowPlayingBar().then(function () {
updatePlayerStateInternal(event, state);
});
}
function updatePlayerStateInternal(event, state) {
showNowPlayingBar();
2015-07-08 17:20:01 -07:00
if (event.type == 'positionchange') {
// Try to avoid hammering the document with changes
var now = new Date().getTime();
if ((now - lastUpdateTime) < 700) {
return;
}
lastUpdateTime = now;
}
lastPlayerState = state;
2014-04-14 20:54:52 -07:00
var playerInfo = MediaController.getPlayerInfo();
2014-04-22 10:25:54 -07:00
var playState = state.PlayState || {};
2016-06-01 23:08:47 -07:00
var i, length;
2014-04-22 10:25:54 -07:00
if (playState.IsPaused) {
2016-06-01 23:08:47 -07:00
for (i = 0, length = pauseButtons.length; i < length; i++) {
hideButton(pauseButtons[i]);
}
for (i = 0, length = unpauseButtons.length; i < length; i++) {
showButton(unpauseButtons[i]);
}
} else {
2016-06-01 23:08:47 -07:00
for (i = 0, length = pauseButtons.length; i < length; i++) {
showButton(pauseButtons[i]);
}
for (i = 0, length = unpauseButtons.length; i < length; i++) {
hideButton(unpauseButtons[i]);
}
}
2015-05-21 13:53:14 -07:00
updatePlayerVolumeState(state, playerInfo);
2014-04-14 20:54:52 -07:00
2014-04-22 10:25:54 -07:00
var nowPlayingItem = state.NowPlayingItem || {};
2015-07-11 17:33:50 -07:00
// See bindEvents for why this is necessary
if (positionSlider) {
if (!positionSlider.dragging) {
2015-07-11 17:33:50 -07:00
if (nowPlayingItem.RunTimeTicks) {
2015-07-11 17:33:50 -07:00
var pct = playState.PositionTicks / nowPlayingItem.RunTimeTicks;
pct *= 100;
2015-07-11 17:33:50 -07:00
positionSlider.value = pct;
2015-07-11 17:33:50 -07:00
} else {
2014-04-30 08:07:02 -07:00
2015-07-11 17:33:50 -07:00
positionSlider.value = 0;
}
positionSlider.disabled = !playState.CanSeek;
}
}
2016-08-19 15:56:32 -07:00
var timeText = playState.PositionTicks == null ? '--:--' : datetime.getDisplayRunningTime(playState.PositionTicks);
2014-04-22 10:25:54 -07:00
if (nowPlayingItem.RunTimeTicks) {
2016-05-05 20:09:36 -07:00
timeText += " / " + datetime.getDisplayRunningTime(nowPlayingItem.RunTimeTicks);
}
2016-02-06 14:11:30 -07:00
currentTimeElement.innerHTML = timeText;
updateNowPlayingInfo(state);
}
2015-05-21 13:53:14 -07:00
function updatePlayerVolumeState(state, playerInfo) {
playerInfo = playerInfo || MediaController.getPlayerInfo();
2015-01-03 12:38:22 -07:00
var playState = state.PlayState || {};
2015-05-21 13:53:14 -07:00
var supportedCommands = playerInfo.supportedCommands;
2015-01-03 12:38:22 -07:00
2015-05-21 13:53:14 -07:00
var showMuteButton = true;
var showUnmuteButton = true;
var showVolumeSlider = true;
2015-01-03 12:38:22 -07:00
2015-05-21 13:53:14 -07:00
if (supportedCommands.indexOf('Mute') == -1) {
showMuteButton = false;
}
if (supportedCommands.indexOf('Unmute') == -1) {
showUnmuteButton = false;
}
if (playState.IsMuted) {
2015-01-03 12:38:22 -07:00
2015-05-21 13:53:14 -07:00
showMuteButton = false;
2015-01-03 12:38:22 -07:00
} else {
2015-05-21 13:53:14 -07:00
showUnmuteButton = false;
}
2015-07-26 14:02:23 -07:00
if (supportedCommands.indexOf('SetRepeatMode') == -1) {
toggleRepeatButton.classList.add('hide');
} else {
toggleRepeatButton.classList.remove('hide');
}
if (playState.RepeatMode == 'RepeatAll') {
2016-06-14 20:12:32 -07:00
toggleRepeatButtonIcon.innerHTML = "repeat";
2015-07-26 14:02:23 -07:00
toggleRepeatButton.classList.add('repeatActive');
}
else if (playState.RepeatMode == 'RepeatOne') {
2016-06-14 20:12:32 -07:00
toggleRepeatButtonIcon.innerHTML = "repeat_one";
2015-07-26 14:02:23 -07:00
toggleRepeatButton.classList.add('repeatActive');
} else {
2016-06-14 20:12:32 -07:00
toggleRepeatButtonIcon.innerHTML = "repeat";
2015-07-26 14:02:23 -07:00
toggleRepeatButton.classList.remove('repeatActive');
}
2015-05-21 13:53:14 -07:00
if (supportedCommands.indexOf('SetVolume') == -1) {
showVolumeSlider = false;
}
if (playerInfo.isLocalPlayer && AppInfo.hasPhysicalVolumeButtons) {
showMuteButton = false;
showUnmuteButton = false;
showVolumeSlider = false;
}
if (showMuteButton) {
2015-01-03 12:38:22 -07:00
showButton(muteButton);
2015-05-21 13:53:14 -07:00
} else {
hideButton(muteButton);
}
if (showUnmuteButton) {
showButton(unmuteButton);
} else {
2015-01-03 12:38:22 -07:00
hideButton(unmuteButton);
}
2015-07-11 17:33:50 -07:00
// See bindEvents for why this is necessary
if (volumeSlider) {
2015-12-14 08:43:03 -07:00
if (showVolumeSlider) {
2016-06-24 10:30:29 -07:00
volumeSliderContainer.classList.remove('hide');
2015-12-14 08:43:03 -07:00
} else {
2016-06-24 10:30:29 -07:00
volumeSliderContainer.classList.add('hide');
2015-12-14 08:43:03 -07:00
}
2015-05-21 13:53:14 -07:00
2015-07-11 17:33:50 -07:00
if (!volumeSlider.dragging) {
volumeSlider.value = playState.VolumeLevel || 0;
}
2015-01-03 12:38:22 -07:00
}
}
function getTextActionButton(item, text) {
if (!text) {
text = itemHelper.getDisplayName(item);
}
var html = '<button data-id="' + item.Id + '" data-type="' + item.Type + '" data-mediatype="' + item.MediaType + '" data-channelid="' + item.ChannelId + '" data-isfolder="' + item.IsFolder + '" type="button" class="itemAction textActionButton" data-action="link">';
html += text;
html += '</button>';
return html;
}
2014-04-13 10:27:13 -07:00
var currentImgUrl;
function updateNowPlayingInfo(state) {
nowPlayingTextElement.innerHTML = MediaController.getNowPlayingNames(state.NowPlayingItem).map(function (nowPlayingName) {
if (nowPlayingName.item) {
return '<div>' + getTextActionButton(nowPlayingName.item, nowPlayingName.text) + '</div>';
}
return '<div>' + nowPlayingName.text + '</div>';
2015-06-26 20:27:38 -07:00
}).join('');
var url;
2016-08-01 13:06:30 -07:00
var imgHeight = 70;
2015-03-17 21:09:31 -07:00
2014-04-22 10:25:54 -07:00
var nowPlayingItem = state.NowPlayingItem;
2014-04-22 10:25:54 -07:00
if (nowPlayingItem.PrimaryImageTag) {
url = ApiClient.getScaledImageUrl(nowPlayingItem.PrimaryImageItemId, {
type: "Primary",
2015-05-01 11:37:01 -07:00
height: imgHeight,
2014-04-22 10:25:54 -07:00
tag: nowPlayingItem.PrimaryImageTag
});
}
2014-04-22 10:25:54 -07:00
else if (nowPlayingItem.BackdropImageTag) {
url = ApiClient.getScaledImageUrl(nowPlayingItem.BackdropItemId, {
type: "Backdrop",
2015-05-01 11:37:01 -07:00
height: imgHeight,
2014-04-22 10:25:54 -07:00
tag: nowPlayingItem.BackdropImageTag,
index: 0
});
2014-04-22 10:25:54 -07:00
} else if (nowPlayingItem.ThumbImageTag) {
url = ApiClient.getScaledImageUrl(nowPlayingItem.ThumbImageItemId, {
type: "Thumb",
2015-05-01 11:37:01 -07:00
height: imgHeight,
2014-04-22 10:25:54 -07:00
tag: nowPlayingItem.ThumbImageTag
});
}
2014-04-22 10:25:54 -07:00
else if (nowPlayingItem.Type == "TvChannel" || nowPlayingItem.Type == "Recording") {
url = "css/images/items/detail/tv.png";
}
2014-04-22 10:25:54 -07:00
else if (nowPlayingItem.MediaType == "Audio") {
url = "css/images/items/detail/audio.png";
}
else {
url = "css/images/items/detail/video.png";
}
2014-04-13 10:27:13 -07:00
if (url == currentImgUrl) {
return;
}
currentImgUrl = url;
2015-03-17 21:09:31 -07:00
2016-02-05 10:04:46 -07:00
ImageLoader.lazyImage(nowPlayingImageElement, url);
if (nowPlayingItem.Id) {
2015-12-14 08:43:03 -07:00
ApiClient.getItem(Dashboard.getCurrentUserId(), nowPlayingItem.Id).then(function (item) {
2016-07-17 23:45:29 -07:00
nowPlayingUserData.innerHTML = userdataButtons.getIconsHtml({
item: item,
includePlayed: false
});
});
2015-06-27 16:18:09 -07:00
} else {
2016-02-06 14:11:30 -07:00
nowPlayingUserData.innerHTML = '';
}
}
function onPlaybackStart(e, state) {
2015-12-23 10:46:01 -07:00
console.log('nowplaying event: ' + e.type);
2014-04-19 10:43:12 -07:00
var player = this;
player.beginPlayerUpdates();
onStateChanged.call(player, e, state);
}
function showNowPlayingBar() {
2015-12-14 08:43:03 -07:00
getNowPlayingBar().then(slideUp);
}
function hideNowPlayingBar() {
// Use a timeout to prevent the bar from hiding and showing quickly
// in the event of a stop->play command
// Don't call getNowPlayingBar here because we don't want to end up creating it just to hide it
2015-06-28 07:45:21 -07:00
var elem = document.getElementsByClassName('nowPlayingBar')[0];
if (elem) {
2015-12-14 08:43:03 -07:00
slideDown(elem);
2015-06-28 07:45:21 -07:00
}
}
function onPlaybackStopped(e, state) {
2015-12-23 10:46:01 -07:00
console.log('nowplaying event: ' + e.type);
var player = this;
player.endPlayerUpdates();
hideNowPlayingBar();
}
function onStateChanged(e, state) {
2015-12-23 10:46:01 -07:00
//console.log('nowplaying event: ' + e.type);
var player = this;
2014-04-22 10:25:54 -07:00
if (player.isDefaultPlayer && state.NowPlayingItem && state.NowPlayingItem.MediaType == 'Video') {
return;
}
2015-07-08 17:20:01 -07:00
updatePlayerState(e, state);
}
function releaseCurrentPlayer() {
if (currentPlayer) {
2016-08-01 13:06:30 -07:00
events.off(currentPlayer, 'playbackstart', onPlaybackStart);
events.off(currentPlayer, 'playbackstop', onPlaybackStopped);
events.off(currentPlayer, 'volumechange', onVolumeChanged);
events.off(currentPlayer, 'playstatechange', onStateChanged);
events.off(currentPlayer, 'positionchange', onStateChanged);
2015-06-29 11:45:42 -07:00
currentPlayer.endPlayerUpdates();
currentPlayer = null;
hideNowPlayingBar();
}
}
2015-01-03 12:38:22 -07:00
function onVolumeChanged(e) {
var player = this;
2015-12-14 08:43:03 -07:00
Promise.all([player.getPlayerState(), getNowPlayingBar()]).then(function (responses) {
var state = responses[0];
2015-03-17 21:09:31 -07:00
2015-01-03 12:38:22 -07:00
if (player.isDefaultPlayer && state.NowPlayingItem && state.NowPlayingItem.MediaType == 'Video') {
return;
}
updatePlayerVolumeState(state);
});
}
function bindToPlayer(player) {
releaseCurrentPlayer();
currentPlayer = player;
2015-12-14 08:43:03 -07:00
player.getPlayerState().then(function (state) {
2014-04-28 20:56:20 -07:00
if (state.NowPlayingItem) {
player.beginPlayerUpdates();
}
2015-03-17 21:09:31 -07:00
onStateChanged.call(player, { type: 'init' }, state);
});
2016-08-01 13:06:30 -07:00
events.on(player, 'playbackstart', onPlaybackStart);
events.on(player, 'playbackstop', onPlaybackStopped);
events.on(player, 'volumechange', onVolumeChanged);
events.on(player, 'playstatechange', onStateChanged);
events.on(player, 'positionchange', onStateChanged);
}
2016-08-01 13:06:30 -07:00
events.on(MediaController, 'playerchange', function () {
bindToPlayer(MediaController.getCurrentPlayer());
});
2015-12-14 08:43:03 -07:00
bindToPlayer(MediaController.getCurrentPlayer());
});