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);
91 //Only store track to localStorage if it's a local file, and current menu is the Audio menu
92 if (!this.content[this.listIndex].remoteFile && localStorage.prevMenu === "mainMusicButton")
94 localStorage.prevAudioTrack = this.content[this.listIndex].contentURI;
98 localStorage.prevAudioTime = undefined;
99 localStorage.prevAudioTrack = undefined;
102 this.playerControls.load();
105 AudioPlayer.prototype.updateMediaName = function(newArtist, newTitle, newCover)
107 var playBarHeight = mediaNameCanvas.height;
108 var boxWidth = playBarHeight * 0.75;
109 mediaNameCTX.clearRect(0,0,mediaNameCanvas.width, mediaNameCanvas.height);
112 var alphaJump = 0.01;
116 mediaNameCTX.fillStyle="rgba(30,30,30,0.5)";
117 mediaNameCTX.strokeStyle="rgba(130,130,130,1)";
118 mediaNameCTX.lineWidth = 5;
119 mediaNameCTX.fillRect(0,0,mediaNameCanvas.width, playBarHeight);
120 mediaNameCTX.strokeRect(-20,0,mediaNameCanvas.width + 40, playBarHeight);
122 if (newCover === undefined || newCover.naturalWidth === undefined || newCover.naturalWidth <= 0)
124 newCover = musicIcon;
127 mediaNameCTX.drawImage(newCover, 20, (playBarHeight - boxWidth) / 2, boxWidth, boxWidth);
129 if (screenOrientation === "portrait")
131 var textStartX = boxWidth + 50;
133 var trackText = new TextObject(mediaNameCTX,{"text" : newTitle, "xLoc" : textStartX, "yLoc" : 70 , "zLoc" : 0,
134 "width" : mediaNameCanvas.width - textStartX, "height" : 50, "lineHeight" : 65, "wordWrap" : true});
136 trackText.applyTemplate(mainMenuTitleTemplate);
138 var artistText = new TextObject(mediaNameCTX,{"text" : newArtist, "xLoc" : textStartX, "yLoc" : 70, "zLoc" : 0,
139 "width" : mediaNameCanvas.width - textStartX, "height" : 50, "lineHeight" : 50, "wordWrap" : true});
141 artistText.applyTemplate(mainTrackTemplate);
145 var textStartX = boxWidth + 50;
146 var trackText = new TextObject(mediaNameCTX,{"text" : newTitle, "xLoc" : textStartX, "yLoc" : 50 , "zLoc" : 0,
147 "width" : mediaNameCanvas.width - textStartX, "height" : 30, "lineHeight" : 30, "wordWrap" : true});
148 trackText.applyTemplate(mainMenuTitleTemplateLandscape);
149 var artistText = new TextObject(mediaNameCTX,{"text" : newArtist, "xLoc" : textStartX, "yLoc" : 50, "zLoc" : 0,
150 "width" : mediaNameCanvas.width - textStartX, "height" : 30, "lineHeight" : 30, "wordWrap" : true});
151 artistText.applyTemplate(mainTrackTemplateLandscape);
155 trackText.drawLargeShadow();
156 artistText.yLoc += trackText.height;
157 artistText.drawObj();
160 AudioPlayer.prototype.playLoadedMedia = function()
162 console.log("MediaPlayer in playLoadedMedia");
163 this.currentFileLoaded = true;
165 this.currentAudioContent = this.content[this.listIndex];
167 if (this.loadPrevAudio)
169 this.playerControls.currentTime = localStorage.prevAudioTime;
170 this.loadPrevAudio = false;
174 if (this.loadAndPlay)
177 this.loadAndPlay = false;
180 this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
181 this.currentFileLoaded = true;
185 AudioPlayer.prototype.sortByAlpha = function()
187 console.log("MediaPlayer in sortByAlpha");
189 this.emptyTimeouts();
191 this.content.sort(function (a,b)
193 var first = a.title.toLowerCase();
194 var second = b.title.toLowerCase();
204 this.fillMediaList();
207 AudioPlayer.prototype.sortByArtist = function()
209 console.log("MediaPlayer in sortByArtist");
211 this.emptyTimeouts();
213 this.content.sort(function (a,b)
215 var first = a.artists[0].toLowerCase();
216 var second = b.artists[0].toLowerCase();
226 this.fillMediaList();
229 AudioPlayer.prototype.sortByAlbum =function()
231 console.log("MediaPlayer in sortByAlbum");
233 this.emptyTimeouts();
235 this.content.sort(function (a,b)
237 var first = a.album.toLowerCase();
238 var second = b.album.toLowerCase();
248 this.fillMediaList();
251 AudioPlayer.prototype.onContentLoaded = function()
255 if (localStorage.prevAudioTrack && localStorage.prevAudioTrack !== "undefined")
257 for (var i = 0; i < this.content.length; i++)
260 if (this.content[i].contentURI === localStorage.prevAudioTrack)
263 console.log("MediaPlayer: Previous audio found, loading " + this.content[this.listIndex].contentURI);
264 this.loadPrevAudio = true;
265 this.fillMediaList();
272 console.log("MediaPlayer: No previous audio found, loading first");
274 this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
276 if (currentMenu === "audio")
277 this.fillMediaList();
282 for (var i = 0; i < this.content.length; i++)
284 var iconURI = (this.content[i].thumbnailURIs !== undefined && this.content[i].thumbnailURIs !== null) ? this.content[i].thumbnailURIs[0] : "images/musicIcon.png";
285 this.content[i].coverArtURI = iconURI;
292 console.log("MediaPlayer: Error when parsing audioContent");
296 AudioPlayer.prototype.redrawMediaName = function()
298 this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
301 AudioPlayer.prototype.makeListItem = function(j, k)
303 var canvasH = mediaListItemH * 0.95 ;
305 for (var i = j; (i < (j+k) && i < this.content.length); i++)
307 //Check if album art is done loading, if not draw default until it is
308 if (this.content[i].artists === undefined)
310 this.content[i].artists = new Array();
311 this.content[i].artists[0] = "Unknown";
314 var trackText = {"text" : this.content[i].title, "xLoc" : canvasH + 20, "yLoc" : canvasH / 2.5 , "zLoc" : 0};
315 var artistText = {"text" : this.content[i].artists[0], "xLoc" : canvasH + 20, "yLoc" : canvasH / 1.1 , "zLoc" : 0};
317 if (this.content[i].coverArt && this.content[i].coverArt.complete)
318 this.makeListBar(this.content[i].coverArt, i, artistText, trackText);
320 this.makeListBar(musicIcon, i, artistText, trackText);
322 // Set callback function for the new list item
323 $("#" + this.type + "CanvasNum" + i).click(function () {
326 audioPlayer.load($(this).parent().parent().index(), true);
330 console.log("MediaPlayer: load audio error " + err.message);