pa-sc-player.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. * SoundCloud Custom Player jQuery Plugin
  3. * Author: Matas Petrikas, matas@soundcloud.com
  4. * Copyright (c) 2009 SoundCloud Ltd.
  5. * Licensed under the MIT license:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. *
  8. * Usage:
  9. * <a href="http://soundcloud.com/matas/hobnotropic" class="sc-player">My new dub track</a>
  10. * The link will be automatically replaced by the HTML based player
  11. */
  12. (function($) {
  13. // Convert milliseconds into Hours (h), Minutes (m), and Seconds (s)
  14. var timecode = function(ms) {
  15. var hms = function(ms) {
  16. return {
  17. h: Math.floor(ms/(60*60*1000)),
  18. m: Math.floor((ms/60000) % 60),
  19. s: Math.floor((ms/1000) % 60)
  20. };
  21. }(ms),
  22. tc = []; // Timecode array to be joined with '.'
  23. if (hms.h > 0) {
  24. tc.push(hms.h);
  25. }
  26. tc.push((hms.m < 10 && hms.h > 0 ? "0" + hms.m : hms.m));
  27. tc.push((hms.s < 10 ? "0" + hms.s : hms.s));
  28. return tc.join(':');
  29. };
  30. // shuffle the array
  31. var shuffle = function(arr) {
  32. arr.sort(function() { return 1 - Math.floor(Math.random() * 3); } );
  33. return arr;
  34. };
  35. var debug = true,
  36. useSandBox = false,
  37. $doc = $(document),
  38. log = function(args) {
  39. try {
  40. if(debug && window.console && window.console.log){
  41. window.console.log.apply(window.console, arguments);
  42. }
  43. } catch (e) {
  44. // no console available
  45. }
  46. },
  47. domain = useSandBox ? 'sandbox-soundcloud.com' : 'soundcloud.com',
  48. secureDocument = (document.location.protocol === 'https:'),
  49. // convert a SoundCloud resource URL to an API URL
  50. scApiUrl = function(url, apiKey) {
  51. // var resolver = ( secureDocument || (/^https/i).test(url) ? 'https' : 'http') + '://api.' + domain + '/resolve?url=',
  52. // params = 'format=json&consumer_key=' + apiKey +'&callback=?';
  53. var resolver = ( secureDocument || (/^https/i).test(url) ? 'https' : 'http') + '://api.' + domain + '/resolve?url=',
  54. params = 'format=json&callback=?';
  55. // debugger;
  56. // force the secure url in the secure environment
  57. if( secureDocument ) {
  58. url = url.replace(/^http:/, 'https:');
  59. }
  60. // check if it's already a resolved api url
  61. if ( (/api\./).test(url) ) {
  62. return url + '?' + params;
  63. } else {
  64. return resolver + url + '&' + params;
  65. }
  66. };
  67. // TODO Expose the audio engine, so it can be unit-tested
  68. var audioEngine = function() {
  69. var html5AudioAvailable = function() {
  70. var state = false;
  71. try{
  72. var a = new Audio();
  73. state = a.canPlayType && (/maybe|probably/).test(a.canPlayType('audio/mpeg'));
  74. // uncomment the following line, if you want to enable the html5 audio only on mobile devices
  75. // state = state && (/iPad|iphone|mobile|pre\//i).test(navigator.userAgent);
  76. }catch(e){
  77. // there's no audio support here sadly
  78. }
  79. return state;
  80. }(),
  81. callbacks = {
  82. onReady: function() {
  83. $doc.trigger('paScPlayer:onAudioReady');
  84. },
  85. onPlay: function() {
  86. $doc.trigger('paScPlayer:onMediaPlay');
  87. },
  88. onPause: function() {
  89. $doc.trigger('paScPlayer:onMediaPause');
  90. },
  91. onEnd: function() {
  92. $doc.trigger('paScPlayer:onMediaEnd');
  93. },
  94. onBuffer: function(percent) {
  95. $doc.trigger({type: 'paScPlayer:onMediaBuffering', percent: percent});
  96. }
  97. };
  98. var html5Driver = function() {
  99. var player = new Audio(),
  100. onTimeUpdate = function(event){
  101. var obj = event.target,
  102. buffer = ((obj.buffered.length && obj.buffered.end(0)) / obj.duration) * 100;
  103. // ipad has no progress events implemented yet
  104. callbacks.onBuffer(buffer);
  105. // anounce if it's finished for the clients without 'ended' events implementation
  106. if (obj.currentTime === obj.duration) { callbacks.onEnd(); }
  107. },
  108. onProgress = function(event) {
  109. var obj = event.target,
  110. buffer = ((obj.buffered.length && obj.buffered.end(0)) / obj.duration) * 100;
  111. callbacks.onBuffer(buffer);
  112. };
  113. $('<div class="pa-sc-player-engine-container"></div>').appendTo(document.body).append(player);
  114. // prepare the listeners
  115. player.addEventListener('play', callbacks.onPlay, false);
  116. player.addEventListener('pause', callbacks.onPause, false);
  117. // handled in the onTimeUpdate for now untill all the browsers support 'ended' event
  118. // player.addEventListener('ended', callbacks.onEnd, false);
  119. player.addEventListener('timeupdate', onTimeUpdate, false);
  120. player.addEventListener('progress', onProgress, false);
  121. return {
  122. load: function(track, apiKey) {
  123. player.pause();
  124. player.src = track.stream_url + (/\?/.test(track.stream_url) ? '&' : '?') + 'consumer_key=' + apiKey;
  125. player.load();
  126. player.play();
  127. },
  128. play: function() {
  129. player.play();
  130. },
  131. pause: function() {
  132. player.pause();
  133. },
  134. stop: function(){
  135. if (player.currentTime) {
  136. player.currentTime = 0;
  137. player.pause();
  138. }
  139. },
  140. seek: function(relative){
  141. player.currentTime = player.duration * relative;
  142. player.play();
  143. },
  144. getDuration: function() {
  145. return player.duration * 1000;
  146. },
  147. getPosition: function() {
  148. return player.currentTime * 1000;
  149. },
  150. setVolume: function(val) {
  151. player.volume = val / 100;
  152. }
  153. };
  154. };
  155. var flashDriver = function() {
  156. var engineId = 'paScPlayerEngine',
  157. player,
  158. flashHtml = function(url) {
  159. var swf = (secureDocument ? 'https' : 'http') + '://player.' + domain +'/player.swf?url=' + url +'&amp;enable_api=true&amp;player_type=engine&amp;object_id=' + engineId;
  160. if ($.browser.msie) {
  161. return '<object height="100%" width="100%" id="' + engineId + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" data="' + swf + '">'+
  162. '<param name="movie" value="' + swf + '" />'+
  163. '<param name="allowscriptaccess" value="always" />'+
  164. '</object>';
  165. } else {
  166. return '<object height="100%" width="100%" id="' + engineId + '">'+
  167. '<embed allowscriptaccess="always" height="100%" width="100%" src="' + swf + '" type="application/x-shockwave-flash" name="' + engineId + '" />'+
  168. '</object>';
  169. }
  170. };
  171. // listen to audio engine events
  172. // when the loaded track is ready to play
  173. soundcloud.addEventListener('onPlayerReady', function(flashId, data) {
  174. player = soundcloud.getPlayer(engineId);
  175. callbacks.onReady();
  176. });
  177. // when the loaded track finished playing
  178. soundcloud.addEventListener('onMediaEnd', callbacks.onEnd);
  179. // when the loaded track is still buffering
  180. soundcloud.addEventListener('onMediaBuffering', function(flashId, data) {
  181. callbacks.onBuffer(data.percent);
  182. });
  183. // when the loaded track started to play
  184. soundcloud.addEventListener('onMediaPlay', callbacks.onPlay);
  185. // when the loaded track is was paused
  186. soundcloud.addEventListener('onMediaPause', callbacks.onPause);
  187. return {
  188. load: function(track) {
  189. var url = track.uri;
  190. if(player){
  191. player.api_load(url);
  192. }else{
  193. // create a container for the flash engine (IE needs this to operate properly)
  194. $('<div class="pa-sc-player-engine-container"></div>').appendTo(document.body).html(flashHtml(url));
  195. }
  196. },
  197. play: function() {
  198. player && player.api_play();
  199. },
  200. pause: function() {
  201. player && player.api_pause();
  202. },
  203. stop: function(){
  204. player && player.api_stop();
  205. },
  206. seek: function(relative){
  207. player && player.api_seekTo((player.api_getTrackDuration() * relative));
  208. },
  209. getDuration: function() {
  210. return player && player.api_getTrackDuration && player.api_getTrackDuration() * 1000;
  211. },
  212. getPosition: function() {
  213. return player && player.api_getTrackPosition && player.api_getTrackPosition() * 1000;
  214. },
  215. setVolume: function(val) {
  216. if(player && player.api_setVolume){
  217. player.api_setVolume(val);
  218. }
  219. }
  220. };
  221. };
  222. return html5AudioAvailable? html5Driver() : flashDriver();
  223. }();
  224. var apiKey,
  225. didAutoPlay = false,
  226. players = [],
  227. updates = {},
  228. currentUrl,
  229. loadTracksData = function($player, links, key) {
  230. var index = 0,
  231. playerObj = {node: $player, tracks: []},
  232. loadUrl = function(link) {
  233. var apiUrl = scApiUrl(link.url, apiKey);
  234. // $.getJSON(apiUrl, function(data) {
  235. // // log('data loaded', link.url, data);
  236. // index += 1;
  237. // // added by Bach for MJ
  238. // data.href = link.href;
  239. // if(data.tracks){
  240. // // log('data.tracks', data.tracks);
  241. // playerObj.tracks = playerObj.tracks.concat(data.tracks);
  242. // }else if(data.duration){
  243. // // a secret link fix, till the SC API returns permalink with secret on secret response
  244. // data.permalink_url = link.url;
  245. // // if track, add to player
  246. // playerObj.tracks.push(data);
  247. // }else if(data.creator){
  248. // // it's a group!
  249. // links.push({url:data.uri + '/tracks'});
  250. // }else if(data.username){
  251. // // if user, get his tracks or favorites
  252. // if(/favorites/.test(link.url)){
  253. // links.push({url:data.uri + '/favorites'});
  254. // }else{
  255. // links.push({url:data.uri + '/tracks'});
  256. // }
  257. // }else if($.isArray(data)){
  258. // playerObj.tracks = playerObj.tracks.concat(data);
  259. // }
  260. // if(links[index]){
  261. // // if there are more track to load, get them from the api
  262. // loadUrl(links[index]);
  263. // }else{
  264. // // if loading finishes, anounce it to the GUI
  265. // playerObj.node.trigger({type:'onTrackDataLoaded', playerObj: playerObj, url: apiUrl});
  266. // }
  267. // });
  268. // https://developers.soundcloud.com/docs/api/guide#authentication
  269. // # obtain the access token
  270. // $ curl -X POST "https://api.soundcloud.com/oauth2/token" \
  271. // -H "accept: application/json; charset=utf-8" \
  272. // -H "Content-Type: application/x-www-form-urlencoded" \
  273. // --data-urlencode "grant_type=client_credentials" \
  274. // --data-urlencode "client_id=YOUR_CLIENT_ID" \
  275. // --data-urlencode "client_secret=YOUR_CLIENT_SECRET"
  276. $.ajax({
  277. method: 'POST',
  278. url: 'https://api.soundcloud.com/oauth2/token',
  279. data: {
  280. 'grant_type' : "client_credentials",
  281. 'client_id' : "965bd4363fdd909723749b003be67125",
  282. 'client_secret': "bb68647335a47f104a86dcddf4e70fa8"
  283. },
  284. headers: {
  285. "accept" : "application/json; charset=utf-8",
  286. "Content-Type" : "application/x-www-form-urlencoded",
  287. "Authorization": "OAuth 965bd4363fdd909723749b003be67125"
  288. }
  289. });
  290. // $.ajax({
  291. // "dataType": "json",
  292. // "url": apiUrl,
  293. // "data": {},
  294. // "headers": {
  295. // "Authorization": "OAuth " + apiKey
  296. // },
  297. // "success": function(data) {
  298. // // log('data loaded', link.url, data);
  299. // index += 1;
  300. // // added by Bach for MJ
  301. // data.href = link.href;
  302. // if(data.tracks){
  303. // // log('data.tracks', data.tracks);
  304. // playerObj.tracks = playerObj.tracks.concat(data.tracks);
  305. // }else if(data.duration){
  306. // // a secret link fix, till the SC API returns permalink with secret on secret response
  307. // data.permalink_url = link.url;
  308. // // if track, add to player
  309. // playerObj.tracks.push(data);
  310. // }else if(data.creator){
  311. // // it's a group!
  312. // links.push({url:data.uri + '/tracks'});
  313. // }else if(data.username){
  314. // // if user, get his tracks or favorites
  315. // if(/favorites/.test(link.url)){
  316. // links.push({url:data.uri + '/favorites'});
  317. // }else{
  318. // links.push({url:data.uri + '/tracks'});
  319. // }
  320. // }else if($.isArray(data)){
  321. // playerObj.tracks = playerObj.tracks.concat(data);
  322. // }
  323. // if(links[index]){
  324. // // if there are more track to load, get them from the api
  325. // loadUrl(links[index]);
  326. // }else{
  327. // // if loading finishes, anounce it to the GUI
  328. // playerObj.node.trigger({type:'onTrackDataLoaded', playerObj: playerObj, url: apiUrl});
  329. // }
  330. // },
  331. // "error": function(errorThrown) {
  332. // console.error(JSON.stringify(errorThrown.error()));
  333. // }
  334. // });
  335. };
  336. // update current API key
  337. apiKey = key;
  338. // update the players queue
  339. players.push(playerObj);
  340. // load first tracks
  341. loadUrl(links[index]);
  342. },
  343. artworkImage = function(track, usePlaceholder) {
  344. if(usePlaceholder){
  345. return '<div class="sc-loading-artwork">Loading Artwork</div>';
  346. }else if (track.artwork_url) {
  347. return '<img src="' + track.artwork_url.replace('-large', '-t300x300') + '"/>';
  348. }else{
  349. return '<div class="sc-no-artwork">No Artwork</div>';
  350. }
  351. },
  352. updateTrackInfo = function($player, track) {
  353. // update the current track info in the player
  354. // log('updateTrackInfo', track);
  355. // $('.sc-info', $player).each(function(index) {
  356. // $('h3', this).html('<a href="' + track.permalink_url +'">' + track.title + '</a>');
  357. // $('h4', this).html('by <a href="' + track.user.permalink_url +'">' + track.user.username + '</a>');
  358. // $('p', this).html(track.description || 'no Description');
  359. // });
  360. // update the artwork
  361. // $('.sc-artwork-list li', $player).each(function(index) {
  362. // var $item = $(this),
  363. // itemTrack = $item.data('sc-track');
  364. //
  365. // if (itemTrack === track) {
  366. // // show track artwork
  367. // $item
  368. // .addClass('active')
  369. // .find('.sc-loading-artwork')
  370. // .each(function(index) {
  371. // // if the image isn't loaded yet, do it now
  372. // $(this).removeClass('sc-loading-artwork').html(artworkImage(track, false));
  373. // });
  374. // }else{
  375. // // reset other artworks
  376. // $item.removeClass('active');
  377. // }
  378. // });
  379. // update the track duration in the progress bar
  380. $('.sc-duration', $player).html(timecode(track.duration));
  381. // put the waveform into the progress bar
  382. $('.sc-waveform-container', $player).html('<img src="' + track.waveform_url +'" />');
  383. $player.trigger('onPlayerTrackSwitch.paScPlayer', [track]);
  384. },
  385. play = function(track) {
  386. var url = track.permalink_url;
  387. if(currentUrl === url){
  388. // log('will play');
  389. audioEngine.play();
  390. }else{
  391. currentUrl = url;
  392. // log('will load', url);
  393. audioEngine.load(track, apiKey);
  394. }
  395. },
  396. getPlayerData = function(node) {
  397. return players[$(node).data('pa-sc-player').id];
  398. },
  399. updatePlayStatus = function(player, status) {
  400. if(status){
  401. // reset all other players playing status
  402. $('div.pa-sc-player.playing').removeClass('playing');
  403. }
  404. $(player)
  405. .toggleClass('playing', status)
  406. .trigger((status ? 'onPlayerPlay' : 'onPlayerPause'));
  407. },
  408. onPlay = function(player, id) {
  409. var track = getPlayerData(player).tracks[id || 0];
  410. updateTrackInfo(player, track);
  411. // cache the references to most updated DOM nodes in the progress bar
  412. updates = {
  413. $buffer: $('.sc-buffer', player),
  414. $played: $('.sc-played', player),
  415. position: $('.sc-position', player)[0]
  416. };
  417. updatePlayStatus(player, true);
  418. play(track);
  419. },
  420. onPause = function(player) {
  421. updatePlayStatus(player, false);
  422. audioEngine.pause();
  423. },
  424. onFinish = function() {
  425. var $player = updates.$played.closest('.pa-sc-player'),
  426. $nextItem;
  427. // update the scrubber width
  428. updates.$played.css('width', '0%');
  429. // show the position in the track position counter
  430. updates.position.innerHTML = timecode(0);
  431. // reset the player state
  432. updatePlayStatus($player, false);
  433. // stop the audio
  434. audioEngine.stop();
  435. $player.trigger('onPlayerTrackFinish');
  436. },
  437. onSeek = function(player, relative) {
  438. audioEngine.seek(relative);
  439. $(player).trigger('onPlayerSeek');
  440. },
  441. onSkip = function(player) {
  442. var $player = $(player);
  443. // continue playing through all players
  444. log('track finished get the next one');
  445. $nextItem = $('.sc-trackslist li.active', $player).next('li');
  446. // try to find the next track in other player
  447. if(!$nextItem.length){
  448. $nextItem = $player.nextAll('div.pa-sc-player:first').find('.sc-trackslist li.active');
  449. }
  450. $nextItem.click();
  451. },
  452. soundVolume = function() {
  453. var vol = 80,
  454. cooks = document.cookie.split(';'),
  455. volRx = new RegExp('paScPlayer_volume=(\\d+)');
  456. for(var i in cooks){
  457. if(volRx.test(cooks[i])){
  458. vol = parseInt(cooks[i].match(volRx)[1], 10);
  459. break;
  460. }
  461. }
  462. return vol;
  463. }(),
  464. onVolume = function(volume) {
  465. var vol = Math.floor(volume);
  466. // save the volume in the cookie
  467. var date = new Date();
  468. date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
  469. soundVolume = vol;
  470. document.cookie = ['paScPlayer_volume=', vol, '; expires=', date.toUTCString(), '; path="/"'].join('');
  471. // update the volume in the engine
  472. audioEngine.setVolume(soundVolume);
  473. },
  474. positionPoll;
  475. // listen to audio engine events
  476. $doc
  477. .bind('paScPlayer:onAudioReady', function(event) {
  478. log('onPlayerReady: audio engine is ready');
  479. audioEngine.play();
  480. // set initial volume
  481. onVolume(soundVolume);
  482. })
  483. // when the loaded track started to play
  484. .bind('paScPlayer:onMediaPlay', function(event) {
  485. clearInterval(positionPoll);
  486. positionPoll = setInterval(function() {
  487. var duration = audioEngine.getDuration(),
  488. position = audioEngine.getPosition(),
  489. relative = (position / duration);
  490. // update the scrubber width
  491. updates.$played.css('width', (100 * relative) + '%');
  492. // show the position in the track position counter
  493. updates.position.innerHTML = timecode(position);
  494. // announce the track position to the DOM
  495. $doc.trigger({
  496. type: 'onMediaTimeUpdate.paScPlayer',
  497. duration: duration,
  498. position: position,
  499. relative: relative
  500. });
  501. }, 500);
  502. })
  503. // when the loaded track is was paused
  504. .bind('paScPlayer:onMediaPause', function(event) {
  505. clearInterval(positionPoll);
  506. positionPoll = null;
  507. })
  508. // change the volume
  509. // .bind('paScPlayer:onVolumeChange', function(event) {
  510. // onVolume(event.volume);
  511. // })
  512. .bind('paScPlayer:onMediaEnd', function(event) {
  513. onFinish();
  514. })
  515. .bind('paScPlayer:onMediaBuffering', function(event) {
  516. updates.$buffer.css('width', event.percent + '%');
  517. });
  518. // Generate custom skinnable HTML/CSS/JavaScript based SoundCloud players from links to SoundCloud resources
  519. $.paScPlayer = function(options, node) {
  520. var opts = $.extend({}, $.paScPlayer.defaults, options),
  521. playerId = players.length,
  522. $source = node && $(node),
  523. sourceClasses = $source[0].className.replace('pa-sc-player', ''),
  524. links =
  525. opts.links
  526. || $.map($('a', $source)
  527. .add($source.filter('a')), function(val) {
  528. //log('val', val);
  529. return {href: val.href, url: $(val).attr('scurl') || val.href, title: val.innerHTML};
  530. // return {url: val.href, title: val.innerHTML};
  531. }),
  532. $player = $('<div class="pa-sc-player loading"></div>').data('pa-sc-player', {id: playerId}),
  533. // $artworks = $('<ol class="sc-artwork-list"></ol>').appendTo($player),
  534. // $info = $('<div class="sc-info"><h3></h3><h4></h4><p></p><a href="#" class="sc-info-close">X</a></div>').appendTo($player),
  535. $list = $('<ul class="sc-trackslist"></ul>').appendTo($player),
  536. $controls = $('<div class="sc-controls"></div>').appendTo($player);
  537. // add the classes of the source node to the player itself
  538. // the players can be indvidually styled this way
  539. if(sourceClasses || opts.customClass){
  540. $player.addClass(sourceClasses).addClass(opts.customClass);
  541. }
  542. log('$source', $source);
  543. log('links', links);
  544. // adding controls to the player
  545. $player
  546. .find('.sc-controls')
  547. .append('<a href="#play" class="sc-play"><span>play</span></a> <a href="#pause" class="sc-pause"><span>pause</span></a>')
  548. .end()
  549. // .append('<a href="#info" class="sc-info-toggle">Info</a>')
  550. // .append('<div class="sc-scrubber"></div>')
  551. // .find('.sc-scrubber')
  552. // .append('<div class="sc-volume-slider"><span class="sc-volume-status" style="width:' + soundVolume +'%"></span></div>')
  553. .append('<div class="sc-time-indicators"><span class="sc-position">00.00</span> | <span class="sc-duration"></span></div>')
  554. .append('<div class="sc-time-span"><div class="sc-buffer"></div><div class="sc-played"></div></div>'); // <div class="sc-waveform-container"></div>
  555. // load and parse the track data from SoundCloud API
  556. loadTracksData($player, links, opts.apiKey);
  557. // init the player GUI, when the tracks data was laoded
  558. $player.bind('onTrackDataLoaded.paScPlayer', function(event) {
  559. log('onTrackDataLoaded.paScPlayer', event, playerId);
  560. // var tracks = event.playerObj.tracks;
  561. // if (opts.randomize) {
  562. // tracks = shuffle(tracks);
  563. // }
  564. // create the playlist
  565. $.each(event.playerObj.tracks, function(index, track) {
  566. log('track', track);
  567. var active = index === 0;
  568. // create an item in the playlist
  569. $('<li>')
  570. .append($('<a>').attr('href', track.href).html(links[index].title))
  571. .data('sc-track', {id:index})
  572. .toggleClass('active', active)
  573. .appendTo($list);
  574. // create an item in the artwork list
  575. // $('<li></li>')
  576. // .append(artworkImage(track, index >= opts.loadArtworks))
  577. // .appendTo($artworks)
  578. // .toggleClass('active', active)
  579. // .data('sc-track', track);
  580. });
  581. // update the element before rendering it in the DOM
  582. $player.each(function() {
  583. if($.isFunction(opts.beforeRender)){
  584. opts.beforeRender.call(this, event.playerObj.tracks);
  585. }
  586. });
  587. // set the first track's duration
  588. $('.sc-duration', $player)[0].innerHTML = timecode(event.playerObj.tracks[0].duration);
  589. $('.sc-position', $player)[0].innerHTML = timecode(0);
  590. // set up the first track info
  591. updateTrackInfo($player, event.playerObj.tracks[0]);
  592. // if continous play enabled always skip to the next track after one finishes
  593. if (opts.continuePlayback) {
  594. $player.bind('onPlayerTrackFinish', function(event) {
  595. onSkip($player);
  596. });
  597. }
  598. // announce the succesful initialization
  599. $player
  600. .removeClass('loading')
  601. .trigger('onPlayerInit');
  602. // if auto play is enabled and it's the first player, start playing
  603. if(opts.autoPlay && !didAutoPlay){
  604. onPlay($player);
  605. didAutoPlay = true;
  606. }
  607. });
  608. // replace the DOM source (if there's one)
  609. $source.each(function(index) {
  610. $(this).replaceWith($player);
  611. });
  612. return $player;
  613. };
  614. // stop all players, might be useful, before replacing the player dynamically
  615. $.paScPlayer.stopAll = function() {
  616. $('.pa-sc-player.playing a.sc-pause').click();
  617. };
  618. // destroy all the players and audio engine, usefull when reloading part of the page and audio has to stop
  619. $.paScPlayer.destroy = function() {
  620. $('.pa-sc-player, .pa-sc-player-engine-container').remove();
  621. };
  622. // plugin wrapper
  623. $.fn.paScPlayer = function(options) {
  624. // reset the auto play
  625. didAutoPlay = false;
  626. // create the players
  627. this.each(function() {
  628. $.paScPlayer(options, this);
  629. });
  630. return this;
  631. };
  632. // default plugin options
  633. $.paScPlayer.defaults = $.fn.paScPlayer.defaults = {
  634. customClass: null,
  635. // do something with the dom object before you render it, add nodes, get more data from the services etc.
  636. beforeRender : function(tracksData) {
  637. var $player = $(this);
  638. },
  639. // initialization, when dom is ready
  640. onDomReady : function() {
  641. $('a.pa-sc-player, div.pa-sc-player').paScPlayer();
  642. },
  643. autoPlay: false,
  644. continuePlayback: true,
  645. randomize: false,
  646. loadArtworks: 5,
  647. // the default Api key should be replaced by your own one
  648. // get it here http://soundcloud.com/you/apps/new
  649. apiKey: '965bd4363fdd909723749b003be67125'
  650. };
  651. // the GUI event bindings
  652. //--------------------------------------------------------
  653. // toggling play/pause
  654. $('a.sc-play, a.sc-pause').live('click', function(event) {
  655. var $list = $(this).closest('.pa-sc-player').find('ul.sc-trackslist');
  656. // simulate the click in the tracklist
  657. $list.find('li.active').click();
  658. return false;
  659. });
  660. // displaying the info panel in the player
  661. // $('a.sc-info-toggle, a.sc-info-close').live('click', function(event) {
  662. // var $link = $(this);
  663. // $link.closest('.pa-sc-player')
  664. // .find('.sc-info').toggleClass('active').end()
  665. // .find('a.sc-info-toggle').toggleClass('active');
  666. // return false;
  667. // });
  668. // selecting tracks in the playlist
  669. $('.sc-trackslist li').live('click', function(event) {
  670. var $track = $(this),
  671. $player = $track.closest('.pa-sc-player'),
  672. trackId = $track.data('sc-track').id,
  673. play = $player.is(':not(.playing)') || $track.is(':not(.active)');
  674. if (play) {
  675. onPlay($player, trackId);
  676. }else{
  677. onPause($player);
  678. }
  679. $track.addClass('active').siblings('li').removeClass('active');
  680. // $('.artworks li', $player).each(function(index) {
  681. // $(this).toggleClass('active', index === trackId);
  682. // });
  683. return false;
  684. });
  685. var scrub = function(node, xPos) {
  686. var $scrubber = $(node).closest('.sc-time-span'),
  687. $buffer = $scrubber.find('.sc-buffer'),
  688. // $available = $scrubber.find('.sc-waveform-container img'),
  689. $player = $scrubber.closest('.pa-sc-player'),
  690. relative = Math.min($buffer.width(), (xPos - $scrubber.offset().left)) / $scrubber.width();
  691. onSeek($player, relative);
  692. };
  693. var onTouchMove = function(ev) {
  694. if (ev.targetTouches.length === 1) {
  695. scrub(ev.target, ev.targetTouches && ev.targetTouches.length && ev.targetTouches[0].clientX);
  696. ev.preventDefault();
  697. }
  698. };
  699. // seeking in the loaded track buffer
  700. $('.sc-time-span')
  701. .live('click', function(event) {
  702. scrub(this, event.pageX);
  703. return false;
  704. })
  705. .live('touchstart', function(event) {
  706. this.addEventListener('touchmove', onTouchMove, false);
  707. event.originalEvent.preventDefault();
  708. })
  709. .live('touchend', function(event) {
  710. this.removeEventListener('touchmove', onTouchMove, false);
  711. event.originalEvent.preventDefault();
  712. });
  713. // changing volume in the player
  714. // var startVolumeTracking = function(node, startEvent) {
  715. // var $node = $(node),
  716. // originX = $node.offset().left,
  717. // originWidth = $node.width(),
  718. // getVolume = function(x) {
  719. // return Math.floor(((x - originX)/originWidth)*100);
  720. // },
  721. // update = function(event) {
  722. // $doc.trigger({type: 'paScPlayer:onVolumeChange', volume: getVolume(event.pageX)});
  723. // };
  724. // $node.bind('mousemove.pa-sc-player', update);
  725. // update(startEvent);
  726. // };
  727. // var stopVolumeTracking = function(node, event) {
  728. // $(node).unbind('mousemove.pa-sc-player');
  729. // };
  730. // $('.sc-volume-slider')
  731. // .live('mousedown', function(event) {
  732. // startVolumeTracking(this, event);
  733. // })
  734. // .live('mouseup', function(event) {
  735. // stopVolumeTracking(this, event);
  736. // });
  737. // $doc.bind('paScPlayer:onVolumeChange', function(event) {
  738. // $('span.sc-volume-status').css({width: event.volume + '%'});
  739. // });
  740. // -------------------------------------------------------------------
  741. // the default Auto-Initialization
  742. $(function() {
  743. if($.isFunction($.paScPlayer.defaults.onDomReady)){
  744. $.paScPlayer.defaults.onDomReady();
  745. }
  746. });
  747. })(jQuery);