jellyfin-web/dashboard-ui/cordova/ios/vlcplayer.js

263 lines
7.3 KiB
JavaScript
Raw Normal View History

2015-09-09 20:22:52 -07:00
(function () {
function vlcRenderer(options) {
var self = this;
2015-09-10 11:28:22 -07:00
// Need to use this to fire events because the iOS vlc callbacks always go to the first instance
window.AudioRenderer.Current = self;
2015-09-09 20:22:52 -07:00
self.enableProgressReporting = options.type == 'audio';
function onEnded() {
2015-09-10 11:28:22 -07:00
Events.trigger(window.AudioRenderer.Current, 'ended');
2015-09-09 20:22:52 -07:00
}
function onTimeUpdate() {
2015-09-10 11:28:22 -07:00
Events.trigger(window.AudioRenderer.Current, 'timeupdate');
2015-09-09 20:22:52 -07:00
}
function onVolumeChange() {
2015-09-10 11:28:22 -07:00
Events.trigger(window.AudioRenderer.Current, 'volumechange');
2015-09-09 20:22:52 -07:00
}
function onPlaying() {
2015-09-10 11:28:22 -07:00
Events.trigger(window.AudioRenderer.Current, 'playing');
2015-09-09 20:22:52 -07:00
}
function onPlay() {
2015-09-10 11:28:22 -07:00
Events.trigger(window.AudioRenderer.Current, 'play');
2015-09-09 20:22:52 -07:00
}
function onPause() {
2015-09-10 11:28:22 -07:00
Events.trigger(window.AudioRenderer.Current, 'pause');
2015-09-09 20:22:52 -07:00
}
function onClick() {
2015-09-10 11:28:22 -07:00
Events.trigger(window.AudioRenderer.Current, 'click');
2015-09-09 20:22:52 -07:00
}
function onDblClick() {
2015-09-10 11:28:22 -07:00
Events.trigger(window.AudioRenderer.Current, 'dblclick');
2015-09-09 20:22:52 -07:00
}
function onError() {
var errorCode = this.error ? this.error.code : '';
Logger.log('Media element error code: ' + errorCode);
2015-09-10 11:28:22 -07:00
Events.trigger(window.AudioRenderer.Current, 'error');
2015-09-09 20:22:52 -07:00
}
2015-09-10 11:28:22 -07:00
self.playerState = {};
2015-09-09 20:22:52 -07:00
self.currentTime = function (val) {
if (val != null) {
window.audioplayer.seekto(function () {
Logger.log('set currentTime succeeded!');
}, function () {
Logger.log('set currentTime failed!');
}, val / 1000);
return;
}
2015-09-10 11:28:22 -07:00
return self.playerState.currentTime;
2015-09-09 20:22:52 -07:00
};
self.duration = function (val) {
2015-09-19 19:06:56 -07:00
// TODO
// This value doesn't seem to be getting reported properly
// Right now it's only used to determine if the player can seek, so for now we can mock it
return 1;
2015-09-09 20:22:52 -07:00
};
self.stop = function () {
window.audioplayer.stop(function (result) {
Logger.log('Stop succeeded!');
reportEvent('playbackstop', result);
}, function () {
Logger.log('Stop failed!');
});
};
self.pause = function () {
window.audioplayer.pause(function (result) {
Logger.log('Pause succeeded!');
reportEvent('sepaused', result);
}, function () {
Logger.log('Pause failed!');
});
};
self.unpause = function () {
window.audioplayer.pause(function (result) {
Logger.log('Unpause succeeded!');
reportEvent('playing', result);
}, function () {
Logger.log('Unpause failed!');
});
};
self.volume = function (val) {
2015-09-10 11:28:22 -07:00
if (val != null) {
// TODO
return;
2015-09-09 20:22:52 -07:00
}
2015-09-10 11:28:22 -07:00
return self.playerState.volume;
2015-09-09 20:22:52 -07:00
};
2015-09-15 20:55:26 -07:00
self.setCurrentSrc = function (streamInfo, item, mediaSource, tracks) {
2015-09-09 20:22:52 -07:00
2015-09-15 20:55:26 -07:00
if (!streamInfo) {
2015-09-09 20:22:52 -07:00
return;
}
2015-09-15 20:55:26 -07:00
var val = streamInfo.url;
2015-09-09 20:22:52 -07:00
var tIndex = val.indexOf('#t=');
var startPosMs = 0;
if (tIndex != -1) {
startPosMs = val.substring(tIndex + 3);
startPosMs = parseFloat(startPosMs) * 1000;
val = val.split('#')[0];
}
if (options.type == 'audio') {
//AndroidVlcPlayer.playAudioVlc(val, JSON.stringify(item), JSON.stringify(mediaSource), options.poster);
var artist = item.ArtistItems && item.ArtistItems.length ? item.ArtistItems[0].Name : null;
2015-09-17 09:04:04 -07:00
var metadata = {};
if (item.Name) {
metadata.title = item.Name;
}
if (artist) {
metadata.artist = artist;
}
if (item.Overview) {
metadata.description = item.Overview;
}
if (options.poster) {
metadata.image = {
url: options.poster
};
metadata.imageThumbnail = {
url: options.poster
};
}
2015-09-09 20:22:52 -07:00
window.audioplayer.playstream(successHandler, function () {
Logger.log('playstream failed!');
//onError();
},
{
ios: val
},
// metadata used for iOS lock screen, Android 'Now Playing' notification
2015-09-17 09:04:04 -07:00
metadata
2015-09-09 20:22:52 -07:00
);
} else {
}
2015-09-10 11:28:22 -07:00
AudioRenderer.Current.playerState.currentSrc = val;
2015-09-09 20:22:52 -07:00
reportEvent('playing', {});
};
self.currentSrc = function () {
2015-09-10 11:28:22 -07:00
return self.playerState.currentSrc;
2015-09-09 20:22:52 -07:00
};
self.paused = function () {
2015-09-10 11:28:22 -07:00
return self.playerState.paused;
2015-09-09 20:22:52 -07:00
};
self.cleanup = function (destroyRenderer) {
2015-09-10 11:28:22 -07:00
self.playerState = {};
2015-09-09 20:22:52 -07:00
};
function reportEvent(eventName, result) {
var duration = result.duration || 0;
var position = result.progress || 0;
Logger.log('eventName: ' + eventName + '. position: ' + position);
2015-09-10 11:28:22 -07:00
var state = AudioRenderer.Current.playerState;
2015-09-09 20:22:52 -07:00
state.duration = duration;
state.currentTime = position;
2015-09-10 11:28:22 -07:00
state.paused = result.state == 3 || eventName == 'paused';
2015-09-09 20:22:52 -07:00
state.volume = 0;
if (eventName == 'playbackstop') {
onEnded();
}
else if (eventName == 'positionchange') {
onTimeUpdate();
}
else if (eventName == 'paused') {
onPause();
}
else if (eventName == 'unpaused') {
onPlaying();
}
else if (eventName == 'playing') {
onPlaying();
}
}
function successHandler(result) {
if (!result) {
return;
}
if (result.state == 4 || result.state == 6) {
reportEvent('playbackstop', result);
}
else {
var eventName = 'positionchange';
reportEvent(eventName, result);
}
}
2015-09-10 11:28:22 -07:00
function errorHandler() {
onError();
}
2015-09-09 20:22:52 -07:00
self.init = function () {
var deferred = DeferredBuilder.Deferred();
2015-09-10 11:28:22 -07:00
window.audioplayer.configure(successHandler, errorHandler);
setTimeout(function () {
2015-09-09 20:22:52 -07:00
deferred.resolve();
2015-09-10 11:28:22 -07:00
}, 500);
2015-09-09 20:22:52 -07:00
return deferred.promise();
};
}
window.AudioRenderer = function (options) {
options = options || {};
options.type = 'audio';
return new vlcRenderer(options);
};
})();