YoutubePlayer.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. this.play = function()
  8. {
  9. create_player();
  10. }
  11. var create_player = function()
  12. {
  13. var div_for_iframe = $('<div>').attr('id', _object_for_player.attr('id')+'_iframe_'+ref_id);
  14. _object_for_player.append(div_for_iframe);
  15. _yt_player = new YT.Player(_object_for_player.attr('id')+'_iframe_'+ref_id, {
  16. height : config_player_youtube_height,
  17. width : '100%',
  18. videoId : _ref_id,
  19. events : {
  20. 'onReady': onPlayerReady,
  21. 'onStateChange': onPlayerStateChange,
  22. 'onError': onError
  23. }
  24. });
  25. }
  26. var onPlayerReady = function(event)
  27. {
  28. _yt_player.playVideo();
  29. }
  30. var onError = function(event)
  31. {
  32. _finish_callback();
  33. }
  34. var onPlayerStateChange = function(event)
  35. {
  36. if (event.data == YT.PlayerState.PLAYING)
  37. {
  38. }
  39. if (event.data == YT.PlayerState.ENDED)
  40. {
  41. _finish_callback();
  42. }
  43. if (event.data == YT.PlayerState.PAUSED)
  44. {
  45. }
  46. if (event.data == YT.PlayerState.BUFFERING)
  47. {
  48. }
  49. if (event.data == YT.PlayerState.CUED)
  50. {
  51. }
  52. }
  53. this.pause = function()
  54. {
  55. if (_yt_player)
  56. {
  57. if(typeof(_yt_player.pauseVideo)!=='undefined')
  58. {
  59. _yt_player.pauseVideo();
  60. }
  61. }
  62. }
  63. this.stop = function()
  64. {
  65. if (_yt_player)
  66. {
  67. if(typeof(_yt_player.stopVideo)!=='undefined')
  68. {
  69. _yt_player.stopVideo();
  70. }
  71. }
  72. }
  73. this.destroy = function()
  74. {
  75. _object_for_player.html('');
  76. }
  77. this.stopAndDestroy = function()
  78. {
  79. this.stop();
  80. this.destroy();
  81. }
  82. this.close = function()
  83. {
  84. this.stopAndDestroy();
  85. }
  86. }