pa-sc-player.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. }
  288. }).done(function (data) {
  289. console.log('Token', data);
  290. });
  291. // $.ajax({
  292. // "dataType": "json",
  293. // "url": apiUrl,
  294. // "data": {},
  295. // "headers": {
  296. // "Authorization": "OAuth bb68647335a47f104a86dcddf4e70fa8"
  297. // },
  298. // "success": function(data) {
  299. // // log('data loaded', link.url, data);
  300. // index += 1;
  301. // // added by Bach for MJ
  302. // data.href = link.href;
  303. // if(data.tracks){
  304. // // log('data.tracks', data.tracks);
  305. // playerObj.tracks = playerObj.tracks.concat(data.tracks);
  306. // }else if(data.duration){
  307. // // a secret link fix, till the SC API returns permalink with secret on secret response
  308. // data.permalink_url = link.url;
  309. // // if track, add to player
  310. // playerObj.tracks.push(data);
  311. // }else if(data.creator){
  312. // // it's a group!
  313. // links.push({url:data.uri + '/tracks'});
  314. // }else if(data.username){
  315. // // if user, get his tracks or favorites
  316. // if(/favorites/.test(link.url)){
  317. // links.push({url:data.uri + '/favorites'});
  318. // }else{
  319. // links.push({url:data.uri + '/tracks'});
  320. // }
  321. // }else if($.isArray(data)){
  322. // playerObj.tracks = playerObj.tracks.concat(data);
  323. // }
  324. // if(links[index]){
  325. // // if there are more track to load, get them from the api
  326. // loadUrl(links[index]);
  327. // }else{
  328. // // if loading finishes, anounce it to the GUI
  329. // playerObj.node.trigger({type:'onTrackDataLoaded', playerObj: playerObj, url: apiUrl});
  330. // }
  331. // },
  332. // "error": function(errorThrown) {
  333. // console.error(JSON.stringify(errorThrown.error()));
  334. // }
  335. // });
  336. };
  337. // update current API key
  338. apiKey = key;
  339. // update the players queue
  340. players.push(playerObj);
  341. // load first tracks
  342. loadUrl(links[index]);
  343. },
  344. artworkImage = function(track, usePlaceholder) {
  345. if(usePlaceholder){
  346. return '<div class="sc-loading-artwork">Loading Artwork</div>';
  347. }else if (track.artwork_url) {
  348. return '<img src="' + track.artwork_url.replace('-large', '-t300x300') + '"/>';
  349. }else{
  350. return '<div class="sc-no-artwork">No Artwork</div>';
  351. }
  352. },
  353. updateTrackInfo = function($player, track) {
  354. // update the current track info in the player
  355. // log('updateTrackInfo', track);
  356. // $('.sc-info', $player).each(function(index) {
  357. // $('h3', this).html('<a href="' + track.permalink_url +'">' + track.title + '</a>');
  358. // $('h4', this).html('by <a href="' + track.user.permalink_url +'">' + track.user.username + '</a>');
  359. // $('p', this).html(track.description || 'no Description');
  360. // });
  361. // update the artwork
  362. // $('.sc-artwork-list li', $player).each(function(index) {
  363. // var $item = $(this),
  364. // itemTrack = $item.data('sc-track');
  365. //
  366. // if (itemTrack === track) {
  367. // // show track artwork
  368. // $item
  369. // .addClass('active')
  370. // .find('.sc-loading-artwork')
  371. // .each(function(index) {
  372. // // if the image isn't loaded yet, do it now
  373. // $(this).removeClass('sc-loading-artwork').html(artworkImage(track, false));
  374. // });
  375. // }else{
  376. // // reset other artworks
  377. // $item.removeClass('active');
  378. // }
  379. // });
  380. // update the track duration in the progress bar
  381. $('.sc-duration', $player).html(timecode(track.duration));
  382. // put the waveform into the progress bar
  383. $('.sc-waveform-container', $player).html('<img src="' + track.waveform_url +'" />');
  384. $player.trigger('onPlayerTrackSwitch.paScPlayer', [track]);
  385. },
  386. play = function(track) {
  387. var url = track.permalink_url;
  388. if(currentUrl === url){
  389. // log('will play');
  390. audioEngine.play();
  391. }else{
  392. currentUrl = url;
  393. // log('will load', url);
  394. audioEngine.load(track, apiKey);
  395. }
  396. },
  397. getPlayerData = function(node) {
  398. return players[$(node).data('pa-sc-player').id];
  399. },
  400. updatePlayStatus = function(player, status) {
  401. if(status){
  402. // reset all other players playing status
  403. $('div.pa-sc-player.playing').removeClass('playing');
  404. }
  405. $(player)
  406. .toggleClass('playing', status)
  407. .trigger((status ? 'onPlayerPlay' : 'onPlayerPause'));
  408. },
  409. onPlay = function(player, id) {
  410. var track = getPlayerData(player).tracks[id || 0];
  411. updateTrackInfo(player, track);
  412. // cache the references to most updated DOM nodes in the progress bar
  413. updates = {
  414. $buffer: $('.sc-buffer', player),
  415. $played: $('.sc-played', player),
  416. position: $('.sc-position', player)[0]
  417. };
  418. updatePlayStatus(player, true);
  419. play(track);
  420. },
  421. onPause = function(player) {
  422. updatePlayStatus(player, false);
  423. audioEngine.pause();
  424. },
  425. onFinish = function() {
  426. var $player = updates.$played.closest('.pa-sc-player'),
  427. $nextItem;
  428. // update the scrubber width
  429. updates.$played.css('width', '0%');
  430. // show the position in the track position counter
  431. updates.position.innerHTML = timecode(0);
  432. // reset the player state
  433. updatePlayStatus($player, false);
  434. // stop the audio
  435. audioEngine.stop();
  436. $player.trigger('onPlayerTrackFinish');
  437. },
  438. onSeek = function(player, relative) {
  439. audioEngine.seek(relative);
  440. $(player).trigger('onPlayerSeek');
  441. },
  442. onSkip = function(player) {
  443. var $player = $(player);
  444. // continue playing through all players
  445. log('track finished get the next one');
  446. $nextItem = $('.sc-trackslist li.active', $player).next('li');
  447. // try to find the next track in other player
  448. if(!$nextItem.length){
  449. $nextItem = $player.nextAll('div.pa-sc-player:first').find('.sc-trackslist li.active');
  450. }
  451. $nextItem.click();
  452. },
  453. soundVolume = function() {
  454. var vol = 80,
  455. cooks = document.cookie.split(';'),
  456. volRx = new RegExp('paScPlayer_volume=(\\d+)');
  457. for(var i in cooks){
  458. if(volRx.test(cooks[i])){
  459. vol = parseInt(cooks[i].match(volRx)[1], 10);
  460. break;
  461. }
  462. }
  463. return vol;
  464. }(),
  465. onVolume = function(volume) {
  466. var vol = Math.floor(volume);
  467. // save the volume in the cookie
  468. var date = new Date();
  469. date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
  470. soundVolume = vol;
  471. document.cookie = ['paScPlayer_volume=', vol, '; expires=', date.toUTCString(), '; path="/"'].join('');
  472. // update the volume in the engine
  473. audioEngine.setVolume(soundVolume);
  474. },
  475. positionPoll;
  476. // listen to audio engine events
  477. $doc
  478. .bind('paScPlayer:onAudioReady', function(event) {
  479. log('onPlayerReady: audio engine is ready');
  480. audioEngine.play();
  481. // set initial volume
  482. onVolume(soundVolume);
  483. })
  484. // when the loaded track started to play
  485. .bind('paScPlayer:onMediaPlay', function(event) {
  486. clearInterval(positionPoll);
  487. positionPoll = setInterval(function() {
  488. var duration = audioEngine.getDuration(),
  489. position = audioEngine.getPosition(),
  490. relative = (position / duration);
  491. // update the scrubber width
  492. updates.$played.css('width', (100 * relative) + '%');
  493. // show the position in the track position counter
  494. updates.position.innerHTML = timecode(position);
  495. // announce the track position to the DOM
  496. $doc.trigger({
  497. type: 'onMediaTimeUpdate.paScPlayer',
  498. duration: duration,
  499. position: position,
  500. relative: relative
  501. });
  502. }, 500);
  503. })
  504. // when the loaded track is was paused
  505. .bind('paScPlayer:onMediaPause', function(event) {
  506. clearInterval(positionPoll);
  507. positionPoll = null;
  508. })
  509. // change the volume
  510. // .bind('paScPlayer:onVolumeChange', function(event) {
  511. // onVolume(event.volume);
  512. // })
  513. .bind('paScPlayer:onMediaEnd', function(event) {
  514. onFinish();
  515. })
  516. .bind('paScPlayer:onMediaBuffering', function(event) {
  517. updates.$buffer.css('width', event.percent + '%');
  518. });
  519. // Generate custom skinnable HTML/CSS/JavaScript based SoundCloud players from links to SoundCloud resources
  520. $.paScPlayer = function(options, node) {
  521. var opts = $.extend({}, $.paScPlayer.defaults, options),
  522. playerId = players.length,
  523. $source = node && $(node),
  524. sourceClasses = $source[0].className.replace('pa-sc-player', ''),
  525. links =
  526. opts.links
  527. || $.map($('a', $source)
  528. .add($source.filter('a')), function(val) {
  529. //log('val', val);
  530. return {href: val.href, url: $(val).attr('scurl') || val.href, title: val.innerHTML};
  531. // return {url: val.href, title: val.innerHTML};
  532. }),
  533. $player = $('<div class="pa-sc-player loading"></div>').data('pa-sc-player', {id: playerId}),
  534. // $artworks = $('<ol class="sc-artwork-list"></ol>').appendTo($player),
  535. // $info = $('<div class="sc-info"><h3></h3><h4></h4><p></p><a href="#" class="sc-info-close">X</a></div>').appendTo($player),
  536. $list = $('<ul class="sc-trackslist"></ul>').appendTo($player),
  537. $controls = $('<div class="sc-controls"></div>').appendTo($player);
  538. // add the classes of the source node to the player itself
  539. // the players can be indvidually styled this way
  540. if(sourceClasses || opts.customClass){
  541. $player.addClass(sourceClasses).addClass(opts.customClass);
  542. }
  543. log('$source', $source);
  544. log('links', links);
  545. // adding controls to the player
  546. $player
  547. .find('.sc-controls')
  548. .append('<a href="#play" class="sc-play"><span>play</span></a> <a href="#pause" class="sc-pause"><span>pause</span></a>')
  549. .end()
  550. // .append('<a href="#info" class="sc-info-toggle">Info</a>')
  551. // .append('<div class="sc-scrubber"></div>')
  552. // .find('.sc-scrubber')
  553. // .append('<div class="sc-volume-slider"><span class="sc-volume-status" style="width:' + soundVolume +'%"></span></div>')
  554. .append('<div class="sc-time-indicators"><span class="sc-position">00.00</span> | <span class="sc-duration"></span></div>')
  555. .append('<div class="sc-time-span"><div class="sc-buffer"></div><div class="sc-played"></div></div>'); // <div class="sc-waveform-container"></div>
  556. // load and parse the track data from SoundCloud API
  557. loadTracksData($player, links, opts.apiKey);
  558. // init the player GUI, when the tracks data was laoded
  559. $player.bind('onTrackDataLoaded.paScPlayer', function(event) {
  560. log('onTrackDataLoaded.paScPlayer', event, playerId);
  561. // var tracks = event.playerObj.tracks;
  562. // if (opts.randomize) {
  563. // tracks = shuffle(tracks);
  564. // }
  565. // create the playlist
  566. $.each(event.playerObj.tracks, function(index, track) {
  567. log('track', track);
  568. var active = index === 0;
  569. // create an item in the playlist
  570. $('<li>')
  571. .append($('<a>').attr('href', track.href).html(links[index].title))
  572. .data('sc-track', {id:index})
  573. .toggleClass('active', active)
  574. .appendTo($list);
  575. // create an item in the artwork list
  576. // $('<li></li>')
  577. // .append(artworkImage(track, index >= opts.loadArtworks))
  578. // .appendTo($artworks)
  579. // .toggleClass('active', active)
  580. // .data('sc-track', track);
  581. });
  582. // update the element before rendering it in the DOM
  583. $player.each(function() {
  584. if($.isFunction(opts.beforeRender)){
  585. opts.beforeRender.call(this, event.playerObj.tracks);
  586. }
  587. });
  588. // set the first track's duration
  589. $('.sc-duration', $player)[0].innerHTML = timecode(event.playerObj.tracks[0].duration);
  590. $('.sc-position', $player)[0].innerHTML = timecode(0);
  591. // set up the first track info
  592. updateTrackInfo($player, event.playerObj.tracks[0]);
  593. // if continous play enabled always skip to the next track after one finishes
  594. if (opts.continuePlayback) {
  595. $player.bind('onPlayerTrackFinish', function(event) {
  596. onSkip($player);
  597. });
  598. }
  599. // announce the succesful initialization
  600. $player
  601. .removeClass('loading')
  602. .trigger('onPlayerInit');
  603. // if auto play is enabled and it's the first player, start playing
  604. if(opts.autoPlay && !didAutoPlay){
  605. onPlay($player);
  606. didAutoPlay = true;
  607. }
  608. });
  609. // replace the DOM source (if there's one)
  610. $source.each(function(index) {
  611. $(this).replaceWith($player);
  612. });
  613. return $player;
  614. };
  615. // stop all players, might be useful, before replacing the player dynamically
  616. $.paScPlayer.stopAll = function() {
  617. $('.pa-sc-player.playing a.sc-pause').click();
  618. };
  619. // destroy all the players and audio engine, usefull when reloading part of the page and audio has to stop
  620. $.paScPlayer.destroy = function() {
  621. $('.pa-sc-player, .pa-sc-player-engine-container').remove();
  622. };
  623. // plugin wrapper
  624. $.fn.paScPlayer = function(options) {
  625. // reset the auto play
  626. didAutoPlay = false;
  627. // create the players
  628. this.each(function() {
  629. $.paScPlayer(options, this);
  630. });
  631. return this;
  632. };
  633. // default plugin options
  634. $.paScPlayer.defaults = $.fn.paScPlayer.defaults = {
  635. customClass: null,
  636. // do something with the dom object before you render it, add nodes, get more data from the services etc.
  637. beforeRender : function(tracksData) {
  638. var $player = $(this);
  639. },
  640. // initialization, when dom is ready
  641. onDomReady : function() {
  642. $('a.pa-sc-player, div.pa-sc-player').paScPlayer();
  643. },
  644. autoPlay: false,
  645. continuePlayback: true,
  646. randomize: false,
  647. loadArtworks: 5,
  648. // the default Api key should be replaced by your own one
  649. // get it here http://soundcloud.com/you/apps/new
  650. apiKey: '965bd4363fdd909723749b003be67125'
  651. };
  652. // the GUI event bindings
  653. //--------------------------------------------------------
  654. // toggling play/pause
  655. $('a.sc-play, a.sc-pause').live('click', function(event) {
  656. var $list = $(this).closest('.pa-sc-player').find('ul.sc-trackslist');
  657. // simulate the click in the tracklist
  658. $list.find('li.active').click();
  659. return false;
  660. });
  661. // displaying the info panel in the player
  662. // $('a.sc-info-toggle, a.sc-info-close').live('click', function(event) {
  663. // var $link = $(this);
  664. // $link.closest('.pa-sc-player')
  665. // .find('.sc-info').toggleClass('active').end()
  666. // .find('a.sc-info-toggle').toggleClass('active');
  667. // return false;
  668. // });
  669. // selecting tracks in the playlist
  670. $('.sc-trackslist li').live('click', function(event) {
  671. var $track = $(this),
  672. $player = $track.closest('.pa-sc-player'),
  673. trackId = $track.data('sc-track').id,
  674. play = $player.is(':not(.playing)') || $track.is(':not(.active)');
  675. if (play) {
  676. onPlay($player, trackId);
  677. }else{
  678. onPause($player);
  679. }
  680. $track.addClass('active').siblings('li').removeClass('active');
  681. // $('.artworks li', $player).each(function(index) {
  682. // $(this).toggleClass('active', index === trackId);
  683. // });
  684. return false;
  685. });
  686. var scrub = function(node, xPos) {
  687. var $scrubber = $(node).closest('.sc-time-span'),
  688. $buffer = $scrubber.find('.sc-buffer'),
  689. // $available = $scrubber.find('.sc-waveform-container img'),
  690. $player = $scrubber.closest('.pa-sc-player'),
  691. relative = Math.min($buffer.width(), (xPos - $scrubber.offset().left)) / $scrubber.width();
  692. onSeek($player, relative);
  693. };
  694. var onTouchMove = function(ev) {
  695. if (ev.targetTouches.length === 1) {
  696. scrub(ev.target, ev.targetTouches && ev.targetTouches.length && ev.targetTouches[0].clientX);
  697. ev.preventDefault();
  698. }
  699. };
  700. // seeking in the loaded track buffer
  701. $('.sc-time-span')
  702. .live('click', function(event) {
  703. scrub(this, event.pageX);
  704. return false;
  705. })
  706. .live('touchstart', function(event) {
  707. this.addEventListener('touchmove', onTouchMove, false);
  708. event.originalEvent.preventDefault();
  709. })
  710. .live('touchend', function(event) {
  711. this.removeEventListener('touchmove', onTouchMove, false);
  712. event.originalEvent.preventDefault();
  713. });
  714. // changing volume in the player
  715. // var startVolumeTracking = function(node, startEvent) {
  716. // var $node = $(node),
  717. // originX = $node.offset().left,
  718. // originWidth = $node.width(),
  719. // getVolume = function(x) {
  720. // return Math.floor(((x - originX)/originWidth)*100);
  721. // },
  722. // update = function(event) {
  723. // $doc.trigger({type: 'paScPlayer:onVolumeChange', volume: getVolume(event.pageX)});
  724. // };
  725. // $node.bind('mousemove.pa-sc-player', update);
  726. // update(startEvent);
  727. // };
  728. // var stopVolumeTracking = function(node, event) {
  729. // $(node).unbind('mousemove.pa-sc-player');
  730. // };
  731. // $('.sc-volume-slider')
  732. // .live('mousedown', function(event) {
  733. // startVolumeTracking(this, event);
  734. // })
  735. // .live('mouseup', function(event) {
  736. // stopVolumeTracking(this, event);
  737. // });
  738. // $doc.bind('paScPlayer:onVolumeChange', function(event) {
  739. // $('span.sc-volume-status').css({width: event.volume + '%'});
  740. // });
  741. // -------------------------------------------------------------------
  742. // the default Auto-Initialization
  743. $(function() {
  744. if($.isFunction($.paScPlayer.defaults.onDomReady)){
  745. $.paScPlayer.defaults.onDomReady();
  746. }
  747. });
  748. })(jQuery);