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 AudioPlayer = function()
12 this.clearAudioTimeInterval = undefined;
13 this.currentAudioContent = undefined;
16 AudioPlayer.prototype.play = function()
18 console.log("MediaPlayer in AudioPlayer::play");
20 if (this.currentFileLoaded)
22 if (this.playerControls.paused)
24 this.playerControls.play();
26 //Start tracking the current time of the media. This is used to play from previous position on restart.
27 //Currently this isn't supported for remote files
28 if (!this.content[this.listIndex].remoteFile)
30 var timeoutFunction = function()
32 localStorage.prevAudioTime = this.playerControls.currentTime;
35 this.clearAudioTimeInterval = setInterval(timeoutFunction.bind(this),500);
40 console.log(this.content[this.listIndex].artists[0] + " : " + this.content[this.listIndex].title + " can't play yet, it hasn't loaded");
43 AudioPlayer.prototype.pause = function()
45 if (!this.playerControls.paused)
47 this.playerControls.pause();
48 clearInterval(this.clearAudioTimeInterval);
52 AudioPlayer.prototype.playing = function()
54 return !(this.playerControls.paused);
57 AudioPlayer.prototype.next = function()
61 if (this.content.length > (this.listIndex + 1))
66 this.load(this.listIndex, true);
70 AudioPlayer.prototype.previous = function()
74 if (this.listIndex > 0 )
77 this.listIndex = this.content.length - 1;
79 this.load(this.listIndex, true);
83 AudioPlayer.prototype.load = function(index, play)
85 this.listIndex = index;
86 this.loadAndPlay = play;
87 this.playerControls.pause();
88 this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
89 $("#audioSrc").attr("src", this.content[this.listIndex].contentURI);
90 this.playerControls.load();
93 AudioPlayer.prototype.updateMediaName = function(newArtist, newTitle, newCover)
95 var playBarHeight = mediaNameCanvas.height;
96 var boxWidth = playBarHeight * 0.75;
97 mediaNameCTX.clearRect(0,0,mediaNameCanvas.width, mediaNameCanvas.height);
100 var alphaJump = 0.01;
104 mediaNameCTX.fillStyle="rgba(30,30,30,0.5)";
105 mediaNameCTX.strokeStyle="rgba(130,130,130,1)";
106 mediaNameCTX.lineWidth = 5;
107 mediaNameCTX.fillRect(0,0,mediaNameCanvas.width, playBarHeight);
108 mediaNameCTX.strokeRect(-20,0,mediaNameCanvas.width + 40, playBarHeight);
110 if (newCover === undefined || newCover.naturalWidth === undefined || newCover.naturalWidth <= 0)
112 newCover = musicIcon;
115 mediaNameCTX.drawImage(newCover, 20, (playBarHeight - boxWidth) / 2, boxWidth, boxWidth);
117 if (screenOrientation === "portrait")
119 var textStartX = boxWidth + 50;
121 var trackText = new TextObject(mediaNameCTX,{"text" : newTitle, "xLoc" : textStartX, "yLoc" : 70 , "zLoc" : 0,
122 "width" : mediaNameCanvas.width - textStartX, "height" : 50, "lineHeight" : 65, "wordWrap" : true});
124 trackText.applyTemplate(mainMenuTitleTemplate);
126 var artistText = new TextObject(mediaNameCTX,{"text" : newArtist, "xLoc" : textStartX, "yLoc" : 70, "zLoc" : 0,
127 "width" : mediaNameCanvas.width - textStartX, "height" : 50, "lineHeight" : 50, "wordWrap" : true});
129 artistText.applyTemplate(mainTrackTemplate);
133 var textStartX = boxWidth + 50;
134 var trackText = new TextObject(mediaNameCTX,{"text" : newTitle, "xLoc" : textStartX, "yLoc" : 50 , "zLoc" : 0,
135 "width" : mediaNameCanvas.width - textStartX, "height" : 30, "lineHeight" : 30, "wordWrap" : true});
136 trackText.applyTemplate(mainMenuTitleTemplateLandscape);
137 var artistText = new TextObject(mediaNameCTX,{"text" : newArtist, "xLoc" : textStartX, "yLoc" : 50, "zLoc" : 0,
138 "width" : mediaNameCanvas.width - textStartX, "height" : 30, "lineHeight" : 30, "wordWrap" : true});
139 artistText.applyTemplate(mainTrackTemplateLandscape);
143 trackText.drawLargeShadow();
144 artistText.yLoc += trackText.height;
145 artistText.drawObj();
148 AudioPlayer.prototype.playLoadedMedia = function()
150 console.log("MediaPlayer in playLoadedMedia");
151 this.currentFileLoaded = true;
153 //Only store track to localStorage if it's a local file, and current menu is the Audio menu
154 if (!this.content[this.listIndex].remoteFile && localStorage.prevMenu === "mainMusicButton")
156 localStorage.prevAudioTrack = this.content[this.listIndex].contentURI;
160 localStorage.prevAudioTime = undefined;
161 localStorage.prevAudioTrack = undefined;
164 this.currentAudioContent = this.content[this.listIndex];
166 if (this.loadPrevAudio)
168 this.playerControls.currentTime = localStorage.prevAudioTime;
169 this.loadPrevAudio = false;
173 if (this.loadAndPlay)
176 this.loadAndPlay = false;
179 this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
180 this.currentFileLoaded = true;
184 AudioPlayer.prototype.sortByAlpha = function()
186 console.log("MediaPlayer in sortByAlpha");
188 this.emptyTimeouts();
190 this.content.sort(function (a,b)
192 var first = a.title.toLowerCase();
193 var second = b.title.toLowerCase();
203 this.fillMediaList();
206 AudioPlayer.prototype.sortByArtist = function()
208 console.log("MediaPlayer in sortByArtist");
210 this.emptyTimeouts();
212 this.content.sort(function (a,b)
214 var first = a.artists[0].toLowerCase();
215 var second = b.artists[0].toLowerCase();
225 this.fillMediaList();
228 AudioPlayer.prototype.sortByAlbum =function()
230 console.log("MediaPlayer in sortByAlbum");
232 this.emptyTimeouts();
234 this.content.sort(function (a,b)
236 var first = a.album.toLowerCase();
237 var second = b.album.toLowerCase();
247 this.fillMediaList();
250 AudioPlayer.prototype.onContentLoaded = function()
254 if (localStorage.prevAudioTrack && localStorage.prevAudioTrack !== "undefined")
256 for (var i = 0; i < this.content.length; i++)
259 if (this.content[i].contentURI === localStorage.prevAudioTrack)
262 console.log("MediaPlayer: Previous audio found, loading " + this.content[this.listIndex].contentURI);
263 this.loadPrevAudio = true;
264 this.fillMediaList();
271 console.log("MediaPlayer: No previous audio found, loading first");
273 this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
275 if (currentMenu === "audio")
276 this.fillMediaList();
281 for (var i = 0; i < this.content.length; i++)
283 var iconURI = (this.content[i].thumbnailURIs !== undefined && this.content[i].thumbnailURIs !== null) ? this.content[i].thumbnailURIs[0] : "images/musicIcon.png";
284 this.content[i].coverArtURI = iconURI;
291 console.log("MediaPlayer: Error when parsing audioContent");
295 AudioPlayer.prototype.redrawMediaName = function()
297 this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
300 AudioPlayer.prototype.makeListItem = function(j, k)
302 var canvasH = mediaListItemH * 0.95 ;
304 for (var i = j; (i < (j+k) && i < this.content.length); i++)
306 //Check if album art is done loading, if not draw default until it is
307 if (this.content[i].artists === undefined)
309 this.content[i].artists = new Array();
310 this.content[i].artists[0] = "Unknown";
313 var trackText = {"text" : this.content[i].title, "xLoc" : canvasH + 20, "yLoc" : canvasH / 2.5 , "zLoc" : 0};
314 var artistText = {"text" : this.content[i].artists[0], "xLoc" : canvasH + 20, "yLoc" : canvasH / 1.1 , "zLoc" : 0};
316 if (this.content[i].coverArt && this.content[i].coverArt.complete)
317 this.makeListBar(this.content[i].coverArt, i, artistText, trackText);
319 this.makeListBar(musicIcon, i, artistText, trackText);
321 // Set callback function for the new list item
322 $("#" + this.type + "CanvasNum" + i).click(function () {
325 audioPlayer.load($(this).parent().parent().index(), true);
329 console.log("MediaPlayer: load audio error " + err.message);