f792d24_part_12_SoundCloudPlayer_4.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. function SoundCloudPlayer(ref_id, object_for_player, finish_callback, autoplay)
  2. {
  3. autoplay = typeof autoplay !== 'undefined' ? autoplay : false;
  4. var _autoplay = autoplay;
  5. var _iframe_id = '';
  6. var _ref_id = ref_id;
  7. var _object_for_player = object_for_player;
  8. var _player = null;
  9. var _sounds_count = 0;
  10. var _current_sound_index = 0;
  11. var _finish_callback = finish_callback;
  12. this.play = function()
  13. {
  14. createPlayer(
  15. event_ready,
  16. event_play,
  17. event_finish
  18. );
  19. }
  20. var createPlayer = function(callback_ready, callback_play, callback_finish)
  21. {
  22. _iframe_id = 'soundcloud_player_'+new Date().getTime();
  23. if (!_autoplay)
  24. {
  25. _object_for_player.html('<iframe id="'+_iframe_id+'" src="http://w.soundcloud.com/player/?url='
  26. +_ref_id+'&show_artwork=false&auto_play=true" width="100%" '
  27. +'height="350" scrolling="no" frameborder="no"></iframe>');
  28. }
  29. else
  30. {
  31. // On gère la lecture auto difframment: rappel: si on supprime une iframe l'api SC ne la trouve plus et bug
  32. $('#autoplay').append('<iframe id="'+_iframe_id+'" src="http://w.soundcloud.com/player/?url='
  33. +_ref_id+'&show_artwork=false&auto_play=true" width="100%" '
  34. +'height="350" scrolling="no" frameborder="no"></iframe>');
  35. }
  36. SC.initialize({
  37. client_id: '39946ea18e3d78d64c0ac95a025794e1'
  38. });
  39. SC.get(_ref_id, function(track, error)
  40. {
  41. if (!error)
  42. {
  43. _player = SC.Widget(document.getElementById(_iframe_id));
  44. _player.bind(SC.Widget.Events.READY, callback_ready);
  45. _player.bind(SC.Widget.Events.PLAY, callback_play);
  46. _player.bind(SC.Widget.Events.FINISH, callback_finish);
  47. }
  48. else
  49. {
  50. _finish_callback();
  51. }
  52. });
  53. }
  54. var event_ready = function()
  55. {
  56. _player.getSounds(function(value){
  57. _sounds_count = value.length;
  58. });
  59. }
  60. var event_play = function()
  61. {
  62. _player.getSounds(function(value){
  63. _sounds_count = value.length;
  64. });
  65. _player.getCurrentSoundIndex(function(value){
  66. _current_sound_index = value;
  67. });
  68. }
  69. var event_finish = function()
  70. {
  71. if (_sounds_count == _current_sound_index+1)
  72. {
  73. event_finish_playlist();
  74. }
  75. }
  76. var event_finish_playlist = function()
  77. {
  78. _finish_callback();
  79. }
  80. this.stop = function()
  81. {
  82. }
  83. this.destroy = function(force)
  84. {
  85. if (_player)
  86. {
  87. _player.pause();
  88. }
  89. if (_autoplay || force)
  90. {
  91. $('#'+_iframe_id).hide();
  92. }
  93. }
  94. this.stopAndDestroy = function()
  95. {
  96. //this.stop();
  97. this.destroy(false);
  98. }
  99. this.close = function()
  100. {
  101. this.destroy(true);
  102. }
  103. }