SoundCloudPlayer.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. $('#autoplay_player_soundcloud').append('<iframe id="'+_iframe_id+'" src="http://w.soundcloud.com/player/?url='
  32. +_ref_id+'&show_artwork=false&auto_play=true" width="100%" '
  33. +'height="350" scrolling="no" frameborder="no"></iframe>');
  34. }
  35. SC.initialize({
  36. client_id: '39946ea18e3d78d64c0ac95a025794e1'
  37. });
  38. SC.get(_ref_id, function(track, error)
  39. {
  40. if (!error)
  41. {
  42. _player = SC.Widget(document.getElementById(_iframe_id));
  43. _player.bind(SC.Widget.Events.READY, callback_ready);
  44. _player.bind(SC.Widget.Events.PLAY, callback_play);
  45. _player.bind(SC.Widget.Events.FINISH, callback_finish);
  46. }
  47. else
  48. {
  49. _finish_callback();
  50. }
  51. });
  52. }
  53. var event_ready = function()
  54. {
  55. _player.getSounds(function(value){
  56. _sounds_count = value.length;
  57. });
  58. }
  59. var event_play = function()
  60. {
  61. _player.getSounds(function(value){
  62. _sounds_count = value.length;
  63. });
  64. _player.getCurrentSoundIndex(function(value){
  65. _current_sound_index = value;
  66. });
  67. }
  68. var event_finish = function()
  69. {
  70. if (_sounds_count == _current_sound_index+1)
  71. {
  72. event_finish_playlist();
  73. }
  74. }
  75. var event_finish_playlist = function()
  76. {
  77. _finish_callback();
  78. }
  79. this.stop = function()
  80. {
  81. }
  82. this.destroy = function(force)
  83. {
  84. if (_player && !_autoplay)
  85. {
  86. _object_for_player.html('');
  87. }
  88. if (_autoplay || force)
  89. {
  90. $('#autoplay_player_soundcloud').html('');
  91. }
  92. }
  93. this.stopAndDestroy = function()
  94. {
  95. //this.stop();
  96. this.destroy(false);
  97. }
  98. this.close = function()
  99. {
  100. this.destroy(true);
  101. }
  102. }