YoutubePlayer.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. function YoutubePlayer(ref_id, object_for_player, finish_callback)
  2. {
  3. var _ref_id = ref_id;
  4. var _object_for_player = object_for_player;
  5. var _yt_player;
  6. var _finish_callback = finish_callback;
  7. var that = this;
  8. this.play = function(play_callback)
  9. {
  10. create_player(play_callback);
  11. }
  12. var create_player = function(play_callback)
  13. {
  14. var div_for_iframe = $('<div>').attr('id', _object_for_player.attr('id')+'_iframe_'+ref_id);
  15. _object_for_player.append(div_for_iframe);
  16. var onPlayerReady_callback = function(){
  17. onPlayerReady();
  18. play_callback(that);
  19. };
  20. _yt_player = new YT.Player(_object_for_player.attr('id')+'_iframe_'+ref_id, {
  21. height : config_player_youtube_height,
  22. width : '100%',
  23. videoId : _ref_id,
  24. events : {
  25. 'onReady': onPlayerReady_callback,
  26. 'onStateChange': onPlayerStateChange,
  27. 'onError': onError
  28. }
  29. });
  30. }
  31. var onPlayerReady = function(event)
  32. {
  33. _yt_player.playVideo();
  34. }
  35. var onError = function(event)
  36. {
  37. _finish_callback();
  38. }
  39. var onPlayerStateChange = function(event)
  40. {
  41. if (event.data == YT.PlayerState.PLAYING)
  42. {
  43. }
  44. if (event.data == YT.PlayerState.ENDED)
  45. {
  46. _finish_callback();
  47. }
  48. if (event.data == YT.PlayerState.PAUSED)
  49. {
  50. }
  51. if (event.data == YT.PlayerState.BUFFERING)
  52. {
  53. }
  54. if (event.data == YT.PlayerState.CUED)
  55. {
  56. }
  57. }
  58. this.pause = function()
  59. {
  60. if (_yt_player)
  61. {
  62. if(typeof(_yt_player.pauseVideo)!=='undefined')
  63. {
  64. _yt_player.pauseVideo();
  65. }
  66. }
  67. }
  68. this.stop = function()
  69. {
  70. if (_yt_player)
  71. {
  72. if(typeof(_yt_player.stopVideo)!=='undefined')
  73. {
  74. try
  75. {
  76. _yt_player.stopVideo();
  77. }
  78. catch (e)
  79. {
  80. }
  81. }
  82. }
  83. }
  84. this.destroy = function()
  85. {
  86. _object_for_player.html('');
  87. }
  88. this.stopAndDestroy = function()
  89. {
  90. this.stop();
  91. this.destroy();
  92. this.disableFullScreen();
  93. }
  94. this.close = function()
  95. {
  96. this.stopAndDestroy();
  97. }
  98. this.enableFullScreen = function() {
  99. var iframe = $('#'+_object_for_player.attr('id')+'_iframe_'+_ref_id);
  100. set_full_screen_on(iframe);
  101. }
  102. this.disableFullScreen = function() {
  103. $('#close_full_screen').remove();
  104. }
  105. }