Image player will now remember what image it was last on
[profile/ivi/MediaPlayer.git] / js / audioPlayer.js
1 /*
2  * Copyright (c) 2013, Intel Corporation.
3  *
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
7  *
8  */
9
10 AudioPlayer = function()
11 {
12         this.clearAudioTimeInterval = undefined;
13         this.currentAudioContent = undefined;
14 }
15
16 AudioPlayer.prototype.play = function()
17 {
18         console.log("MediaPlayer in AudioPlayer::play");
19
20         if (this.currentFileLoaded)
21         {
22                 if (this.playerControls.paused)
23                 {
24                         this.playerControls.play();
25
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)
29                         {
30                                 var timeoutFunction = function()
31                                 {
32                                         localStorage.prevAudioTime = this.playerControls.currentTime;
33                                 }
34
35                                 this.clearAudioTimeInterval = setInterval(timeoutFunction.bind(this),500);
36                         }
37                 }
38         }
39         else
40                 console.log(this.content[this.listIndex].artists[0] + " : " + this.content[this.listIndex].title + " can't play yet, it hasn't loaded");
41 }
42
43 AudioPlayer.prototype.pause = function()
44 {
45         if (!this.playerControls.paused)
46         {
47                 this.playerControls.pause();
48                 clearInterval(this.clearAudioTimeInterval);
49         }
50 }
51
52 AudioPlayer.prototype.playing = function()
53 {
54         return !(this.playerControls.paused);
55 }
56
57 AudioPlayer.prototype.next = function()
58 {
59         if (this.content)
60         {
61                 if (this.content.length > (this.listIndex + 1))
62                         this.listIndex++;
63                 else
64                         this.listIndex = 0;
65
66                 this.load(this.listIndex, true);
67         }
68 }
69
70 AudioPlayer.prototype.previous = function()
71 {
72         if (this.content)
73         {
74                 if (this.listIndex > 0 )
75                         this.listIndex--;
76                 else
77                         this.listIndex = this.content.length - 1;
78
79                 this.load(this.listIndex, true);
80         }
81 }
82
83 AudioPlayer.prototype.load = function(index, play)
84 {
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
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")
93         {
94                 localStorage.prevAudioTrack = this.content[this.listIndex].contentURI;
95         }
96         else
97         {
98                 localStorage.prevAudioTime = undefined;
99                 localStorage.prevAudioTrack = undefined;
100         }
101
102         this.playerControls.load();
103 }
104
105 AudioPlayer.prototype.updateMediaName = function(newArtist, newTitle, newCover)
106 {
107         var playBarHeight = mediaNameCanvas.height;
108         var boxWidth = playBarHeight * 0.75;
109         mediaNameCTX.clearRect(0,0,mediaNameCanvas.width, mediaNameCanvas.height);
110
111         var shadeJump = 10;
112         var alphaJump = 0.01;
113         var currColor = 255;
114         var currAlpha = 1.0;
115
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);
121
122         if (newCover === undefined || newCover.naturalWidth === undefined || newCover.naturalWidth <= 0)
123         {
124                 newCover = musicIcon;
125         }
126
127         mediaNameCTX.drawImage(newCover, 20, (playBarHeight - boxWidth) / 2, boxWidth, boxWidth);
128
129         if (screenOrientation === "portrait")
130         {
131                 var textStartX = boxWidth + 50;
132
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});
135
136                 trackText.applyTemplate(mainMenuTitleTemplate);
137
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});
140
141                 artistText.applyTemplate(mainTrackTemplate);
142         }
143         else
144         {
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);
152         }
153
154         trackText.drawObj();
155         trackText.drawLargeShadow();
156         artistText.yLoc += trackText.height;
157         artistText.drawObj();
158 }
159
160 AudioPlayer.prototype.playLoadedMedia = function()
161 {
162         console.log("MediaPlayer in playLoadedMedia");
163         this.currentFileLoaded = true;
164
165         this.currentAudioContent = this.content[this.listIndex];
166
167         if (this.loadPrevAudio)
168         {
169                 this.playerControls.currentTime = localStorage.prevAudioTime;
170                 this.loadPrevAudio = false;
171                 this.play();
172         }
173
174         if (this.loadAndPlay)
175         {
176                 this.play();
177                 this.loadAndPlay = false;
178         }
179
180         this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
181         this.currentFileLoaded = true;
182 }
183
184
185 AudioPlayer.prototype.sortByAlpha = function()
186 {
187         console.log("MediaPlayer in sortByAlpha");
188
189         this.emptyTimeouts();
190
191         this.content.sort(function (a,b)
192         {
193                 var first = a.title.toLowerCase();
194                 var second = b.title.toLowerCase();
195
196                 if (first < second)
197                         return -1;
198                 if (first > second)
199                         return 1;
200
201                 return 0;
202         });
203
204         this.fillMediaList();
205 }
206
207 AudioPlayer.prototype.sortByArtist = function()
208 {
209         console.log("MediaPlayer in sortByArtist");
210
211         this.emptyTimeouts();
212
213         this.content.sort(function (a,b)
214         {
215                 var first = a.artists[0].toLowerCase();
216                 var second = b.artists[0].toLowerCase();
217
218                 if (first < second)
219                         return -1;
220                 if (first > second)
221                         return 1;
222
223                 return 0;
224         });
225
226         this.fillMediaList();
227 }
228
229 AudioPlayer.prototype.sortByAlbum =function()
230 {
231         console.log("MediaPlayer in sortByAlbum");
232
233         this.emptyTimeouts();
234
235         this.content.sort(function (a,b)
236         {
237                 var first = a.album.toLowerCase();
238                 var second = b.album.toLowerCase();
239
240                 if (first < second)
241                         return -1;
242                 if (first > second)
243                         return 1;
244
245                 return 0;
246         });
247
248         this.fillMediaList();
249 }
250
251 AudioPlayer.prototype.onContentLoaded = function()
252 {
253         try
254         {
255                 if (localStorage.prevAudioTrack && localStorage.prevAudioTrack !== "undefined")
256                 {
257                         for (var i = 0; i < this.content.length; i++)
258                         {
259
260                                 if (this.content[i].contentURI === localStorage.prevAudioTrack)
261                                 {
262                                         this.listIndex = i;
263                                         console.log("MediaPlayer: Previous audio found, loading " + this.content[this.listIndex].contentURI);
264                                         this.loadPrevAudio = true;
265                                         this.fillMediaList();
266                                         this.load(i, true);
267                                 }
268                         }
269                 }
270                 else
271                 {
272                         console.log("MediaPlayer: No previous audio found, loading first");
273                         this.load(0, false);
274                         this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
275
276                         if (currentMenu === "audio")
277                                 this.fillMediaList();
278                 }
279
280                 var imgSources = [];
281
282                 for (var i = 0; i < this.content.length; i++)
283                 {
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;
286                 }
287
288                 loadImages();
289         }
290         catch (err)
291         {
292                 console.log("MediaPlayer: Error when parsing audioContent");
293         }
294 }
295
296 AudioPlayer.prototype.redrawMediaName = function()
297 {
298         this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
299 }
300
301 AudioPlayer.prototype.makeListItem = function(j, k)
302 {
303         var canvasH = mediaListItemH * 0.95 ;
304
305     for (var i = j; (i < (j+k) && i < this.content.length); i++)
306     {
307             //Check if album art is done loading, if not draw default until it is
308              if (this.content[i].artists === undefined)
309              {
310                 this.content[i].artists = new Array();
311                 this.content[i].artists[0] = "Unknown";
312              }
313
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};
316
317             if (this.content[i].coverArt && this.content[i].coverArt.complete)
318                 this.makeListBar(this.content[i].coverArt, i, artistText, trackText);
319             else
320                 this.makeListBar(musicIcon, i, artistText, trackText);
321
322                 // Set callback function for the new list item
323                 $("#" + this.type + "CanvasNum" + i).click(function () {
324                         try
325                         {
326                                 audioPlayer.load($(this).parent().parent().index(), true);
327                         }
328                         catch(err)
329                         {
330                                 console.log("MediaPlayer: load audio error " + err.message);
331                         }
332                 });
333     }
334 }