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

1976 lines
62 KiB
JavaScript
Raw Normal View History

2015-06-25 14:50:56 -07:00
(function (document, setTimeout, clearTimeout, screen, $, 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 currentProgressInterval;
var currentPlaylistIndex = 0;
2013-05-23 13:09:01 -07:00
2015-06-07 13:06:18 -07:00
self.currentMediaRenderer = null;
2014-06-11 12:31:33 -07:00
self.currentItem = null;
self.currentMediaSource = null;
2014-03-31 14:43:49 -07:00
self.currentDurationTicks = null;
2014-03-29 08:40:32 -07:00
self.startTimeTicksOffset = null;
2013-06-07 09:06:32 -07:00
self.playlist = [];
2014-03-29 08:40:32 -07:00
self.isLocalPlayer = true;
2014-04-06 10:53:23 -07:00
self.isDefaultPlayer = true;
2015-09-15 11:09:44 -07:00
self.streamInfo = {};
2014-07-01 14:13:32 -07:00
2014-03-29 09:58:49 -07:00
self.name = 'Html5 Player';
self.getTargets = function () {
var targets = [{
2015-05-06 20:11:51 -07:00
name: Globalize.translate('MyDevice'),
2014-10-25 11:32:58 -07:00
id: ConnectionManager.deviceId(),
2014-03-29 11:20:42 -07:00
playerName: self.name,
playableMediaTypes: ['Audio', 'Video'],
2014-04-13 10:27:13 -07:00
isLocalPlayer: true,
supportedCommands: Dashboard.getSupportedRemoteCommands()
2014-03-29 09:58:49 -07:00
}];
return targets;
};
2014-03-29 08:40:32 -07:00
2015-05-24 11:33:28 -07:00
var canPlayAac = document.createElement('audio').canPlayType('audio/aac').replace(/no/, '');
2015-06-13 08:16:08 -07:00
self.getVideoQualityOptions = function (videoWidth, videoHeight) {
var bitrateSetting = AppSettings.maxStreamingBitrate();
2015-06-13 08:16:08 -07:00
var maxAllowedWidth = videoWidth || 4096;
2015-06-13 07:46:59 -07:00
var maxAllowedHeight = videoHeight || 2304;
var options = [];
// Some 1080- videos are reported as 1912?
2015-06-13 08:16:08 -07:00
if (maxAllowedWidth >= 1900) {
2015-06-13 07:46:59 -07:00
2015-09-03 09:39:24 -07:00
options.push({ name: '1080p - 40Mbps', maxHeight: 1080, bitrate: 40000000 });
options.push({ name: '1080p - 35Mbps', maxHeight: 1080, bitrate: 35000000 });
2015-06-13 07:46:59 -07:00
options.push({ name: '1080p - 30Mbps', maxHeight: 1080, bitrate: 30000000 });
options.push({ name: '1080p - 25Mbps', maxHeight: 1080, bitrate: 25000000 });
options.push({ name: '1080p - 20Mbps', maxHeight: 1080, bitrate: 20000000 });
options.push({ name: '1080p - 15Mbps', maxHeight: 1080, bitrate: 15000000 });
2015-07-13 14:26:11 -07:00
options.push({ name: '1080p - 10Mbps', maxHeight: 1080, bitrate: 10000001 });
options.push({ name: '1080p - 8Mbps', maxHeight: 1080, bitrate: 8000001 });
options.push({ name: '1080p - 6Mbps', maxHeight: 1080, bitrate: 6000001 });
options.push({ name: '1080p - 5Mbps', maxHeight: 1080, bitrate: 5000001 });
2015-07-20 21:22:46 -07:00
options.push({ name: '1080p - 4Mbps', maxHeight: 1080, bitrate: 4000002 });
2015-06-13 07:46:59 -07:00
2015-06-13 08:16:08 -07:00
} else if (maxAllowedWidth >= 1260) {
2015-06-13 07:46:59 -07:00
options.push({ name: '720p - 10Mbps', maxHeight: 720, bitrate: 10000000 });
options.push({ name: '720p - 8Mbps', maxHeight: 720, bitrate: 8000000 });
options.push({ name: '720p - 6Mbps', maxHeight: 720, bitrate: 6000000 });
options.push({ name: '720p - 5Mbps', maxHeight: 720, bitrate: 5000000 });
2015-06-13 08:16:08 -07:00
} else if (maxAllowedWidth >= 700) {
2015-07-13 14:26:11 -07:00
options.push({ name: '480p - 4Mbps', maxHeight: 480, bitrate: 4000001 });
options.push({ name: '480p - 3Mbps', maxHeight: 480, bitrate: 3000001 });
2015-06-13 07:46:59 -07:00
options.push({ name: '480p - 2.5Mbps', maxHeight: 480, bitrate: 2500000 });
2015-07-13 14:26:11 -07:00
options.push({ name: '480p - 2Mbps', maxHeight: 480, bitrate: 2000001 });
options.push({ name: '480p - 1.5Mbps', maxHeight: 480, bitrate: 1500001 });
}
2015-06-13 08:16:08 -07:00
if (maxAllowedWidth >= 1260) {
2015-06-13 07:46:59 -07:00
options.push({ name: '720p - 4Mbps', maxHeight: 720, bitrate: 4000000 });
options.push({ name: '720p - 3Mbps', maxHeight: 720, bitrate: 3000000 });
options.push({ name: '720p - 2Mbps', maxHeight: 720, bitrate: 2000000 });
// The extra 1 is because they're keyed off the bitrate value
2015-07-13 14:26:11 -07:00
options.push({ name: '720p - 1.5Mbps', maxHeight: 720, bitrate: 1500000 });
2015-06-13 07:46:59 -07:00
options.push({ name: '720p - 1Mbps', maxHeight: 720, bitrate: 1000001 });
}
2015-06-13 07:46:59 -07:00
options.push({ name: '480p - 1.0Mbps', maxHeight: 480, bitrate: 1000000 });
options.push({ name: '480p - 720kbps', maxHeight: 480, bitrate: 720000 });
options.push({ name: '480p - 420kbps', maxHeight: 480, bitrate: 420000 });
options.push({ name: '360p', maxHeight: 360, bitrate: 400000 });
options.push({ name: '240p', maxHeight: 240, bitrate: 320000 });
options.push({ name: '144p', maxHeight: 144, bitrate: 192000 });
var i, length, option;
var selectedIndex = -1;
for (i = 0, length = options.length; i < length; i++) {
option = options[i];
if (selectedIndex == -1 && option.bitrate <= bitrateSetting) {
selectedIndex = i;
}
}
if (selectedIndex == -1) {
selectedIndex = options.length - 1;
}
options[selectedIndex].selected = true;
return options;
};
2015-06-13 07:46:59 -07:00
self.getDeviceProfile = function (maxHeight) {
2015-06-13 07:46:59 -07:00
if (!maxHeight) {
maxHeight = self.getVideoQualityOptions().filter(function (q) {
2015-05-26 08:31:50 -07:00
return q.selected;
2015-06-13 07:46:59 -07:00
})[0].maxHeight;
2015-05-26 08:31:50 -07:00
}
2015-07-20 21:22:46 -07:00
var isVlc = AppInfo.isNativeApp && $.browser.android;
var bitrateSetting = AppSettings.maxStreamingBitrate();
2015-03-26 12:18:21 -07:00
var canPlayWebm = self.canPlayWebm();
var profile = {};
profile.MaxStreamingBitrate = bitrateSetting;
2015-09-11 09:26:06 -07:00
profile.MaxStaticBitrate = 8000000;
2015-03-26 16:10:34 -07:00
profile.MusicStreamingTranscodingBitrate = Math.min(bitrateSetting, 192000);
profile.DirectPlayProfiles = [];
2015-07-23 06:23:22 -07:00
if (canPlayH264()) {
profile.DirectPlayProfiles.push({
Container: 'mp4,m4v',
Type: 'Video',
VideoCodec: 'h264',
AudioCodec: 'aac,mp3'
});
}
if ($.browser.chrome) {
profile.DirectPlayProfiles.push({
2015-05-02 09:34:27 -07:00
Container: 'mkv',
Type: 'Video',
VideoCodec: 'h264',
AudioCodec: 'aac,mp3'
});
profile.DirectPlayProfiles.push({
Container: 'mov',
Type: 'Video',
VideoCodec: 'h264',
AudioCodec: 'aac,mp3'
});
}
2015-07-06 19:25:23 -07:00
var directPlayVideoContainers = AppInfo.directPlayVideoContainers;
if (directPlayVideoContainers && directPlayVideoContainers.length) {
profile.DirectPlayProfiles.push({
Container: directPlayVideoContainers.join(','),
Type: 'Video'
});
}
profile.DirectPlayProfiles.push({
Container: 'mp3',
Type: 'Audio'
});
2015-05-24 11:33:28 -07:00
if (canPlayAac) {
profile.DirectPlayProfiles.push({
Container: 'aac',
Type: 'Audio'
});
}
2015-06-10 23:27:05 -07:00
var directPlayAudioContainers = AppInfo.directPlayAudioContainers;
if (directPlayAudioContainers && directPlayAudioContainers.length) {
profile.DirectPlayProfiles.push({
Container: directPlayAudioContainers.join(','),
Type: 'Audio'
});
}
2015-03-26 12:18:21 -07:00
if (canPlayWebm) {
profile.DirectPlayProfiles.push({
Container: 'webm',
Type: 'Video'
});
profile.DirectPlayProfiles.push({
Container: 'webm,webma',
Type: 'Audio'
});
}
profile.TranscodingProfiles = [];
2015-05-22 12:16:14 -07:00
if (self.canPlayHls()) {
profile.TranscodingProfiles.push({
Container: 'ts',
Type: 'Video',
AudioCodec: 'aac',
VideoCodec: 'h264',
Context: 'Streaming',
Protocol: 'hls'
});
2015-05-24 11:33:28 -07:00
2015-09-09 20:22:52 -07:00
if (canPlayAac && $.browser.safari && !AppInfo.isNativeApp) {
2015-05-24 11:33:28 -07:00
profile.TranscodingProfiles.push({
2015-06-07 18:23:56 -07:00
Container: 'ts',
2015-05-24 11:33:28 -07:00
Type: 'Audio',
AudioCodec: 'aac',
Context: 'Streaming',
Protocol: 'hls'
});
}
}
2015-03-26 12:18:21 -07:00
if (canPlayWebm) {
profile.TranscodingProfiles.push({
Container: 'webm',
Type: 'Video',
AudioCodec: 'vorbis',
VideoCodec: 'vpx',
Context: 'Streaming',
Protocol: 'http'
});
}
profile.TranscodingProfiles.push({
Container: 'mp4',
Type: 'Video',
AudioCodec: 'aac',
VideoCodec: 'h264',
Context: 'Streaming',
Protocol: 'http'
});
2015-05-31 14:07:44 -07:00
profile.TranscodingProfiles.push({
Container: 'mp4',
Type: 'Video',
AudioCodec: 'aac',
VideoCodec: 'h264',
Context: 'Static',
Protocol: 'http'
});
2015-05-24 11:33:28 -07:00
if (canPlayAac && $.browser.safari) {
2015-09-09 20:22:52 -07:00
2015-05-24 11:33:28 -07:00
profile.TranscodingProfiles.push({
Container: 'aac',
Type: 'Audio',
AudioCodec: 'aac',
Context: 'Streaming',
Protocol: 'http'
});
2015-09-09 20:22:52 -07:00
2015-05-31 14:07:44 -07:00
profile.TranscodingProfiles.push({
Container: 'aac',
Type: 'Audio',
AudioCodec: 'aac',
Context: 'Static',
Protocol: 'http'
});
2015-09-09 20:22:52 -07:00
2015-05-24 11:33:28 -07:00
} else {
profile.TranscodingProfiles.push({
Container: 'mp3',
Type: 'Audio',
AudioCodec: 'mp3',
Context: 'Streaming',
Protocol: 'http'
});
2015-05-31 14:07:44 -07:00
profile.TranscodingProfiles.push({
Container: 'mp3',
Type: 'Audio',
AudioCodec: 'mp3',
Context: 'Static',
Protocol: 'http'
});
2015-05-24 11:33:28 -07:00
}
profile.ContainerProfiles = [];
profile.CodecProfiles = [];
profile.CodecProfiles.push({
Type: 'Audio',
2015-07-16 05:56:38 -07:00
Conditions: [{
Condition: 'LessThanEqual',
Property: 'AudioChannels',
Value: '2'
}]
});
2015-07-06 19:25:23 -07:00
if (!isVlc) {
profile.CodecProfiles.push({
Type: 'VideoAudio',
Codec: 'aac',
Container: 'mkv,mov',
Conditions: [
{
Condition: 'NotEquals',
Property: 'AudioProfile',
Value: 'HE-AAC'
}
// Disabling this is going to require us to learn why it was disabled in the first place
,
2015-07-06 19:25:23 -07:00
{
Condition: 'NotEquals',
Property: 'AudioProfile',
Value: 'LC'
}
]
});
}
2015-04-24 20:30:44 -07:00
profile.CodecProfiles.push({
Type: 'VideoAudio',
2015-04-24 20:30:44 -07:00
Codec: 'aac',
2015-05-06 20:11:51 -07:00
Conditions: [
2015-05-19 12:15:40 -07:00
{
Condition: 'LessThanEqual',
Property: 'AudioChannels',
2015-09-21 08:43:10 -07:00
Value: '6'
2015-05-19 12:15:40 -07:00
}
2015-05-06 20:11:51 -07:00
]
});
2015-09-05 09:58:27 -07:00
// These don't play very well
if (isVlc) {
profile.CodecProfiles.push({
Type: 'VideoAudio',
Codec: 'dca',
Conditions: [
{
Condition: 'LessThanEqual',
Property: 'AudioChannels',
Value: 6
}
]
});
}
2015-07-06 19:25:23 -07:00
if (isVlc) {
profile.CodecProfiles.push({
Type: 'Video',
Codec: 'h264',
Conditions: [
{
Condition: 'EqualsAny',
Property: 'VideoProfile',
Value: 'high|main|baseline|constrained baseline'
},
{
Condition: 'LessThanEqual',
Property: 'VideoLevel',
Value: '41'
}]
});
} else {
profile.CodecProfiles.push({
Type: 'Video',
Codec: 'h264',
Conditions: [
{
Condition: 'NotEquals',
Property: 'IsAnamorphic',
Value: 'true',
IsRequired: false
},
{
Condition: 'EqualsAny',
Property: 'VideoProfile',
Value: 'high|main|baseline|constrained baseline'
},
{
Condition: 'LessThanEqual',
Property: 'VideoLevel',
Value: '41'
},
{
Condition: 'LessThanEqual',
Property: 'Height',
Value: maxHeight
}]
});
}
2015-07-06 19:25:23 -07:00
if (!isVlc) {
profile.CodecProfiles.push({
Type: 'Video',
Codec: 'vpx',
Conditions: [
{
Condition: 'NotEquals',
Property: 'IsAnamorphic',
Value: 'true',
IsRequired: false
},
{
Condition: 'LessThanEqual',
Property: 'Height',
Value: maxHeight
}]
});
}
// Subtitle profiles
// External vtt or burn in
profile.SubtitleProfiles = [];
if (self.supportsTextTracks()) {
2015-07-10 19:23:28 -07:00
if (isVlc) {
2015-07-19 20:43:13 -07:00
profile.SubtitleProfiles.push({
Format: 'srt',
Method: 'External'
});
2015-07-14 09:39:34 -07:00
profile.SubtitleProfiles.push({
Format: 'srt',
Method: 'Embed'
});
2015-09-12 09:39:24 -07:00
profile.SubtitleProfiles.push({
Format: 'ass',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'ssa',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'pgs',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'pgssub',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'dvdsub',
Method: 'Embed'
});
2015-07-18 11:07:03 -07:00
profile.SubtitleProfiles.push({
Format: 'vtt',
Method: 'Embed'
});
2015-09-12 09:39:24 -07:00
profile.SubtitleProfiles.push({
Format: 'sub',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'idx',
Method: 'Embed'
});
2015-07-14 09:39:34 -07:00
} else {
2015-07-10 19:23:28 -07:00
profile.SubtitleProfiles.push({
Format: 'vtt',
Method: 'External'
});
}
}
2015-05-02 09:34:27 -07:00
profile.ResponseProfiles = [];
2015-05-06 20:11:51 -07:00
//profile.ResponseProfiles.push({
// Type: 'Video',
// Container: 'mkv',
// MimeType: 'video/webm'
//});
profile.ResponseProfiles.push({
Type: 'Video',
Container: 'm4v',
MimeType: 'video/mp4'
});
2015-05-06 20:11:51 -07:00
profile.ResponseProfiles.push({
Type: 'Video',
Container: 'mov',
MimeType: 'video/webm'
});
return profile;
};
2015-06-13 07:46:59 -07:00
var supportsTextTracks;
self.supportsTextTracks = function () {
if (supportsTextTracks == null) {
supportsTextTracks = document.createElement('video').textTracks != null;
}
// For now, until ready
return supportsTextTracks;
};
2015-09-19 19:06:56 -07:00
// Returns true if the player can seek using native client-side seeking functions
function canPlayerSeek() {
2014-10-07 18:37:45 -07:00
2015-09-19 19:06:56 -07:00
var mediaRenderer = self.currentMediaRenderer;
2015-09-09 10:49:44 -07:00
var currentSrc = self.getCurrentSrc(mediaRenderer);
2014-10-03 18:42:38 -07:00
2015-09-09 10:49:44 -07:00
if ((currentSrc || '').indexOf('.m3u8') != -1) {
2015-09-19 19:06:56 -07:00
return true;
2015-09-09 10:49:44 -07:00
} else {
var duration = mediaRenderer.duration();
2015-09-19 19:06:56 -07:00
return duration && !isNaN(duration) && duration != Number.POSITIVE_INFINITY && duration != Number.NEGATIVE_INFINITY;
2015-09-09 10:49:44 -07:00
}
2015-09-19 19:06:56 -07:00
}
2013-05-23 13:09:01 -07:00
2015-06-07 13:06:18 -07:00
self.getCurrentSrc = function (mediaRenderer) {
return mediaRenderer.currentSrc();
2014-10-22 21:26:01 -07:00
};
2015-06-07 13:06:18 -07:00
self.getCurrentTicks = function (mediaRenderer) {
2014-06-13 07:24:14 -07:00
2015-06-25 14:50:56 -07:00
var playerTime = Math.floor(10000 * (mediaRenderer || self.currentMediaRenderer).currentTime());
2014-06-13 07:24:14 -07:00
2014-07-01 14:13:32 -07:00
playerTime += self.startTimeTicksOffset;
2014-06-13 07:24:14 -07:00
return playerTime;
};
2013-05-25 21:52:14 -07:00
self.playNextAfterEnded = function () {
2013-06-07 10:23:23 -07:00
self.nextTrack();
};
2013-05-23 13:09:01 -07:00
2014-05-07 11:38:50 -07:00
self.startProgressInterval = function () {
2013-05-23 13:09:01 -07:00
clearProgressInterval();
2014-11-13 23:27:10 -07:00
var intervalTime = ApiClient.isWebSocketOpen() ? 1200 : 5000;
2015-07-08 17:20:01 -07:00
// Ease up with safari because it doesn't perform as well
if ($.browser.safari) {
intervalTime = Math.max(intervalTime, 5000);
}
2014-11-13 23:27:10 -07:00
self.lastProgressReport = 0;
2013-05-23 13:09:01 -07:00
currentProgressInterval = setInterval(function () {
2015-06-07 13:06:18 -07:00
if (self.currentMediaRenderer) {
2014-11-13 23:27:10 -07:00
if ((new Date().getTime() - self.lastProgressReport) > intervalTime) {
self.lastProgressReport = new Date().getTime();
sendProgressUpdate();
}
2013-05-23 15:39:51 -07:00
}
2013-04-08 21:26:02 -07:00
2014-11-13 23:27:10 -07:00
}, 250);
};
2014-01-10 06:52:01 -07:00
2014-10-07 18:37:45 -07:00
self.getCurrentMediaExtension = function (currentSrc) {
2014-10-03 18:42:38 -07:00
currentSrc = currentSrc.split('?')[0];
return currentSrc.substring(currentSrc.lastIndexOf('.'));
};
2015-06-01 22:46:06 -07:00
self.canPlayNativeHls = function () {
2015-07-10 11:30:42 -07:00
if (AppInfo.isNativeApp) {
2015-07-06 19:25:23 -07:00
return true;
}
2015-06-07 13:06:18 -07:00
var media = document.createElement('video');
if (media.canPlayType('application/x-mpegURL').replace(/no/, '') ||
media.canPlayType('application/vnd.apple.mpegURL').replace(/no/, '')) {
return true;
}
return false;
};
2015-06-01 22:46:06 -07:00
self.canPlayHls = function () {
if (self.canPlayNativeHls()) {
return true;
}
2015-07-17 15:32:00 -07:00
// Don't use viblast with windows phone, not working at the moment.
if ($.browser.msie && $.browser.mobile) {
return false;
}
2015-07-10 11:30:42 -07:00
// viblast can help us here
//return true;
return window.MediaSource != null;
2015-06-01 22:46:06 -07:00
};
self.changeStream = function (ticks, params) {
2013-05-23 18:47:07 -07:00
2015-06-07 13:06:18 -07:00
var mediaRenderer = self.currentMediaRenderer;
2013-05-23 18:47:07 -07:00
2015-09-19 19:06:56 -07:00
if (canPlayerSeek() && params == null) {
2013-05-23 18:47:07 -07:00
2015-06-25 14:50:56 -07:00
mediaRenderer.currentTime(ticks / 10000);
return;
}
2013-05-23 18:47:07 -07:00
params = params || {};
2013-05-23 18:47:07 -07:00
2015-06-07 13:06:18 -07:00
var currentSrc = mediaRenderer.currentSrc();
2013-05-23 18:47:07 -07:00
2015-03-29 11:31:28 -07:00
var playSessionId = getParameterByName('PlaySessionId', currentSrc);
2015-03-31 09:24:16 -07:00
var liveStreamId = getParameterByName('LiveStreamId', currentSrc);
2014-01-29 14:54:40 -07:00
2015-03-26 12:18:21 -07:00
var deviceProfile = self.getDeviceProfile();
2014-04-11 20:48:57 -07:00
2015-03-26 13:31:57 -07:00
var audioStreamIndex = params.AudioStreamIndex == null ? (getParameterByName('AudioStreamIndex', currentSrc) || null) : params.AudioStreamIndex;
2015-03-26 12:18:21 -07:00
if (typeof (audioStreamIndex) == 'string') {
audioStreamIndex = parseInt(audioStreamIndex);
}
2014-04-11 20:48:57 -07:00
2015-03-26 13:31:57 -07:00
var subtitleStreamIndex = params.SubtitleStreamIndex == null ? (getParameterByName('SubtitleStreamIndex', currentSrc) || null) : params.SubtitleStreamIndex;
2015-03-26 12:18:21 -07:00
if (typeof (subtitleStreamIndex) == 'string') {
subtitleStreamIndex = parseInt(subtitleStreamIndex);
}
2014-04-02 14:55:19 -07:00
2015-05-25 10:32:22 -07:00
MediaController.getPlaybackInfo(self.currentItem.Id, deviceProfile, ticks, self.currentMediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId).done(function (result) {
2014-07-02 11:34:08 -07:00
2015-03-26 12:18:21 -07:00
if (validatePlaybackInfoResult(result)) {
2013-05-23 18:47:07 -07:00
2015-03-26 12:18:21 -07:00
self.currentMediaSource = result.MediaSources[0];
2015-09-21 08:43:10 -07:00
self.createStreamInfo(self.currentItem.MediaType, self.currentItem, self.currentMediaSource, ticks).done(function (streamInfo) {
2015-07-16 05:56:38 -07:00
2015-09-21 08:43:10 -07:00
if (!streamInfo.url) {
MediaController.showPlaybackInfoErrorMessage('NoCompatibleStream');
self.stop();
return;
}
2015-09-21 08:43:10 -07:00
self.currentSubtitleStreamIndex = subtitleStreamIndex;
2013-05-23 18:47:07 -07:00
2015-09-21 08:43:10 -07:00
changeStreamToUrl(mediaRenderer, playSessionId, streamInfo, streamInfo.startTimeTicksOffset || 0);
});
2015-03-26 12:18:21 -07:00
}
});
};
2013-05-23 18:47:07 -07:00
2015-09-15 11:09:44 -07:00
function changeStreamToUrl(mediaRenderer, playSessionId, streamInfo, newPositionTicks) {
2014-01-10 06:52:01 -07:00
clearProgressInterval();
2015-06-29 11:45:42 -07:00
Events.off(mediaRenderer, 'ended', self.onPlaybackStopped);
Events.off(mediaRenderer, 'ended', self.playNextAfterEnded);
2015-07-06 19:25:23 -07:00
2015-06-28 07:45:21 -07:00
$(mediaRenderer).one("play", function () {
2013-05-23 18:47:07 -07:00
2015-06-29 11:45:42 -07:00
Events.on(this, 'ended', self.onPlaybackStopped);
2015-07-06 19:25:23 -07:00
2015-06-29 11:45:42 -07:00
$(this).one('ended', self.playNextAfterEnded);
self.startProgressInterval();
sendProgressUpdate();
2014-04-11 20:48:57 -07:00
});
2014-06-14 19:58:00 -07:00
if (self.currentItem.MediaType == "Video") {
2015-03-29 11:31:28 -07:00
ApiClient.stopActiveEncodings(playSessionId).done(function () {
2015-07-16 05:56:38 -07:00
//self.startTimeTicksOffset = newPositionTicks;
2015-09-15 11:09:44 -07:00
self.setSrcIntoRenderer(mediaRenderer, streamInfo, self.currentItem, self.currentMediaSource);
});
2015-07-16 05:56:38 -07:00
self.startTimeTicksOffset = newPositionTicks || 0;
self.updateTextStreamUrls(newPositionTicks || 0);
} else {
2015-07-16 05:56:38 -07:00
self.startTimeTicksOffset = newPositionTicks || 0;
2015-09-15 11:09:44 -07:00
self.setSrcIntoRenderer(mediaRenderer, streamInfo, self.currentItem, self.currentMediaSource);
2013-05-23 18:47:07 -07:00
}
}
2013-04-30 12:13:06 -07:00
2015-09-15 11:09:44 -07:00
self.setSrcIntoRenderer = function (mediaRenderer, streamInfo, item, mediaSource) {
2015-07-30 18:52:11 -07:00
var subtitleStreams = mediaSource.MediaStreams.filter(function (s) {
return s.Type == 'Subtitle';
});
var textStreams = subtitleStreams.filter(function (s) {
return s.DeliveryMethod == 'External';
});
var tracks = [];
for (var i = 0, length = textStreams.length; i < length; i++) {
var textStream = textStreams[i];
var textStreamUrl = !textStream.IsExternalUrl ? ApiClient.getUrl(textStream.DeliveryUrl) : textStream.DeliveryUrl;
tracks.push({
url: textStreamUrl,
language: (textStream.Language || 'und'),
isDefault: textStream.Index == mediaSource.DefaultSubtitleStreamIndex
});
}
2015-09-15 11:09:44 -07:00
mediaRenderer.setCurrentSrc(streamInfo, item, mediaSource, tracks);
self.streamInfo = streamInfo;
2015-07-30 18:52:11 -07:00
};
self.setCurrentTime = function (ticks, positionSlider, currentTimeElement) {
2013-05-23 13:09:01 -07:00
// Convert to ticks
ticks = Math.floor(ticks);
2013-06-07 10:29:33 -07:00
var timeText = Dashboard.getDisplayTime(ticks);
2015-09-09 10:49:44 -07:00
var mediaRenderer = self.currentMediaRenderer;
2013-05-23 13:09:01 -07:00
2014-03-31 14:43:49 -07:00
if (self.currentDurationTicks) {
2013-05-23 13:09:01 -07:00
2014-03-31 14:43:49 -07:00
timeText += " / " + Dashboard.getDisplayTime(self.currentDurationTicks);
2013-05-23 13:09:01 -07:00
if (positionSlider) {
2014-03-31 14:43:49 -07:00
var percent = ticks / self.currentDurationTicks;
2013-05-23 13:09:01 -07:00
percent *= 100;
2015-06-27 12:53:36 -07:00
positionSlider.value = percent;
2013-04-30 12:13:06 -07:00
}
2015-09-09 10:49:44 -07:00
}
2015-09-09 10:49:44 -07:00
if (positionSlider) {
2015-09-19 19:06:56 -07:00
positionSlider.disabled = !((self.currentDurationTicks || 0) > 0 || canPlayerSeek());
}
if (currentTimeElement) {
currentTimeElement.html(timeText);
2013-05-24 07:35:15 -07:00
}
2013-05-23 13:09:01 -07:00
2015-09-09 10:49:44 -07:00
var state = self.getPlayerStateInternal(mediaRenderer, self.currentItem, self.currentMediaSource);
2015-06-28 07:45:21 -07:00
Events.trigger(self, 'positionchange', [state]);
};
2013-04-30 10:21:21 -07:00
2014-03-29 08:40:32 -07:00
self.canQueueMediaType = function (mediaType) {
2013-04-08 21:16:32 -07:00
2014-06-11 12:31:33 -07:00
return self.currentItem && self.currentItem.MediaType == mediaType;
2014-03-29 08:40:32 -07:00
};
2013-05-24 19:45:12 -07:00
2014-03-29 12:02:43 -07:00
function translateItemsForPlayback(items) {
var deferred = $.Deferred();
var firstItem = items[0];
var promise;
if (firstItem.Type == "Playlist") {
2014-03-29 12:02:43 -07:00
promise = self.getItemsForPlayback({
ParentId: firstItem.Id,
});
}
else if (firstItem.Type == "MusicArtist") {
2014-04-02 14:55:19 -07:00
2014-03-29 12:02:43 -07:00
promise = self.getItemsForPlayback({
2015-03-13 10:25:28 -07:00
ArtistIds: firstItem.Id,
2014-03-29 12:02:43 -07:00
Filters: "IsNotFolder",
Recursive: true,
SortBy: "SortName",
MediaTypes: "Audio"
});
}
else if (firstItem.Type == "MusicGenre") {
2014-04-02 14:55:19 -07:00
2014-03-29 12:02:43 -07:00
promise = self.getItemsForPlayback({
Genres: firstItem.Name,
Filters: "IsNotFolder",
Recursive: true,
SortBy: "SortName",
MediaTypes: "Audio"
});
}
else if (firstItem.IsFolder) {
promise = self.getItemsForPlayback({
ParentId: firstItem.Id,
Filters: "IsNotFolder",
Recursive: true,
SortBy: "SortName",
MediaTypes: "Audio,Video"
});
}
2014-03-29 12:02:43 -07:00
if (promise) {
promise.done(function (result) {
deferred.resolveWith(null, [result.Items]);
});
} else {
deferred.resolveWith(null, [items]);
}
return deferred.promise();
}
2014-03-29 08:40:32 -07:00
self.play = function (options) {
2013-05-24 19:45:12 -07:00
2015-07-09 20:00:03 -07:00
Dashboard.showLoadingMsg();
2014-03-29 08:40:32 -07:00
Dashboard.getCurrentUser().done(function (user) {
2014-03-29 08:40:32 -07:00
if (options.items) {
2014-03-29 12:02:43 -07:00
translateItemsForPlayback(options.items).done(function (items) {
2014-08-01 19:34:45 -07:00
self.playWithIntros(items, options, user);
2014-03-29 12:02:43 -07:00
});
2013-06-07 09:06:32 -07:00
2014-03-29 08:40:32 -07:00
} else {
2013-06-07 09:06:32 -07:00
2014-03-29 08:40:32 -07:00
self.getItemsForPlayback({
2013-06-07 09:06:32 -07:00
2014-03-29 08:40:32 -07:00
Ids: options.ids.join(',')
2014-03-29 08:40:32 -07:00
}).done(function (result) {
2013-05-24 19:45:12 -07:00
2014-03-29 12:02:43 -07:00
translateItemsForPlayback(result.Items).done(function (items) {
2014-08-01 19:34:45 -07:00
self.playWithIntros(items, options, user);
2014-03-29 12:02:43 -07:00
});
2013-06-07 09:06:32 -07:00
2014-03-29 08:40:32 -07:00
});
}
});
};
2014-08-01 19:34:45 -07:00
self.playWithIntros = function (items, options, user) {
var firstItem = items[0];
2015-07-09 20:00:03 -07:00
if (firstItem.MediaType === "Video") {
Dashboard.showModalLoadingMsg();
}
2015-07-03 04:51:45 -07:00
if (options.startPositionTicks || firstItem.MediaType !== 'Video') {
2014-08-01 19:34:45 -07:00
2015-03-03 00:03:17 -07:00
self.playInternal(firstItem, options.startPositionTicks, function () {
self.setPlaylistState(0, items);
2015-03-03 00:03:17 -07:00
});
2014-08-21 08:55:35 -07:00
return;
2014-08-01 19:34:45 -07:00
}
ApiClient.getJSON(ApiClient.getUrl('Users/' + user.Id + '/Items/' + firstItem.Id + '/Intros')).done(function (intros) {
items = intros.Items.concat(items);
2015-03-03 00:03:17 -07:00
self.playInternal(items[0], options.startPositionTicks, function () {
self.setPlaylistState(0, items);
2015-03-03 00:03:17 -07:00
});
2014-08-01 19:34:45 -07:00
});
};
2014-03-22 09:16:43 -07:00
function getOptimalMediaSource(mediaType, versions) {
2014-03-20 21:52:46 -07:00
2015-09-20 09:16:06 -07:00
var deferred = $.Deferred();
2014-03-20 21:52:46 -07:00
2015-09-20 09:16:06 -07:00
var promises = versions.map(function (v) {
return MediaController.supportsDirectPlay(v);
});
2014-03-20 21:52:46 -07:00
2015-09-20 09:16:06 -07:00
$.when.apply($, promises).done(function () {
2015-03-16 21:08:09 -07:00
2015-09-20 09:16:06 -07:00
for (var i = 0, length = versions.length; i < length; i++) {
versions[i].enableDirectPlay = arguments[i] || false;
}
var optimalVersion = versions.filter(function (v) {
2015-03-07 22:37:48 -07:00
2015-09-20 09:16:06 -07:00
return v.enableDirectPlay;
2015-03-07 22:37:48 -07:00
2015-09-20 09:16:06 -07:00
})[0];
2015-03-07 22:37:48 -07:00
2015-09-20 09:16:06 -07:00
if (!optimalVersion) {
optimalVersion = versions.filter(function (v) {
return v.SupportsDirectStream;
})[0];
}
optimalVersion = optimalVersion || versions.filter(function (s) {
return s.SupportsTranscoding;
2014-03-20 21:52:46 -07:00
})[0];
2014-03-20 20:31:40 -07:00
2015-09-20 09:16:06 -07:00
deferred.resolveWith(null, [optimalVersion]);
});
return deferred.promise();
2014-03-20 20:31:40 -07:00
}
2013-06-07 09:06:32 -07:00
2015-04-07 21:01:52 -07:00
self.createStreamInfo = function (type, item, mediaSource, startPosition) {
2015-09-21 08:43:10 -07:00
var deferred = $.Deferred();
var mediaUrl;
var contentType;
var startTimeTicksOffset = 0;
var startPositionInSeekParam = startPosition ? (startPosition / 10000000) : 0;
var seekParam = startPositionInSeekParam ? '#t=' + startPositionInSeekParam : '';
2015-05-18 18:46:31 -07:00
var playMethod = 'Transcode';
2015-04-07 21:01:52 -07:00
if (type == 'Video') {
contentType = 'video/' + mediaSource.Container;
if (mediaSource.enableDirectPlay) {
2015-07-14 09:39:34 -07:00
mediaUrl = mediaSource.Path;
2015-05-18 18:46:31 -07:00
playMethod = 'DirectPlay';
2015-05-31 11:22:51 -07:00
} else {
if (mediaSource.SupportsDirectStream) {
var directOptions = {
Static: true,
mediaSourceId: mediaSource.Id,
deviceId: ApiClient.deviceId(),
api_key: ApiClient.accessToken()
};
if (mediaSource.LiveStreamId) {
directOptions.LiveStreamId = mediaSource.LiveStreamId;
}
mediaUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.' + mediaSource.Container, directOptions);
mediaUrl += seekParam;
2015-05-18 18:46:31 -07:00
playMethod = 'DirectStream';
} else if (mediaSource.SupportsTranscoding) {
2015-04-01 19:47:59 -07:00
mediaUrl = ApiClient.getUrl(mediaSource.TranscodingUrl);
if (mediaSource.TranscodingSubProtocol == 'hls') {
mediaUrl += seekParam;
contentType = 'application/x-mpegURL';
} else {
2015-07-03 04:51:45 -07:00
startTimeTicksOffset = startPosition || 0;
contentType = 'video/' + mediaSource.TranscodingContainer;
}
}
}
} else {
contentType = 'audio/' + mediaSource.Container;
if (mediaSource.enableDirectPlay) {
2015-07-14 09:39:34 -07:00
mediaUrl = mediaSource.Path;
2015-05-18 18:46:31 -07:00
playMethod = 'DirectPlay';
2015-05-31 11:22:51 -07:00
} else {
var isDirectStream = mediaSource.SupportsDirectStream;
if (isDirectStream) {
var outputContainer = (mediaSource.Container || '').toLowerCase();
var directOptions = {
Static: true,
mediaSourceId: mediaSource.Id,
deviceId: ApiClient.deviceId(),
api_key: ApiClient.accessToken()
};
if (mediaSource.LiveStreamId) {
directOptions.LiveStreamId = mediaSource.LiveStreamId;
}
mediaUrl = ApiClient.getUrl('Audio/' + item.Id + '/stream.' + outputContainer, directOptions);
mediaUrl += seekParam;
2015-05-18 18:46:31 -07:00
playMethod = 'DirectStream';
} else if (mediaSource.SupportsTranscoding) {
2015-04-01 19:47:59 -07:00
mediaUrl = ApiClient.getUrl(mediaSource.TranscodingUrl);
2015-07-03 04:51:45 -07:00
if (mediaSource.TranscodingSubProtocol == 'hls') {
mediaUrl += seekParam;
contentType = 'application/x-mpegURL';
} else {
startTimeTicksOffset = startPosition || 0;
contentType = 'audio/' + mediaSource.TranscodingContainer;
}
}
}
}
2015-09-21 08:43:10 -07:00
var resultInfo = {
url: mediaUrl,
2015-05-25 10:32:22 -07:00
mimeType: contentType,
startTimeTicksOffset: startTimeTicksOffset,
2015-05-18 18:46:31 -07:00
startPositionInSeekParam: startPositionInSeekParam,
playMethod: playMethod
};
2015-09-21 08:43:10 -07:00
if (playMethod == 'DirectPlay' && mediaSource.Protocol == 'File') {
require(['localassetmanager'], function () {
LocalAssetManager.translateFilePath(resultInfo.url).done(function (path) {
resultInfo.url = path;
Logger.log('LocalAssetManager.translateFilePath: path: ' + resultInfo.url + ' result: ' + path);
deferred.resolveWith(null, [resultInfo]);
});
});
}
else {
deferred.resolveWith(null, [resultInfo]);
}
return deferred.promise();
};
2015-09-05 21:53:37 -07:00
self.lastBitrateDetect = 0;
2015-09-05 14:15:36 -07:00
2015-03-03 00:03:17 -07:00
self.playInternal = function (item, startPosition, callback) {
2013-07-11 13:25:12 -07:00
if (item == null) {
throw new Error("item cannot be null");
}
2013-06-07 10:23:23 -07:00
if (self.isPlaying()) {
2015-06-25 14:50:56 -07:00
self.stop(false);
2013-04-10 14:03:28 -07:00
}
2013-04-08 21:16:32 -07:00
2015-03-07 22:37:48 -07:00
if (item.MediaType !== 'Audio' && item.MediaType !== 'Video') {
throw new Error("Unrecognized media type");
}
2015-03-25 21:44:24 -07:00
if (item.IsPlaceHolder) {
2015-07-09 20:00:03 -07:00
Dashboard.hideModalLoadingMsg();
2015-05-19 12:15:40 -07:00
MediaController.showPlaybackInfoErrorMessage('PlaceHolder');
2015-03-25 21:44:24 -07:00
return;
}
2015-09-05 21:53:37 -07:00
if (item.MediaType == 'Video' && AppSettings.enableAutomaticBitrateDetection() && (new Date().getTime() - self.lastBitrateDetect) > 300000) {
2015-09-05 14:15:36 -07:00
Dashboard.showModalLoadingMsg();
ApiClient.detectBitrate().done(function (bitrate) {
Logger.log('Max bitrate auto detected to ' + bitrate);
2015-09-05 21:53:37 -07:00
self.lastBitrateDetect = new Date().getTime();
2015-09-05 14:15:36 -07:00
AppSettings.maxStreamingBitrate(bitrate);
playOnDeviceProfileCreated(self.getDeviceProfile(), item, startPosition, callback);
}).fail(function () {
playOnDeviceProfileCreated(self.getDeviceProfile(), item, startPosition, callback);
});
} else {
playOnDeviceProfileCreated(self.getDeviceProfile(), item, startPosition, callback);
}
};
2015-09-20 09:16:06 -07:00
self.tryStartPlayback = function (deviceProfile, item, startPosition, callback) {
2015-05-06 20:11:51 -07:00
if (item.MediaType === "Video") {
Dashboard.showModalLoadingMsg();
}
2015-04-14 08:26:02 -07:00
2015-05-25 10:32:22 -07:00
MediaController.getPlaybackInfo(item.Id, deviceProfile, startPosition).done(function (playbackInfoResult) {
2015-03-07 22:37:48 -07:00
2015-04-13 09:21:10 -07:00
if (validatePlaybackInfoResult(playbackInfoResult)) {
2013-04-08 21:16:32 -07:00
2015-09-20 09:16:06 -07:00
getOptimalMediaSource(item.MediaType, playbackInfoResult.MediaSources).done(function (mediaSource) {
if (mediaSource) {
2014-11-18 19:45:12 -07:00
2015-09-20 09:16:06 -07:00
if (mediaSource.RequiresOpening) {
2015-02-05 22:39:07 -07:00
2015-09-20 09:16:06 -07:00
MediaController.getLiveStream(item.Id, playbackInfoResult.PlaySessionId, deviceProfile, startPosition, mediaSource, null, null).done(function (openLiveStreamResult) {
2014-11-18 19:45:12 -07:00
2015-09-20 09:16:06 -07:00
MediaController.supportsDirectPlay(openLiveStreamResult.MediaSource).done(function (result) {
2015-03-03 00:03:17 -07:00
2015-09-20 09:16:06 -07:00
openLiveStreamResult.MediaSource.enableDirectPlay = result;
callback(openLiveStreamResult.MediaSource);
});
2015-04-13 09:21:10 -07:00
2015-09-20 09:16:06 -07:00
});
2015-03-03 00:03:17 -07:00
2015-09-20 09:16:06 -07:00
} else {
callback(mediaSource);
}
2015-03-28 21:56:39 -07:00
} else {
2015-09-20 09:16:06 -07:00
Dashboard.hideModalLoadingMsg();
MediaController.showPlaybackInfoErrorMessage('NoCompatibleStream');
2015-03-07 22:37:48 -07:00
}
2015-09-20 09:16:06 -07:00
});
2015-03-03 00:03:17 -07:00
}
2015-03-07 22:37:48 -07:00
});
2015-09-20 09:16:06 -07:00
};
function playOnDeviceProfileCreated(deviceProfile, item, startPosition, callback) {
self.tryStartPlayback(deviceProfile, item, startPosition, function (mediaSource) {
playInternalPostMediaSourceSelection(item, mediaSource, startPosition, callback);
});
2015-09-05 14:15:36 -07:00
}
2015-03-28 21:56:39 -07:00
function playInternalPostMediaSourceSelection(item, mediaSource, startPosition, callback) {
2015-08-24 13:37:34 -07:00
Dashboard.hideModalLoadingMsg();
2015-04-14 08:26:02 -07:00
2015-03-28 21:56:39 -07:00
self.currentMediaSource = mediaSource;
self.currentItem = item;
if (item.MediaType === "Video") {
2015-07-05 11:34:52 -07:00
requirejs(['videorenderer'], function () {
2015-07-08 09:10:34 -07:00
self.playVideo(item, self.currentMediaSource, startPosition, callback);
2015-07-05 11:34:52 -07:00
});
2015-03-28 21:56:39 -07:00
} else if (item.MediaType === "Audio") {
2015-07-08 09:10:34 -07:00
playAudio(item, self.currentMediaSource, startPosition, callback);
2015-03-28 21:56:39 -07:00
}
}
2015-03-03 00:03:17 -07:00
function validatePlaybackInfoResult(result) {
2015-03-06 20:53:31 -07:00
if (result.ErrorCode) {
2015-05-19 12:15:40 -07:00
MediaController.showPlaybackInfoErrorMessage(result.ErrorCode);
2015-03-06 20:53:31 -07:00
return false;
}
2015-03-03 00:03:17 -07:00
return true;
}
2015-05-08 20:48:43 -07:00
self.getPosterUrl = function (item) {
2014-04-13 10:27:13 -07:00
2015-05-08 20:48:43 -07:00
var screenWidth = Math.max(screen.height, screen.width);
2014-04-13 10:27:13 -07:00
2015-05-08 20:48:43 -07:00
if (item.BackdropImageTags && item.BackdropImageTags.length) {
2014-04-13 10:27:13 -07:00
2015-05-08 20:48:43 -07:00
return ApiClient.getScaledImageUrl(item.Id, {
type: "Backdrop",
index: 0,
maxWidth: screenWidth,
tag: item.BackdropImageTags[0]
});
2014-04-13 10:27:13 -07:00
}
2015-05-08 20:48:43 -07:00
else if (item.ParentBackdropItemId && item.ParentBackdropImageTags && item.ParentBackdropImageTags.length) {
return ApiClient.getScaledImageUrl(item.ParentBackdropItemId, {
type: 'Backdrop',
index: 0,
maxWidth: screenWidth,
tag: item.ParentBackdropImageTags[0]
});
2014-04-13 10:27:13 -07:00
}
2015-05-08 20:48:43 -07:00
return null;
2014-04-13 10:27:13 -07:00
};
self.displayContent = function (options) {
2014-04-14 20:54:52 -07:00
2014-04-13 10:27:13 -07:00
// Handle it the same as a remote control command
2014-08-25 19:30:52 -07:00
Dashboard.onBrowseCommand(options);
2013-04-10 14:03:28 -07:00
};
2013-06-07 09:06:32 -07:00
self.getItemsForPlayback = function (query) {
var userId = Dashboard.getCurrentUserId();
2015-02-24 11:25:19 -07:00
if (query.Ids && query.Ids.split(',').length == 1) {
var deferred = DeferredBuilder.Deferred();
ApiClient.getItem(userId, query.Ids.split(',')).done(function (item) {
deferred.resolveWith(null, [
{
Items: [item],
TotalRecordCount: 1
}]);
});
2013-06-07 09:06:32 -07:00
2015-02-24 11:25:19 -07:00
return deferred.promise();
}
else {
query.Limit = query.Limit || 100;
query.Fields = getItemFields;
query.ExcludeLocationTypes = "Virtual";
return ApiClient.getItems(userId, query);
}
2013-06-07 09:06:32 -07:00
};
self.removeFromPlaylist = function (index) {
2013-06-07 10:23:23 -07:00
2013-06-07 09:06:32 -07:00
self.playlist.remove(index);
2013-06-07 10:23:23 -07:00
2013-05-23 13:09:01 -07:00
};
2013-06-07 09:06:32 -07:00
// Gets or sets the current playlist index
2013-06-07 10:23:23 -07:00
self.currentPlaylistIndex = function (i) {
2013-06-07 09:06:32 -07:00
if (i == null) {
return currentPlaylistIndex;
}
2013-05-23 13:09:01 -07:00
2013-06-07 09:06:32 -07:00
var newItem = self.playlist[i];
2013-05-23 13:09:01 -07:00
2015-03-03 00:03:17 -07:00
self.playInternal(newItem, 0, function () {
self.setPlaylistState(i);
2013-06-07 09:06:32 -07:00
});
2013-04-30 10:21:21 -07:00
};
// Set currentPlaylistIndex and playlist. Using a method allows for overloading in derived player implementations
self.setPlaylistState = function (i, items) {
if (!isNaN(i)) {
currentPlaylistIndex = i;
}
if (items) {
self.playlist = items;
}
if (self.updatePlaylistUi) {
self.updatePlaylistUi();
}
};
self.nextTrack = function () {
2013-05-23 13:09:01 -07:00
2015-07-26 14:02:23 -07:00
var newIndex;
switch (self.getRepeatMode()) {
case 'RepeatOne':
newIndex = currentPlaylistIndex;
break;
case 'RepeatAll':
newIndex = currentPlaylistIndex + 1;
if (newIndex >= self.playlist.length) {
newIndex = 0;
}
break;
default:
newIndex = currentPlaylistIndex + 1;
break;
}
2013-06-07 09:06:32 -07:00
var newItem = self.playlist[newIndex];
2013-05-23 13:09:01 -07:00
if (newItem) {
2014-07-17 15:21:35 -07:00
2015-06-26 20:27:38 -07:00
Logger.log('playing next track');
2014-08-10 15:13:17 -07:00
2015-03-03 00:03:17 -07:00
self.playInternal(newItem, 0, function () {
self.setPlaylistState(newIndex);
});
}
2013-05-23 13:09:01 -07:00
};
2013-08-09 08:55:22 -07:00
self.previousTrack = function () {
var newIndex = currentPlaylistIndex - 1;
if (newIndex >= 0) {
var newItem = self.playlist[newIndex];
if (newItem) {
2015-03-03 00:03:17 -07:00
self.playInternal(newItem, 0, function () {
self.setPlaylistState(newIndex);
});
}
}
};
2013-11-30 11:32:39 -07:00
self.queueItemsNext = function (items) {
var insertIndex = 1;
for (var i = 0, length = items.length; i < length; i++) {
self.playlist.splice(insertIndex, 0, items[i]);
insertIndex++;
}
};
2013-11-29 23:07:45 -07:00
self.queueItems = function (items) {
2013-06-07 09:06:32 -07:00
2013-11-29 23:07:45 -07:00
for (var i = 0, length = items.length; i < length; i++) {
2013-06-07 09:06:32 -07:00
2013-11-29 23:07:45 -07:00
self.playlist.push(items[i]);
}
2013-04-30 10:21:21 -07:00
};
2014-03-29 08:40:32 -07:00
self.queue = function (options) {
2013-06-07 09:06:32 -07:00
2015-06-07 13:06:18 -07:00
if (!self.playlist.length) {
2014-03-29 08:40:32 -07:00
self.play(options);
2014-01-18 10:58:05 -07:00
return;
}
2014-01-29 14:54:40 -07:00
2014-03-29 08:40:32 -07:00
Dashboard.getCurrentUser().done(function (user) {
2013-06-07 09:06:32 -07:00
2014-03-29 08:40:32 -07:00
if (options.items) {
2014-03-29 12:02:43 -07:00
translateItemsForPlayback(options.items).done(function (items) {
self.queueItems(items);
});
2014-03-29 08:40:32 -07:00
} else {
2013-06-07 09:06:32 -07:00
self.getItemsForPlayback({
2014-03-29 08:40:32 -07:00
Ids: options.ids.join(',')
2013-06-07 09:06:32 -07:00
}).done(function (result) {
2014-03-29 12:02:43 -07:00
translateItemsForPlayback(result.Items).done(function (items) {
2013-06-07 09:06:32 -07:00
2014-03-29 12:02:43 -07:00
self.queueItems(items);
});
2013-06-07 09:06:32 -07:00
2014-03-29 08:40:32 -07:00
});
2013-06-07 09:06:32 -07:00
}
2013-05-23 13:09:01 -07:00
});
};
2014-03-29 08:40:32 -07:00
self.queueNext = function (options) {
2013-08-08 07:53:25 -07:00
2015-06-07 13:06:18 -07:00
if (!self.playlist.length) {
2014-03-29 08:40:32 -07:00
self.play(options);
return;
}
2013-08-08 07:53:25 -07:00
2014-03-29 08:40:32 -07:00
Dashboard.getCurrentUser().done(function (user) {
2013-08-08 07:53:25 -07:00
2014-03-29 08:40:32 -07:00
if (options.items) {
2013-08-08 07:53:25 -07:00
2014-03-29 08:40:32 -07:00
self.queueItemsNext(options.items);
2013-08-08 07:53:25 -07:00
2014-03-29 08:40:32 -07:00
} else {
self.getItemsForPlayback({
2013-08-08 07:53:25 -07:00
2014-03-29 08:40:32 -07:00
Ids: options.ids.join(',')
}).done(function (result) {
options.items = result.Items;
self.queueItemsNext(options.items);
});
}
});
2013-08-08 07:53:25 -07:00
};
2013-05-23 13:09:01 -07:00
self.pause = function () {
2014-04-13 11:25:22 -07:00
2015-06-07 13:06:18 -07:00
self.currentMediaRenderer.pause();
2013-05-23 13:09:01 -07:00
};
self.unpause = function () {
2015-06-07 13:06:18 -07:00
self.currentMediaRenderer.unpause();
2013-05-23 13:09:01 -07:00
};
self.seek = function (position) {
2013-07-25 12:43:40 -07:00
self.changeStream(position);
};
2013-08-09 08:55:22 -07:00
2013-05-23 13:09:01 -07:00
self.mute = function () {
self.setVolume(0);
2013-05-23 13:09:01 -07:00
};
self.unMute = function () {
self.setVolume(self.getSavedVolume() * 100);
};
2014-11-13 23:27:10 -07:00
self.volume = function () {
2015-06-07 13:06:18 -07:00
return self.currentMediaRenderer.volume() * 100;
2014-10-22 21:26:01 -07:00
};
self.toggleMute = function () {
2015-06-07 13:06:18 -07:00
if (self.currentMediaRenderer) {
2015-06-26 20:27:38 -07:00
Logger.log('MediaPlayer toggling mute');
2014-04-30 20:24:55 -07:00
2014-10-22 21:26:01 -07:00
if (self.volume()) {
self.mute();
} else {
self.unMute();
}
}
};
2013-08-27 22:44:43 -07:00
self.volumeDown = function () {
2015-06-07 13:06:18 -07:00
if (self.currentMediaRenderer) {
2014-10-22 21:26:01 -07:00
self.setVolume(Math.max(self.volume() - 2, 0));
}
};
2013-08-27 22:44:43 -07:00
self.volumeUp = function () {
2015-06-07 13:06:18 -07:00
if (self.currentMediaRenderer) {
2014-10-22 21:26:01 -07:00
self.setVolume(Math.min(self.volume() + 2, 100));
}
};
// Sets volume using a 0-100 scale
self.setVolume = function (val) {
2015-06-07 13:06:18 -07:00
if (self.currentMediaRenderer) {
2015-06-26 20:27:38 -07:00
Logger.log('MediaPlayer setting volume to ' + val);
2015-06-07 13:06:18 -07:00
self.currentMediaRenderer.volume(val / 100);
2015-06-07 13:06:18 -07:00
self.onVolumeChanged(self.currentMediaRenderer);
2015-06-07 13:06:18 -07:00
//self.saveVolume();
}
2013-04-30 10:21:21 -07:00
};
self.saveVolume = function (val) {
if (val) {
2015-06-25 14:50:56 -07:00
appStorage.setItem("volume", val);
}
};
self.getSavedVolume = function () {
2015-06-25 14:50:56 -07:00
return appStorage.getItem("volume") || 0.5;
};
2014-03-29 08:40:32 -07:00
self.shuffle = function (id) {
var userId = Dashboard.getCurrentUserId();
ApiClient.getItem(userId, id).done(function (item) {
var query = {
UserId: userId,
Fields: getItemFields,
2014-11-02 20:38:43 -07:00
Limit: 100,
2014-03-29 08:40:32 -07:00
Filters: "IsNotFolder",
Recursive: true,
SortBy: "Random"
};
if (item.Type == "MusicArtist") {
2014-03-29 08:40:32 -07:00
query.MediaTypes = "Audio";
2015-03-13 10:25:28 -07:00
query.ArtistIds = item.Id;
2014-03-29 08:40:32 -07:00
}
else if (item.Type == "MusicGenre") {
query.MediaTypes = "Audio";
query.Genres = item.Name;
}
else if (item.IsFolder) {
query.ParentId = id;
}
else {
2014-03-29 08:40:32 -07:00
return;
}
self.getItemsForPlayback(query).done(function (result) {
self.play({ items: result.Items });
});
});
};
self.instantMix = function (id) {
var userId = Dashboard.getCurrentUserId();
ApiClient.getItem(userId, id).done(function (item) {
var promise;
2014-11-02 20:38:43 -07:00
var itemLimit = 100;
2014-03-29 08:40:32 -07:00
2015-08-27 21:19:08 -07:00
promise = ApiClient.getInstantMixFromItem(id, {
UserId: Dashboard.getCurrentUserId(),
Fields: getItemFields,
Limit: itemLimit
});
2014-03-29 08:40:32 -07:00
promise.done(function (result) {
self.play({ items: result.Items });
});
});
};
2015-06-25 14:50:56 -07:00
self.stop = function (destroyRenderer) {
2013-03-31 18:52:07 -07:00
2015-06-07 13:06:18 -07:00
var mediaRenderer = self.currentMediaRenderer;
2013-03-31 18:52:07 -07:00
2015-06-07 13:06:18 -07:00
if (mediaRenderer) {
2013-02-20 18:33:05 -07:00
2015-06-10 20:01:41 -07:00
mediaRenderer.stop();
2015-06-29 11:45:42 -07:00
Events.off(mediaRenderer, 'ended', self.playNextAfterEnded);
2013-06-07 10:23:23 -07:00
2015-07-06 19:25:23 -07:00
$(mediaRenderer).one("ended", function () {
2015-06-28 07:45:21 -07:00
2015-06-29 11:45:42 -07:00
$(this).off('.mediaplayerevent');
2015-06-25 14:50:56 -07:00
this.cleanup(destroyRenderer);
2015-06-07 13:06:18 -07:00
self.currentMediaRenderer = null;
2014-07-01 14:13:32 -07:00
self.currentItem = null;
self.currentMediaSource = null;
2015-09-15 11:09:44 -07:00
self.currentSubtitleStreamIndex = null;
self.streamInfo = {};
2015-06-28 07:45:21 -07:00
});
2015-07-06 19:25:23 -07:00
2015-06-28 07:45:21 -07:00
Events.trigger(mediaRenderer, "ended");
2014-07-01 14:13:32 -07:00
} else {
2015-06-07 13:06:18 -07:00
self.currentMediaRenderer = null;
2014-06-11 12:31:33 -07:00
self.currentItem = null;
self.currentMediaSource = null;
2015-09-15 11:09:44 -07:00
self.currentSubtitleStreamIndex = null;
self.streamInfo = {};
2014-07-01 14:13:32 -07:00
}
2013-03-26 14:32:01 -07:00
2015-03-26 13:48:24 -07:00
if (self.isFullScreen()) {
self.exitFullScreen();
2013-12-29 19:41:22 -07:00
}
2015-03-26 13:48:24 -07:00
self.resetEnhancements();
2013-04-10 14:03:28 -07:00
};
2013-03-26 14:32:01 -07:00
2013-04-10 14:03:28 -07:00
self.isPlaying = function () {
2015-06-07 13:06:18 -07:00
return self.playlist.length > 0;
2013-04-10 14:03:28 -07:00
};
2013-05-25 17:53:51 -07:00
2014-04-13 10:27:13 -07:00
self.getPlayerState = function () {
var deferred = $.Deferred();
2015-06-07 13:06:18 -07:00
var result = self.getPlayerStateInternal(self.currentMediaRenderer, self.currentItem, self.currentMediaSource);
deferred.resolveWith(null, [result]);
return deferred.promise();
};
2015-06-07 13:06:18 -07:00
self.getPlayerStateInternal = function (mediaRenderer, item, mediaSource) {
2014-04-22 10:25:54 -07:00
var state = {
PlayState: {}
};
2015-06-07 13:06:18 -07:00
if (mediaRenderer) {
2015-06-07 13:06:18 -07:00
state.PlayState.VolumeLevel = mediaRenderer.volume() * 100;
state.PlayState.IsMuted = mediaRenderer.volume() == 0;
state.PlayState.IsPaused = mediaRenderer.paused();
state.PlayState.PositionTicks = self.getCurrentTicks(mediaRenderer);
2015-07-26 14:02:23 -07:00
state.PlayState.RepeatMode = self.getRepeatMode();
2014-04-22 10:25:54 -07:00
2015-09-15 11:09:44 -07:00
var currentSrc = mediaRenderer.currentSrc();
2014-04-22 10:25:54 -07:00
if (currentSrc) {
var audioStreamIndex = getParameterByName('AudioStreamIndex', currentSrc);
if (audioStreamIndex) {
state.PlayState.AudioStreamIndex = parseInt(audioStreamIndex);
}
2014-06-11 12:31:33 -07:00
state.PlayState.SubtitleStreamIndex = self.currentSubtitleStreamIndex;
2014-04-22 10:25:54 -07:00
2015-09-15 11:09:44 -07:00
state.PlayState.PlayMethod = self.streamInfo.playMethod;
2015-03-29 11:38:18 -07:00
2015-09-15 11:09:44 -07:00
state.PlayState.LiveStreamId = mediaSource.LiveStreamId;
2015-04-01 14:55:50 -07:00
state.PlayState.PlaySessionId = getParameterByName('PlaySessionId', currentSrc);
2014-04-22 10:25:54 -07:00
}
}
if (mediaSource) {
2014-04-22 10:25:54 -07:00
state.PlayState.MediaSourceId = mediaSource.Id;
state.NowPlayingItem = {
RunTimeTicks: mediaSource.RunTimeTicks
};
2015-09-19 19:06:56 -07:00
state.PlayState.CanSeek = (mediaSource.RunTimeTicks || 0) > 0 || canPlayerSeek();
}
if (item) {
2015-05-25 10:32:22 -07:00
state.NowPlayingItem = self.getNowPlayingItemForReporting(item, mediaSource);
}
2014-04-27 18:57:29 -07:00
2015-05-25 10:32:22 -07:00
return state;
};
2014-04-22 10:25:54 -07:00
2015-05-25 10:32:22 -07:00
self.getNowPlayingItemForReporting = function (item, mediaSource) {
2015-05-25 10:32:22 -07:00
var nowPlayingItem = {};
2015-05-25 10:32:22 -07:00
nowPlayingItem.RunTimeTicks = mediaSource.RunTimeTicks;
2014-06-28 12:35:30 -07:00
2015-05-25 10:32:22 -07:00
nowPlayingItem.Id = item.Id;
nowPlayingItem.MediaType = item.MediaType;
nowPlayingItem.Type = item.Type;
nowPlayingItem.Name = item.Name;
2015-05-25 10:32:22 -07:00
nowPlayingItem.IndexNumber = item.IndexNumber;
nowPlayingItem.IndexNumberEnd = item.IndexNumberEnd;
nowPlayingItem.ParentIndexNumber = item.ParentIndexNumber;
nowPlayingItem.ProductionYear = item.ProductionYear;
nowPlayingItem.PremiereDate = item.PremiereDate;
nowPlayingItem.SeriesName = item.SeriesName;
nowPlayingItem.Album = item.Album;
nowPlayingItem.Artists = item.Artists;
2014-04-13 10:27:13 -07:00
2015-05-25 10:32:22 -07:00
var imageTags = item.ImageTags || {};
2014-04-13 10:27:13 -07:00
2015-05-25 10:32:22 -07:00
if (item.SeriesPrimaryImageTag) {
2015-05-25 10:32:22 -07:00
nowPlayingItem.PrimaryImageItemId = item.SeriesId;
nowPlayingItem.PrimaryImageTag = item.SeriesPrimaryImageTag;
}
else if (imageTags.Primary) {
2015-05-25 10:32:22 -07:00
nowPlayingItem.PrimaryImageItemId = item.Id;
nowPlayingItem.PrimaryImageTag = imageTags.Primary;
}
else if (item.AlbumPrimaryImageTag) {
2015-05-25 10:32:22 -07:00
nowPlayingItem.PrimaryImageItemId = item.AlbumId;
nowPlayingItem.PrimaryImageTag = item.AlbumPrimaryImageTag;
}
else if (item.SeriesPrimaryImageTag) {
2015-05-25 10:32:22 -07:00
nowPlayingItem.PrimaryImageItemId = item.SeriesId;
nowPlayingItem.PrimaryImageTag = item.SeriesPrimaryImageTag;
}
2014-06-28 12:35:30 -07:00
2015-05-25 10:32:22 -07:00
if (item.BackdropImageTags && item.BackdropImageTags.length) {
2014-06-28 12:35:30 -07:00
2015-05-25 10:32:22 -07:00
nowPlayingItem.BackdropItemId = item.Id;
nowPlayingItem.BackdropImageTag = item.BackdropImageTags[0];
}
else if (item.ParentBackdropImageTags && item.ParentBackdropImageTags.length) {
nowPlayingItem.BackdropItemId = item.ParentBackdropItemId;
nowPlayingItem.BackdropImageTag = item.ParentBackdropImageTags[0];
}
2014-06-28 12:35:30 -07:00
2015-05-25 10:32:22 -07:00
if (imageTags.Thumb) {
nowPlayingItem.ThumbItemId = item.Id;
nowPlayingItem.ThumbImageTag = imageTags.Thumb;
}
2015-05-25 10:32:22 -07:00
if (imageTags.Logo) {
nowPlayingItem.LogoItemId = item.Id;
nowPlayingItem.LogoImageTag = imageTags.Logo;
}
else if (item.ParentLogoImageTag) {
nowPlayingItem.LogoItemId = item.ParentLogoItemId;
nowPlayingItem.LogoImageTag = item.ParentLogoImageTag;
}
return nowPlayingItem;
};
self.beginPlayerUpdates = function () {
// Nothing to setup here
};
self.endPlayerUpdates = function () {
// Nothing to setup here
};
2015-06-07 13:06:18 -07:00
self.onPlaybackStart = function (mediaRenderer, item, mediaSource) {
2015-06-07 13:06:18 -07:00
var state = self.getPlayerStateInternal(mediaRenderer, item, mediaSource);
2015-06-28 07:45:21 -07:00
Events.trigger(self, 'playbackstart', [state]);
2014-05-07 11:38:50 -07:00
self.startProgressInterval();
};
2015-06-07 13:06:18 -07:00
self.onVolumeChanged = function (mediaRenderer) {
2015-06-07 13:06:18 -07:00
self.saveVolume(mediaRenderer.volume());
2015-06-07 13:06:18 -07:00
var state = self.getPlayerStateInternal(mediaRenderer, self.currentItem, self.currentMediaSource);
2015-06-28 07:45:21 -07:00
Events.trigger(self, 'volumechange', [state]);
};
2014-08-14 06:24:30 -07:00
self.cleanup = function () {
2014-08-10 15:13:17 -07:00
};
self.onPlaybackStopped = function () {
2015-06-26 20:27:38 -07:00
Logger.log('playback stopped');
2014-08-10 15:13:17 -07:00
2015-06-28 07:45:21 -07:00
document.body.classList.remove('bodyWithPopupOpen');
2014-06-28 12:35:30 -07:00
2015-06-07 13:06:18 -07:00
var mediaRenderer = this;
2015-06-28 07:45:21 -07:00
Events.off(mediaRenderer, '.mediaplayerevent');
2015-07-06 19:25:23 -07:00
2015-06-29 11:45:42 -07:00
Events.off(mediaRenderer, 'ended', self.onPlaybackStopped);
2015-07-03 04:51:45 -07:00
var item = self.currentItem;
var mediaSource = self.currentMediaSource;
var state = self.getPlayerStateInternal(mediaRenderer, item, mediaSource);
2015-06-07 13:06:18 -07:00
self.cleanup(mediaRenderer);
2014-08-10 15:13:17 -07:00
clearProgressInterval();
if (item.MediaType == "Video") {
2015-03-20 10:29:18 -07:00
if (self.isFullScreen()) {
self.exitFullScreen();
}
self.resetEnhancements();
}
2015-06-28 07:45:21 -07:00
Events.trigger(self, 'playbackstop', [state]);
};
2015-06-07 13:06:18 -07:00
self.onPlaystateChange = function (mediaRenderer) {
2015-06-07 13:06:18 -07:00
var state = self.getPlayerStateInternal(mediaRenderer, self.currentItem, self.currentMediaSource);
2015-06-28 07:45:21 -07:00
Events.trigger(self, 'playstatechange', [state]);
};
2015-06-28 07:45:21 -07:00
Events.on(window, "beforeunload", function () {
// Try to report playback stopped before the browser closes
2015-06-07 13:06:18 -07:00
if (self.currentItem && self.currentMediaRenderer && currentProgressInterval) {
2015-06-07 13:06:18 -07:00
self.onPlaybackStopped.call(self.currentMediaRenderer);
}
});
2014-05-07 11:38:50 -07:00
function sendProgressUpdate() {
2015-07-09 20:00:03 -07:00
var mediaRenderer = self.currentMediaRenderer;
if (mediaRenderer.enableProgressReporting === false) {
return;
}
var state = self.getPlayerStateInternal(mediaRenderer, self.currentItem, self.currentMediaSource);
2014-06-11 19:38:40 -07:00
2014-05-07 11:38:50 -07:00
var info = {
QueueableMediaTypes: state.NowPlayingItem.MediaType,
ItemId: state.NowPlayingItem.Id,
NowPlayingItem: state.NowPlayingItem
};
2014-04-30 08:07:02 -07:00
2014-05-07 11:38:50 -07:00
info = $.extend(info, state.PlayState);
2015-07-30 07:34:46 -07:00
console.log('repeat mode ' + info.RepeatMode);
2014-05-07 11:38:50 -07:00
ApiClient.reportPlaybackProgress(info);
2014-04-02 14:55:19 -07:00
}
function clearProgressInterval() {
if (currentProgressInterval) {
clearTimeout(currentProgressInterval);
currentProgressInterval = null;
}
2014-04-30 08:07:02 -07:00
}
2015-07-23 06:23:22 -07:00
function canPlayH264() {
2015-08-27 21:19:08 -07:00
var userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf('firefox') != -1) {
if (userAgent.indexOf('windows') != -1) {
return true;
}
2015-07-23 06:23:22 -07:00
return false;
}
return true;
}
2015-06-07 13:06:18 -07:00
self._canPlayWebm = null;
self.canPlayWebm = function () {
2015-06-07 18:23:56 -07:00
if (self._canPlayWebm == null) {
2015-07-23 06:23:22 -07:00
self._canPlayWebm = document.createElement('video').canPlayType('video/webm').replace(/no/, '');
2015-06-07 18:23:56 -07:00
}
2015-06-07 13:06:18 -07:00
return self._canPlayWebm;
2014-08-14 06:24:30 -07:00
};
2014-04-14 20:54:52 -07:00
self.canAutoPlayAudio = function () {
2015-05-28 16:37:43 -07:00
if (AppInfo.isNativeApp) {
2015-05-02 09:34:27 -07:00
return true;
}
2015-05-09 21:29:04 -07:00
if ($.browser.mobile) {
2014-04-14 20:54:52 -07:00
return false;
}
return true;
};
2015-07-26 14:02:23 -07:00
var repeatMode = 'RepeatNone';
self.getRepeatMode = function () {
return repeatMode;
};
self.setRepeatMode = function (mode) {
repeatMode = mode;
};
2015-05-22 12:16:14 -07:00
function onTimeUpdate() {
var currentTicks = self.getCurrentTicks(this);
self.setCurrentTime(currentTicks);
}
2015-07-08 09:10:34 -07:00
function playAudio(item, mediaSource, startPositionTicks, callback) {
2015-03-07 22:37:48 -07:00
2015-06-10 06:37:07 -07:00
requirejs(['audiorenderer'], function () {
2015-06-07 20:16:42 -07:00
playAudioInternal(item, mediaSource, startPositionTicks);
2015-07-08 09:10:34 -07:00
if (callback) {
callback();
}
2015-06-07 20:16:42 -07:00
});
}
function playAudioInternal(item, mediaSource, startPositionTicks) {
2015-09-21 08:43:10 -07:00
self.createStreamInfo('Audio', item, mediaSource, startPositionTicks).done(function (streamInfo) {
2014-04-13 10:27:13 -07:00
2015-09-21 08:43:10 -07:00
var audioUrl = streamInfo.url;
self.startTimeTicksOffset = streamInfo.startTimeTicksOffset;
2015-09-21 08:43:10 -07:00
var initialVolume = self.getSavedVolume();
var mediaRenderer = new AudioRenderer({
poster: self.getPosterUrl(item)
});
2015-09-21 08:43:10 -07:00
Events.on(mediaRenderer, "volumechange.mediaplayerevent", function () {
2015-09-21 08:43:10 -07:00
Logger.log('audio element event: volumechange');
2014-08-10 15:13:17 -07:00
2015-09-21 08:43:10 -07:00
self.onVolumeChanged(this);
2015-09-21 08:43:10 -07:00
});
2015-06-28 07:45:21 -07:00
2015-09-21 08:43:10 -07:00
$(mediaRenderer).one("playing.mediaplayerevent", function () {
2015-09-21 08:43:10 -07:00
Logger.log('audio element event: playing');
2014-08-10 15:13:17 -07:00
2015-09-21 08:43:10 -07:00
// For some reason this is firing at the start, so don't bind until playback has begun
Events.on(this, 'ended', self.onPlaybackStopped);
2015-06-29 11:45:42 -07:00
2015-09-21 08:43:10 -07:00
$(this).one('ended', self.playNextAfterEnded);
2014-08-10 15:13:17 -07:00
2015-09-21 08:43:10 -07:00
self.onPlaybackStart(this, item, mediaSource);
2015-09-21 08:43:10 -07:00
}).on("pause.mediaplayerevent", function () {
2015-09-21 08:43:10 -07:00
Logger.log('audio element event: pause');
2014-08-10 15:13:17 -07:00
2015-09-21 08:43:10 -07:00
self.onPlaystateChange(this);
2015-09-21 08:43:10 -07:00
// In the event timeupdate isn't firing, at least we can update when this happens
self.setCurrentTime(self.getCurrentTicks());
2014-11-13 23:27:10 -07:00
2015-09-21 08:43:10 -07:00
}).on("playing.mediaplayerevent", function () {
2015-09-21 08:43:10 -07:00
Logger.log('audio element event: playing');
2014-08-10 15:13:17 -07:00
2015-09-21 08:43:10 -07:00
self.onPlaystateChange(this);
2015-09-21 08:43:10 -07:00
// In the event timeupdate isn't firing, at least we can update when this happens
self.setCurrentTime(self.getCurrentTicks());
2014-11-13 23:27:10 -07:00
2015-09-21 08:43:10 -07:00
}).on("timeupdate.mediaplayerevent", onTimeUpdate);
2015-06-07 13:06:18 -07:00
2015-09-21 08:43:10 -07:00
self.currentMediaRenderer = mediaRenderer;
self.currentDurationTicks = self.currentMediaSource.RunTimeTicks;
2015-09-09 20:22:52 -07:00
2015-09-21 08:43:10 -07:00
mediaRenderer.init().done(function () {
2015-09-09 20:22:52 -07:00
2015-09-21 08:43:10 -07:00
// Set volume first to avoid an audible change
mediaRenderer.volume(initialVolume);
2015-09-09 20:22:52 -07:00
2015-09-21 08:43:10 -07:00
mediaRenderer.setCurrentSrc(streamInfo, item, mediaSource);
self.streamInfo = streamInfo;
});
2015-09-09 20:22:52 -07:00
});
2015-06-07 20:16:42 -07:00
}
2014-03-22 09:16:43 -07:00
var getItemFields = "MediaSources,Chapters";
2015-05-18 09:40:20 -07:00
self.tryPair = function (target) {
var deferred = $.Deferred();
deferred.resolve();
return deferred.promise();
};
2013-04-10 14:03:28 -07:00
}
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
2015-09-05 21:53:37 -07:00
function onConnectionChange() {
window.MediaPlayer.lastBitrateDetect = 0;
}
2015-05-21 13:53:14 -07:00
Dashboard.ready(function () {
2015-05-19 12:15:40 -07:00
window.MediaController.registerPlayer(window.MediaPlayer);
window.MediaController.setActivePlayer(window.MediaPlayer, window.MediaPlayer.getTargets()[0]);
2015-09-05 21:53:37 -07:00
Events.on(ConnectionManager, 'localusersignedin', onConnectionChange);
Events.on(ConnectionManager, 'localusersignedout', onConnectionChange);
2015-05-19 12:15:40 -07:00
});
2014-03-29 08:40:32 -07:00
2015-06-25 14:50:56 -07:00
})(document, setTimeout, clearTimeout, screen, $, setInterval, window);