preview_jplayer.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. var _playlist_jplayer;
  2. var _idToPostionLookUp;
  3. /**
  4. *When the page loads the ready function will get all the data it can from the hidden span elements
  5. *and call one of three functions depending on weather the window was open to play an audio file,
  6. *or a playlist or a show.
  7. */
  8. $(document).ready(function(){
  9. $.jPlayer.timeFormat.showHour = true;
  10. var audioUri = $('.audioUri').text();
  11. var audioMime = $('.audioMime').text();
  12. var playlistID = $('.playlistID').text();
  13. var playlistIndex = $('.playlistIndex').text();
  14. var showID = $('.showID').text();
  15. var showIndex = $('.showIndex').text();
  16. var blockId = $('.blockId').text();
  17. var blockIndex = $('.blockIndex').text();
  18. _playlist_jplayer = new jPlayerPlaylist({
  19. jPlayer: "#jquery_jplayer_1",
  20. cssSelectorAncestor: "#jp_container_1"
  21. },[], //array of songs will be filled with below's json call
  22. {
  23. swfPath: baseUrl+"js/jplayer",
  24. supplied:"oga, mp3, m4v, m4a, wav",
  25. size: {
  26. width: "0px",
  27. height: "0px",
  28. cssClass: "jp-video-270p"
  29. },
  30. playlistOptions: {
  31. autoPlay: false,
  32. loopOnPrevious: false,
  33. shuffleOnLoop: true,
  34. enableRemoveControls: false,
  35. displayTime: 0,
  36. addTime: 0,
  37. removeTime: 0,
  38. shuffleTime: 0
  39. },
  40. ready: function(){
  41. if (playlistID != "" && playlistID !== ""){
  42. playAllPlaylist(playlistID, playlistIndex);
  43. }else if (audioUri != "") {
  44. playOne(audioUri, audioMime);
  45. }else if (showID != "") {
  46. playAllShow(showID, showIndex);
  47. }else if(blockId != "" && blockIndex != ""){
  48. playBlock(blockId, blockIndex);
  49. }
  50. }
  51. });
  52. $("#jp_container_1").on("mouseenter", "ul.jp-controls li", function(ev) {
  53. $(this).addClass("ui-state-hover");
  54. });
  55. $("#jp_container_1").on("mouseleave", "ul.jp-controls li", function(ev) {
  56. $(this).removeClass("ui-state-hover");
  57. });
  58. });
  59. /**
  60. * Sets up the jPlayerPlaylist to play.
  61. * - Get the playlist info based on the playlistID give.
  62. * - Update the playlistIndex to the position in the pllist to start playing.
  63. * - Select the element played from and start playing. If playlist is null then start at index 0.
  64. **/
  65. function playAllPlaylist(p_playlistID, p_playlistIndex) {
  66. var viewsPlaylistID = $('.playlistID').text();
  67. if ( _idToPostionLookUp !== undefined && viewsPlaylistID == p_playlistID ) {
  68. play(p_playlistIndex);
  69. }else {
  70. buildplaylist(baseUrl+"audiopreview/get-playlist/playlistID/"+p_playlistID, p_playlistIndex);
  71. }
  72. }
  73. function playBlock(p_blockId, p_blockIndex)
  74. {
  75. var viewsBlockId = $('.blockId').text();
  76. if ( _idToPostionLookUp !== undefined && viewsBlockId == p_blockId ) {
  77. play(p_blockIndex);
  78. } else {
  79. buildplaylist(baseUrl+"audiopreview/get-block/blockId/"+p_blockId, p_blockIndex);
  80. }
  81. }
  82. /**
  83. * Sets up the show to play.
  84. * checks with the show id given to the show id on the page/view
  85. * if the show id and the page or views show id are the same it means the user clicked another
  86. * file in the same show, so don't refresh the show content tust play the track from the preloaded show.
  87. * if the the ids are different they we'll need to get the show's context so create the uri
  88. * and call the controller.
  89. **/
  90. function playAllShow(p_showID, p_index) {
  91. var viewsShowID = $('.showID').text();
  92. if ( _idToPostionLookUp !== undefined && viewsShowID == p_showID ) {
  93. play(p_index);
  94. }else {
  95. buildplaylist(baseUrl+"audiopreview/get-show/showID/"+p_showID, p_index);
  96. }
  97. }
  98. /**
  99. * This function will call the AudiopreviewController to get the contents of
  100. * either a show or playlist Looping throught the returned contents and
  101. * creating media for each track.
  102. *
  103. * Then trigger the jplayer to play the list.
  104. */
  105. function buildplaylist(p_url, p_playIndex) {
  106. _idToPostionLookUp = Array();
  107. $.getJSON(p_url, function(data){ // get the JSON array produced by my PHP
  108. var myPlaylist = new Array();
  109. var media;
  110. var index;
  111. var total = 0;
  112. var skipped = 0;
  113. for(index in data) {
  114. if (data[index]['type'] == 0) {
  115. if (data[index]['element_mp3'] != undefined){
  116. media = {title: data[index]['element_title'],
  117. artist: data[index]['element_artist'],
  118. mp3:data[index]['uri']
  119. };
  120. } else if (data[index]['element_oga'] != undefined) {
  121. media = {title: data[index]['element_title'],
  122. artist: data[index]['element_artist'],
  123. oga:data[index]['uri']
  124. };
  125. } else if (data[index]['element_m4a'] != undefined) {
  126. media = {title: data[index]['element_title'],
  127. artist: data[index]['element_artist'],
  128. m4a:data[index]['uri']
  129. };
  130. } else if (data[index]['element_wav'] != undefined) {
  131. media = {title: data[index]['element_title'],
  132. artist: data[index]['element_artist'],
  133. wav:data[index]['uri']
  134. };
  135. } else {
  136. // skip this track since it's not supported
  137. console.log("continue");
  138. skipped++;
  139. continue;
  140. }
  141. } else if (data[index]['type'] == 1) {
  142. var mime = data[index]['mime'];
  143. if (mime.search(/mp3/i) > 0 || mime.search(/mpeg/i) > 0) {
  144. key = "mp3";
  145. } else if (mime.search(/og(g|a)/i) > 0 || mime.search(/vorbis/i) > 0) {
  146. key = "oga";
  147. } else if (mime.search(/mp4/i) > 0) {
  148. key = "m4a";
  149. } else if (mime.search(/wav/i) > 0) {
  150. key = "wav";
  151. }
  152. if (key) {
  153. media = {title: data[index]['element_title'],
  154. artist: data[index]['element_artist']
  155. };
  156. media[key] = data[index]['uri']
  157. }
  158. }
  159. if (media && isAudioSupported(data[index]['mime'])) {
  160. // javascript doesn't support associative array with numeric key
  161. // so we need to remove the gap if we skip any of tracks due to
  162. // browser incompatibility.
  163. myPlaylist[index-skipped] = media;
  164. }
  165. // we should create a map according to the new position in the
  166. // player itself total is the index on the player
  167. _idToPostionLookUp[data[index]['element_id']] = total;
  168. total++;
  169. }
  170. _playlist_jplayer.setPlaylist(myPlaylist);
  171. _playlist_jplayer.option("autoPlay", true);
  172. play(p_playIndex);
  173. var height = Math.min(143 + (26 * total), 400);
  174. var width = 505;
  175. if (height === 400) {
  176. window.scrollbars = true;
  177. }
  178. else {
  179. //there's no scrollbars so we don't need the window to be as wide.
  180. width = 490;
  181. }
  182. window.resizeTo(width, height);
  183. });
  184. }
  185. /**
  186. *Function simply plays the given index, for playlists index can be different so need to look up the
  187. *right index.
  188. */
  189. function play(p_playlistIndex){
  190. playlistIndex = _idToPostionLookUp[p_playlistIndex];
  191. if(playlistIndex == undefined){
  192. playlistIndex = 0
  193. }
  194. //_playlist_jplayer.select(playlistIndex);
  195. _playlist_jplayer.play(playlistIndex);
  196. }
  197. /**
  198. * Playing one audio track occurs from the library. This function will create the media, setup
  199. * jplayer and play the track.
  200. */
  201. function playOne(uri, mime) {
  202. var playlist = new Array();
  203. var media = null;
  204. var key = null;
  205. if (mime.search(/mp3/i) > 0 || mime.search(/mpeg/i) > 0) {
  206. key = "mp3";
  207. } else if (mime.search(/og(g|a)/i) > 0 || mime.search(/vorbis/i) > 0) {
  208. key = "oga";
  209. } else if (mime.search(/mp4/i) > 0) {
  210. key = "m4a";
  211. } else if (mime.search(/wav/i) > 0) {
  212. key = "wav";
  213. }
  214. if (key) {
  215. media = {title: $('.audioFileTitle').text() != 'null' ?$('.audioFileTitle').text():"",
  216. artist: $('.audioFileArtist').text() != 'null' ?$('.audioFileArtist').text():""
  217. };
  218. media[key] = uri;
  219. }
  220. if (media) {
  221. _playlist_jplayer.option("autoPlay", true);
  222. playlist[0] = media;
  223. _playlist_jplayer.setPlaylist(playlist);
  224. _playlist_jplayer.play(0);
  225. }
  226. window.resizeTo(490, 167);
  227. }