2 * Copyright (c) 2013, Intel Corporation.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
10 //Audio list can be re-ordered while filling.
11 //This requires cancelling all previous timeouts to properly fill
12 var clearAudioTimeouts = new Array();
13 var lightColor = false;
17 console.log("MediaPlayer: file failed to load");
20 var drawCanvasImage = function(imageSrc) {
22 drawAlbumArt(imageSrc);
26 function drawAlbumArt(imageSrc)
28 if (currentPlayerType === "AUDIO")
30 var canvasH = mediaListItemH * 0.95 ;
31 for (var i in audioContent)
33 if (audioContent[i].coverArt.src !== musicIcon.src && audioContent[i].coverArt.src === imageSrc && audioContent[i].ctx)
39 updateMediaName(audioContent[audioIndex].artists[0], audioContent[audioIndex].title, audioContent[audioIndex].coverArt);
41 audioContent[i].ctx.drawImage(audioContent[i].coverArt, 0, 0, canvasH, canvasH);
45 console.log("MediaPlayer: drawImage failed - " + err);
54 console.log("MediaPlayer in loadImages");
56 for (var src in audioContent)
60 var imgSrc = (audioContent[src].coverArtURI !== undefined && audioContent[src].coverArtURI !== "") ? audioContent[src].coverArtURI : "images/musicIcon.png";
61 audioContent[src].coverArt = new Image();
62 audioContent[src].coverArt.onload = drawCanvasImage(imgSrc);
64 audioContent[src].coverArt.onerror = function(e){
65 audioContent[src].coverArt.src = "images/musicIcon.png";
68 audioContent[src].coverArt.src = imgSrc;
72 console.log("Failed to load audio cover image: " + err);
77 function makeListItem(j, k, contentList, playerType, listItems)
81 var canvasW = mediaListItemW;
82 var canvasH = mediaListItemH * 0.95 ;
83 var lowerType = playerType.toLowerCase();
85 for (var i = j; (i < (j+k) && i < contentList.length); i++)
93 listItems.append("<li id=" + lowerType + "ListItem" + i + " style='width:" + mediaListItemW + "px;" + " height:" + mediaListItemH + "px;" +
94 " margin-bottom: 10px' ><a href='#' class='night' >" +
95 "<canvas id=" + lowerType + "CanvasNum" + i + " width=" + canvasW + " height=" + canvasH + "> </canvas>" +
101 listItems.append("<li id=" + lowerType + "ListItem" + i + " style='width:" + mediaListItemW + "px;" + " height:" + mediaListItemH + "px;" +
102 " margin-bottom: 10px' ><a href='#'>" +
103 "<canvas id=" + lowerType + "CanvasNum" + i + " width=" + canvasW + " height=" + canvasH + "> </canvas>" +
112 listItems.append("<li id=" + lowerType + "ListItem" + i + " style='width:" + mediaListItemW + "px;" + " height:" + mediaListItemH + "px;" +
113 " margin-bottom: 10px' ><a href='#' class='lightColor night' >" +
114 "<canvas id=" + lowerType + "CanvasNum" + i + " width=" + canvasW + " height=" + canvasH + "> </canvas>" +
120 listItems.append("<li id=" + lowerType + "ListItem" + i + " style='width:" + mediaListItemW + "px;" + " height:" + mediaListItemH + "px;" +
121 " margin-bottom: 10px' ><a href='#' class='lightColor' >" +
122 "<canvas id=" + lowerType + "CanvasNum" + i + " width=" + canvasW + " height=" + canvasH + "> </canvas>" +
129 lightColor = !lightColor;
131 var currentCanvas = document.getElementById(lowerType + "CanvasNum" + i);
132 var currentCTX = currentCanvas.getContext("2d");
134 // Set callback function for the new list item
135 $("#" + lowerType + "CanvasNum" + i).click(function () {
137 $("#playButton").toggleClass('playing', false);
144 audioIndex = $(this).parent().parent().index();
146 updateMediaName(audioContent[audioIndex].artists[0], audioContent[audioIndex].title, audioContent[audioIndex].coverArt);
147 $("#audioSrc").attr("src", audioContent[audioIndex].contentURI);
148 localStorage.prevAudioTrack = audioContent[audioIndex].contentURI;
153 console.log("MediaPlayer: load audio error " + err.message);
160 videoIndex = $(this).parent().parent().index();
161 $("#videoSrc").attr("src", videoContent[videoIndex].contentURI);
162 localStorage.prevVideo = videoContent[videoIndex].contentURI;
164 currentMediaList.fadeOut(300);
168 console.log("MediaPlayer: load video error " + err.message);
175 imageIndex = $(this).parent().parent().index();
176 $("#imagePlayer").css("background", "url(" + imageContent[imageIndex].contentURI + ") no-repeat center");
177 $("#imagePlayer").css("background-size", "contain");
178 currentMediaList.fadeOut(300);
182 console.log("MediaPlayer: load image error: " + err.message);
195 audioContent[i].ctx = currentCTX;
199 //Check if album art is done loading, if not draw default until it is
200 if (audioContent[i].coverArt.complete)
201 currentCTX.drawImage(audioContent[i].coverArt, 0, 0, canvasH, canvasH );
203 currentCTX.drawImage(musicIcon, 0, 0, canvasH, canvasH );
207 console.log("MediaPlayer: drawImage failed - " + err);
210 var trackText = new TextObject(currentCTX,{"text" : contentList[i].title, "xLoc" : canvasH + 20, "yLoc" : canvasH / 2.5 , "zLoc" : 0});
211 var artistText = new TextObject(currentCTX,{"text" : contentList[i].artists[0], "xLoc" : canvasH + 20, "yLoc" : canvasH / 1.1 , "zLoc" : 0});
212 clearAudioTimeouts.splice(0, 1);
216 currentCTX.drawImage(vidIcon, 0, 0, canvasH, canvasH );
217 var artistText = new TextObject(currentCTX,{"text" : contentList[i].artists[0], "xLoc" : canvasH + 20, "yLoc" : canvasH / 2.5 , "zLoc" : 0});
218 var trackText = new TextObject(currentCTX,{"text" : contentList[i].title, "xLoc" : canvasH + 20, "yLoc" : canvasH / 1.1 , "zLoc" : 0});
222 currentCTX.drawImage(imgIcon, 0, 0, canvasH, canvasH );
223 var artistText = new TextObject(currentCTX,{"text" : contentList[i].title, "xLoc" : canvasH + 20, "yLoc" : canvasH / 2.5 , "zLoc" : 0});
224 var trackText = new TextObject(currentCTX,{"text" : " ", "xLoc" : canvasH + 20, "yLoc" : canvasH / 1.1 , "zLoc" : 0});
231 var mediaTextTemp1 = screenOrientation === "portrait" ? mediaTextTemplate2 : mediaTextTemplate2Landscape;
232 var mediaTextTemp2 = screenOrientation === "portrait" ? mediaTextTemplate3 : mediaTextTemplate3Landscape;
233 var trackTextTemp = screenOrientation === "portrait" ? trackTextTemplate : trackTextTemplateLandscape;
235 trackText.applyTemplate(mediaTextTemp1);
237 trackText.applyTemplate(mediaTextTemp2);
240 artistText.applyTemplate(trackTextTemp);
241 artistText.drawObj();
245 function emptyTimeouts()
249 while (clearItem = clearAudioTimeouts.pop())
251 clearTimeout(clearItem);
255 function fillMediaList(contentList)
257 console.log("MediaPlayer in fillMediaList");
259 //Don't try and fill an empty content list
260 if (contentList === undefined || contentList === null || contentList.length <=0)
263 if (currentPlayerType === "AUDIO")
266 currentMediaListItems.empty();
269 switch (currentPlayerType)
272 audioMediaListLoaded = true;
275 videoMediaListLoaded = true;
278 imageMediaListLoaded = true;
287 for (var i=0; i < contentList.length; i++)
289 tmpClearTimeout = setTimeout(makeListItem.bind(this, i, jumpSize, contentList, currentPlayerType, currentMediaListItems), timeOut);
290 clearAudioTimeouts.push(tmpClearTimeout);