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

283 lines
8.9 KiB
JavaScript
Raw Normal View History

2013-02-20 18:33:05 -07:00
var MediaPlayer = {
testableAudioElement: document.createElement('audio'),
testableVideoElement: document.createElement('video'),
2013-02-20 18:33:05 -07:00
canPlay: function (item) {
if (item.MediaType === "Video") {
var media = MediaPlayer.testableVideoElement;
2013-02-20 18:33:05 -07:00
if (media.canPlayType) {
return media.canPlayType('video/mp4').replace(/no/, '') || media.canPlayType('video/mp2t').replace(/no/, '') || media.canPlayType('video/webm').replace(/no/, '') || media.canPlayType('application/x-mpegURL').replace(/no/, '') || media.canPlayType('video/ogv').replace(/no/, '');
2013-02-20 18:33:05 -07:00
}
return false;
}
if (item.MediaType === "Audio") {
var media = MediaPlayer.testableAudioElement;
2013-02-20 18:33:05 -07:00
if (media.canPlayType) {
return media.canPlayType('audio/mpeg').replace(/no/, '') || media.canPlayType('audio/aac').replace(/no/, '');
}
return false;
}
return false;
},
play: function (items) {
if (MediaPlayer.isPlaying()) {
MediaPlayer.stop();
}
var item = items[0];
var mediaElement;
2013-02-27 10:53:42 -07:00
2013-02-20 18:33:05 -07:00
if (item.MediaType === "Video") {
mediaElement = MediaPlayer.playVideo(items);
}
else if (item.MediaType === "Audio") {
mediaElement = MediaPlayer.playAudio(items);
}
2013-02-27 10:53:42 -07:00
2013-02-20 18:33:05 -07:00
if (!mediaElement) {
return;
}
MediaPlayer.mediaElement = mediaElement;
var nowPlayingBar = $('#nowPlayingBar').show();
if (items.length > 1) {
$('#previousTrackButton', nowPlayingBar)[0].disabled = false;
$('#nextTrackButton', nowPlayingBar)[0].disabled = false;
} else {
$('#previousTrackButton', nowPlayingBar)[0].disabled = true;
$('#nextTrackButton', nowPlayingBar)[0].disabled = true;
}
},
playAudio: function (items, params) {
2013-02-20 18:33:05 -07:00
var item = items[0];
var baseParams = {
audioChannels: 2,
audioBitrate: 128000
};
$.extend(baseParams, params);
2013-02-27 10:53:42 -07:00
var mp3Url = ApiClient.getUrl('Audio/' + item.Id + '/stream.mp3', $.extend({}, baseParams, {
2013-02-20 18:33:05 -07:00
audioCodec: 'mp3'
}));
2013-02-27 10:53:42 -07:00
var aacUrl = ApiClient.getUrl('Audio/' + item.Id + '/stream.aac', $.extend({}, baseParams, {
2013-02-20 18:33:05 -07:00
audioCodec: 'aac'
}));
2013-02-27 10:53:42 -07:00
var webmUrl = ApiClient.getUrl('Audio/' + item.Id + '/stream.webma', $.extend({}, baseParams, {
2013-02-20 18:33:05 -07:00
audioCodec: 'Vorbis'
}));
2013-02-27 10:53:42 -07:00
var oggUrl = ApiClient.getUrl('Audio/' + item.Id + '/stream.oga', $.extend({}, baseParams, {
2013-02-20 18:33:05 -07:00
audioCodec: 'Vorbis'
}));
var html = '';
html += '<audio class="itemAudio" preload="none" controls autoplay>';
html += '<source type="audio/mpeg" src="' + mp3Url + '" />';
html += '<source type="audio/aac" src="' + aacUrl + '" />';
html += '<source type="audio/webm" src="' + webmUrl + '" />';
html += '<source type="audio/ogg" src="' + oggUrl + '" />';
2013-02-20 18:33:05 -07:00
html += '</audio';
var nowPlayingBar = $('#nowPlayingBar').show();
$('#mediaElement', nowPlayingBar).html(html);
return $('audio', nowPlayingBar)[0];
},
playVideo: function (items) {
var item = items[0];
2013-03-12 20:57:54 -07:00
// Account for screen rotation. Use the larger dimension as the width.
2013-02-20 18:33:05 -07:00
var screenWidth = Math.max(screen.height, screen.width);
var screenHeight = Math.min(screen.height, screen.width);
var baseParams = {
audioChannels: 2,
audioBitrate: 128000,
videoBitrate: 500000,
maxWidth: screenWidth,
maxHeight: screenHeight
};
//TODO if you press "stop" button on the nowPlayingBar and restart the same video without refreshing the page
//there is an issue since VideoJS is still loaded.
var html = '<video id="videoWindow" class="itemVideo video-js vjs-default-skin"></video>';
2013-02-20 18:33:05 -07:00
var nowPlayingBar = $('#nowPlayingBar');
2013-02-20 18:33:05 -07:00
$('#mediaElement', nowPlayingBar).html(html).show();
_V_("videoWindow", {'controls': true, 'autoplay': true, 'preload': 'auto'}, function(){
2013-02-20 18:33:05 -07:00
var mp4VideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.mp4', $.extend({}, baseParams, {
videoCodec: 'h264',
audioCodec: 'aac'
}));
2013-02-20 18:33:05 -07:00
var tsVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.ts', $.extend({}, baseParams, {
videoCodec: 'h264',
audioCodec: 'aac'
}));
2013-02-20 18:33:05 -07:00
var webmVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.webm', $.extend({}, baseParams, {
videoCodec: 'vpx',
audioCodec: 'Vorbis'
}));
var hlsVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.m3u8', $.extend({}, baseParams, {
videoCodec: 'h264',
audioCodec: 'aac'
}));
var ogvVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.ogv', $.extend({}, baseParams, {
videoCodec: 'theora',
audioCodec: 'Vorbis'
}));
(this).src([{ type: "video/webm", src: webmVideoUrl },
{ type: "video/mp4", src: mp4VideoUrl },
{ type: "video/mp2t; codecs='h264, aac'", src: tsVideoUrl },
{ type: "application/x-mpegURL", src: hlsVideoUrl },
{ type: "video/ogg", src: ogvVideoUrl }]);
videoJSextension.setup_video( $( '#videoWindow' ), item );
(this).addEvent("loadstart",function(){
$(".vjs-remaining-time-display").hide();
});
(this).addEvent("durationchange",function(){
if ((this).duration() != "Infinity")
$(".vjs-remaining-time-display").show();
});
});
2013-02-20 18:33:05 -07:00
return $('video', nowPlayingBar)[0];
},
stop: function () {
var elem = MediaPlayer.mediaElement;
2013-03-25 17:56:31 -07:00
//check if it's a video using VideoJS
if ($(elem).hasClass("vjs-tech")) {
var player = _V_("videoWindow");
if (player.techName == "html5") {
player.tag.src = "";
player.tech.removeTriggers();
player.load();
}
// player.tech.destroy();
player.destroy();
}else {
elem.pause();
elem.src = "";
}
2013-02-20 18:33:05 -07:00
$(elem).remove();
$('#nowPlayingBar').hide();
MediaPlayer.mediaElement = null;
},
2013-02-27 10:53:42 -07:00
isPlaying: function () {
2013-02-20 18:33:05 -07:00
return MediaPlayer.mediaElement;
}
};
var videoJSextension = {
/*
Add our video quality selector button to the videojs controls. This takes
a mandatory jQuery object of the <video> element we are setting up the
videojs video for.
*/
setup_video : function( $video, item ) {
var vid_id = $video.attr( 'id' ),
available_res = ['high','medium','low'],
default_res,
vjs_sources = [], // This will be an array of arrays of objects, see the video.js api documentation for myPlayer.src()
vjs_source = {},
vjs_chapters = [], // This will be an array of arrays of objects, see the video.js api documentation for myPlayer.src()
vjs_chapter = {};
// Determine this video's default res (it might not have the globally determined default available)
default_res = available_res[0];
// Put together the videojs source arrays for each available resolution
$.each( available_res, function( i, res ) {
vjs_sources[i] = [];
vjs_source = {};
vjs_source.res = res;
vjs_sources[i].push( vjs_source );
});
_V_.ResolutionSelectorButton = _V_.ResolutionSelector.extend({
buttonText: default_res,
availableRes: vjs_sources
});
// Add the resolution selector button.
_V_.merge( _V_.ControlBar.prototype.options.components, { ResolutionSelectorButton : {} } );
//chceck if chapters exist and add chapter selector
if (item.Chapters.length > 0) {
// Put together the videojs source arrays for each available chapter
$.each( item.Chapters, function( i, chapter ) {
vjs_chapters[i] = [];
vjs_chapter = {};
vjs_chapter.Name = chapter.Name + " (" + ticks_to_human(chapter.StartPositionTicks) + ")";
vjs_chapter.StartPositionTicks = chapter.StartPositionTicks;
vjs_chapters[i].push( vjs_chapter );
});
_V_.ChapterSelectorButton = _V_.ChapterSelector.extend({
buttonText: '',
Chapters: vjs_chapters
});
// Add the chapter selector button.
_V_.merge( _V_.ControlBar.prototype.options.components, { ChapterSelectorButton : {} } );
}
}
2013-02-20 18:33:05 -07:00
};