3081f7877f9e4329bb8f92f91fa44885bf23f95e
[profile/ivi/sdk/web-sample-build.git] / samples / web / Sample / Tizen / Web App / MediaPlayer / project / 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         this.playerControls.load();
91 }
92
93 AudioPlayer.prototype.updateMediaName = function(newArtist, newTitle, newCover)
94 {
95         var playBarHeight = mediaNameCanvas.height;
96         var boxWidth = playBarHeight * 0.75;
97         mediaNameCTX.clearRect(0,0,mediaNameCanvas.width, mediaNameCanvas.height);
98
99         var shadeJump = 10;
100         var alphaJump = 0.01;
101         var currColor = 255;
102         var currAlpha = 1.0;
103
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);
109
110         if (newCover === undefined || newCover.naturalWidth === undefined || newCover.naturalWidth <= 0)
111         {
112                 newCover = musicIcon;
113         }
114
115         mediaNameCTX.drawImage(newCover, 20, (playBarHeight - boxWidth) / 2, boxWidth, boxWidth);
116
117         if (screenOrientation === "portrait")
118         {
119                 var textStartX = boxWidth + 50;
120
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});
123
124                 trackText.applyTemplate(mainMenuTitleTemplate);
125
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});
128
129                 artistText.applyTemplate(mainTrackTemplate);
130         }
131         else
132         {
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);
140         }
141
142         trackText.drawObj();
143         trackText.drawLargeShadow();
144         artistText.yLoc += trackText.height;
145         artistText.drawObj();
146 }
147
148 AudioPlayer.prototype.playLoadedMedia = function()
149 {
150         console.log("MediaPlayer in playLoadedMedia");
151         this.currentFileLoaded = true;
152
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")
155         {
156                 localStorage.prevAudioTrack = this.content[this.listIndex].contentURI;
157         }
158         else
159         {
160                 localStorage.prevAudioTime = undefined;
161                 localStorage.prevAudioTrack = undefined;
162         }
163
164         this.currentAudioContent = this.content[this.listIndex];
165
166         if (this.loadPrevAudio)
167         {
168                 this.playerControls.currentTime = localStorage.prevAudioTime;
169                 this.loadPrevAudio = false;
170                 this.play();
171         }
172
173         if (this.loadAndPlay)
174         {
175                 this.play();
176                 this.loadAndPlay = false;
177         }
178
179         this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
180         this.currentFileLoaded = true;
181 }
182
183
184 AudioPlayer.prototype.sortByAlpha = function()
185 {
186         console.log("MediaPlayer in sortByAlpha");
187
188         this.emptyTimeouts();
189
190         this.content.sort(function (a,b)
191         {
192                 var first = a.title.toLowerCase();
193                 var second = b.title.toLowerCase();
194
195                 if (first < second)
196                         return -1;
197                 if (first > second)
198                         return 1;
199
200                 return 0;
201         });
202
203         this.fillMediaList();
204 }
205
206 AudioPlayer.prototype.sortByArtist = function()
207 {
208         console.log("MediaPlayer in sortByArtist");
209
210         this.emptyTimeouts();
211
212         this.content.sort(function (a,b)
213         {
214                 var first = a.artists[0].toLowerCase();
215                 var second = b.artists[0].toLowerCase();
216
217                 if (first < second)
218                         return -1;
219                 if (first > second)
220                         return 1;
221
222                 return 0;
223         });
224
225         this.fillMediaList();
226 }
227
228 AudioPlayer.prototype.sortByAlbum =function()
229 {
230         console.log("MediaPlayer in sortByAlbum");
231
232         this.emptyTimeouts();
233
234         this.content.sort(function (a,b)
235         {
236                 var first = a.album.toLowerCase();
237                 var second = b.album.toLowerCase();
238
239                 if (first < second)
240                         return -1;
241                 if (first > second)
242                         return 1;
243
244                 return 0;
245         });
246
247         this.fillMediaList();
248 }
249
250 AudioPlayer.prototype.onContentLoaded = function()
251 {
252         try
253         {
254                 if (localStorage.prevAudioTrack && localStorage.prevAudioTrack !== "undefined")
255                 {
256                         for (var i = 0; i < this.content.length; i++)
257                         {
258
259                                 if (this.content[i].contentURI === localStorage.prevAudioTrack)
260                                 {
261                                         this.listIndex = i;
262                                         console.log("MediaPlayer: Previous audio found, loading " + this.content[this.listIndex].contentURI);
263                                         this.loadPrevAudio = true;
264                                         this.fillMediaList();
265                                         this.load(i, true);
266                                 }
267                         }
268                 }
269                 else
270                 {
271                         console.log("MediaPlayer: No previous audio found, loading first");
272                         this.load(0, false);
273                         this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
274
275                         if (currentMenu === "audio")
276                                 this.fillMediaList();
277                 }
278
279                 var imgSources = [];
280
281                 for (var i = 0; i < this.content.length; i++)
282                 {
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;
285                 }
286
287                 loadImages();
288         }
289         catch (err)
290         {
291                 console.log("MediaPlayer: Error when parsing audioContent");
292         }
293 }
294
295 AudioPlayer.prototype.redrawMediaName = function()
296 {
297         this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
298 }
299
300 AudioPlayer.prototype.makeListItem = function(j, k)
301 {
302         var canvasH = mediaListItemH * 0.95 ;
303
304     for (var i = j; (i < (j+k) && i < this.content.length); i++)
305     {
306             //Check if album art is done loading, if not draw default until it is
307              if (this.content[i].artists === undefined)
308              {
309                 this.content[i].artists = new Array();
310                 this.content[i].artists[0] = "Unknown";
311              }
312
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};
315
316             if (this.content[i].coverArt && this.content[i].coverArt.complete)
317                 this.makeListBar(this.content[i].coverArt, i, artistText, trackText);
318             else
319                 this.makeListBar(musicIcon, i, artistText, trackText);
320
321                 // Set callback function for the new list item
322                 $("#" + this.type + "CanvasNum" + i).click(function () {
323                         try
324                         {
325                                 audioPlayer.load($(this).parent().parent().index(), true);
326                         }
327                         catch(err)
328                         {
329                                 console.log("MediaPlayer: load audio error " + err.message);
330                         }
331                 });
332     }
333 }