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
13 AudioPlayer = function()
15 this.clearAudioTimeInterval = undefined;
16 this.currentAudioContent = undefined;
20 * Play function for when play button is clicked
23 AudioPlayer.prototype.play = function()
25 console.log("MediaPlayer in AudioPlayer::play");
27 if (this.currentFileLoaded)
29 if (this.playerControls.paused)
31 this.playerControls.play();
33 //Start tracking the current time of the media. This is used to play from previous position on restart.
34 //Currently this isn't supported for remote files
35 if (!this.content[this.listIndex].remoteFile)
37 var timeoutFunction = function()
39 localStorage.prevAudioTime = this.playerControls.currentTime;
40 this.updateTrackTime();
43 this.clearAudioTimeInterval = setInterval(timeoutFunction.bind(this),500);
48 console.log(this.content[this.listIndex].artists[0] + " : " + this.content[this.listIndex].title + " can't play yet, it hasn't loaded");
52 * Pause function for when pause button is clicked
55 AudioPlayer.prototype.pause = function()
57 if (!this.playerControls.paused)
59 this.playerControls.pause();
60 clearInterval(this.clearAudioTimeInterval);
65 * Next function for when next button is clicked
68 AudioPlayer.prototype.next = function()
72 if (this.content.length > (this.listIndex + 1))
77 this.load(this.listIndex, true);
82 * Previous function for when previous button is clicked
85 AudioPlayer.prototype.previous = function()
89 if (this.listIndex > 0 )
92 this.listIndex = this.content.length - 1;
94 this.load(this.listIndex, true);
102 AudioPlayer.prototype.playing = function()
104 return !(this.playerControls.paused);
108 * Load the file located at the index. If play is true, play immediately once file is loaded
111 AudioPlayer.prototype.load = function(index, play)
113 this.listIndex = index;
114 this.loadAndPlay = play;
115 this.playerControls.pause();
116 this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
117 $("#audioSrc").attr("src", this.content[this.listIndex].contentURI);
118 this.playerControls.load();
122 * Redraws the media bar with the new artist, title, and cover album
125 AudioPlayer.prototype.updateMediaName = function(newArtist, newTitle, newCover)
127 playBarHeight = mediaNameCanvas.height;
128 boxWidth = playBarHeight * 0.75;
129 textStartX = boxWidth + 50;
130 songDurationSec = this.playerControls.duration;
131 mediaNameCTX.clearRect(0,0,mediaNameCanvas.width, mediaNameCanvas.height);
133 mediaNameCTX.fillStyle="rgba(30,30,30,0.5)";
134 mediaNameCTX.strokeStyle="rgba(130,130,130,1)";
135 mediaNameCTX.lineWidth = 5;
136 mediaNameCTX.fillRect(0,0,mediaNameCanvas.width, playBarHeight);
137 mediaNameCTX.strokeRect(-20,0,mediaNameCanvas.width + 40, playBarHeight);
139 if (newCover === undefined || newCover.naturalWidth === undefined || newCover.naturalWidth <= 0)
141 newCover = musicIcon;
144 mediaNameCTX.drawImage(newCover, 20, (playBarHeight - boxWidth) / 2, boxWidth, boxWidth);
146 if (screenOrientation === "portrait")
148 var trackText = new TextObject(mediaNameCTX,{"text" : newTitle, "xLoc" : textStartX, "yLoc" : 70 , "zLoc" : 0,
149 "width" : mediaNameCanvas.width - textStartX, "height" : 50, "lineHeight" : 65, "wordWrap" : true});
151 trackText.applyTemplate(mainMenuTitleTemplate);
153 mainArtistText = new TextObject(mediaNameCTX,{"text" : newArtist, "xLoc" : textStartX, "yLoc" : 70, "zLoc" : 0,
154 "width" : mediaNameCanvas.width - textStartX, "height" : 50, "lineHeight" : 50, "wordWrap" : true});
156 mainArtistText.applyTemplate(mainTrackTemplate);
160 var trackText = new TextObject(mediaNameCTX,{"text" : newTitle, "xLoc" : textStartX, "yLoc" : 50 , "zLoc" : 0,
161 "width" : mediaNameCanvas.width - textStartX, "height" : 30, "lineHeight" : 30, "wordWrap" : true});
162 trackText.applyTemplate(mainMenuTitleTemplateLandscape);
163 mainArtistText = new TextObject(mediaNameCTX,{"text" : newArtist, "xLoc" : textStartX, "yLoc" : 50, "zLoc" : 0,
164 "width" : mediaNameCanvas.width - textStartX, "height" : 30, "lineHeight" : 30, "wordWrap" : true});
165 mainArtistText.applyTemplate(mainTrackTemplateLandscape);
169 trackText.drawLargeShadow();
170 mainArtistText.yLoc += trackText.height;
171 mainArtistText.drawObj();
173 this.updateTrackTime();
177 * Redraws the current track time
180 AudioPlayer.prototype.updateTrackTime = function()
182 playBarHeight = mediaNameCanvas.height;
183 boxWidth = playBarHeight * 0.75;
184 textStartX = boxWidth + 50;
186 var songDurationSec = Math.floor(this.playerControls.duration % 60) > 9 ? Math.floor(this.playerControls.duration % 60) : '0' + Math.floor(this.playerControls.duration % 60);
187 var songDurationMin = Math.floor(this.playerControls.duration / 60) > 9 ? Math.floor(this.playerControls.duration / 60) : '0' + Math.floor(this.playerControls.duration / 60);
188 var songCurrentMin = Math.floor(this.playerControls.currentTime / 60) > 9 ? Math.floor(this.playerControls.currentTime / 60) : '0' + Math.floor(this.playerControls.currentTime / 60);
189 var songCurrentSec = Math.floor(this.playerControls.currentTime % 60) > 9 ? Math.floor(this.playerControls.currentTime % 60) : '0' + Math.floor(this.playerControls.currentTime % 60);
191 if ((songCurrentMin !== undefined && songCurrentSec !== undefined) && (!isNaN(songCurrentMin) && !isNaN(songCurrentSec)))
192 timeText += songCurrentMin + ':' + songCurrentSec + ' / ';
194 timeText += "00:00 / ";
196 if ((songDurationSec !== undefined && songDurationMin !== undefined) && (!isNaN(songDurationMin) && !isNaN(songDurationSec)))
197 timeText += songDurationMin + ':' + songDurationSec;
201 var trackTimeText = new TextObject(trackTimeCTX,{"text" : timeText, "xLoc" : textStartX, "yLoc" : 50, "zLoc" : 0,
202 "width" : mediaNameCanvas.width - textStartX, "height" : 20, "lineHeight" : 20, "wordWrap" : false});
204 if (screenOrientation === "portrait")
206 trackTimeText.applyTemplate(timeTemplate);
209 trackTimeText.applyTemplate(timeTemplateLandscape);
211 trackTimeCTX.clearRect(0, 0, trackTimeCanvas.width, trackTimeCanvas.height);
213 trackTimeText.yLoc = ((playBarHeight - boxWidth) / 2) + boxWidth;
214 trackTimeText.drawObj();
218 * Plays the currently loaded media file
221 AudioPlayer.prototype.playLoadedMedia = function()
223 console.log("MediaPlayer in playLoadedMedia");
224 this.currentFileLoaded = true;
226 //Only store track to localStorage if it's a local file, and current menu is the Audio menu
227 if (!this.content[this.listIndex].remoteFile && localStorage.prevMenu === "mainMusicButton")
229 localStorage.prevAudioTrack = this.content[this.listIndex].contentURI;
233 localStorage.prevAudioTime = undefined;
234 localStorage.prevAudioTrack = undefined;
237 this.currentAudioContent = this.content[this.listIndex];
239 if (this.loadPrevAudio)
241 this.playerControls.currentTime = localStorage.prevAudioTime;
242 this.loadPrevAudio = false;
246 if (this.loadAndPlay)
249 this.loadAndPlay = false;
252 this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
253 this.currentFileLoaded = true;
257 * Sort the file list alphabetically
260 AudioPlayer.prototype.sortByAlpha = function()
262 console.log("MediaPlayer in sortByAlpha");
264 this.emptyTimeouts();
266 this.content.sort(function (a,b)
268 var first = a.title.toLowerCase();
269 var second = b.title.toLowerCase();
279 this.fillMediaList();
283 * Sort the file list by artist name
286 AudioPlayer.prototype.sortByArtist = function()
288 console.log("MediaPlayer in sortByArtist");
290 this.emptyTimeouts();
292 this.content.sort(function (a,b)
294 var first = a.artists[0].toLowerCase();
295 var second = b.artists[0].toLowerCase();
305 this.fillMediaList();
309 * Sort the file list by album name
312 AudioPlayer.prototype.sortByAlbum =function()
314 console.log("MediaPlayer in sortByAlbum");
316 this.emptyTimeouts();
318 this.content.sort(function (a,b)
320 var first = a.album.toLowerCase();
321 var second = b.album.toLowerCase();
331 this.fillMediaList();
335 * Callback for when the local content has been successfully read using tizen.content.
338 AudioPlayer.prototype.onContentLoaded = function()
342 if (localStorage.prevAudioTrack && localStorage.prevAudioTrack !== "undefined")
344 for (var i = 0; i < this.content.length; i++)
347 if (this.content[i].contentURI === localStorage.prevAudioTrack)
350 console.log("MediaPlayer: Previous audio found, loading " + this.content[this.listIndex].contentURI);
351 this.loadPrevAudio = true;
352 this.fillMediaList();
359 console.log("MediaPlayer: No previous audio found, loading first");
361 this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
363 if (currentMenu === "audio")
364 this.fillMediaList();
369 for (var i = 0; i < this.content.length; i++)
371 var iconURI = (this.content[i].thumbnailURIs !== undefined && this.content[i].thumbnailURIs !== null) ? this.content[i].thumbnailURIs[0] : "images/musicIcon.png";
372 this.content[i].coverArtURI = iconURI;
379 console.log("MediaPlayer: Error when parsing audioContent");
384 * Create and draw an item in the media list
387 AudioPlayer.prototype.makeListItem = function(j, k)
389 var canvasH = mediaListItemH * 0.95 ;
391 for (var i = j; (i < (j+k) && i < this.content.length); i++)
393 if (typeof this.content[i].title !== "string")
394 this.content[i].title = "Unknown";
396 //Check if album art is done loading, if not draw default until it is
397 if (this.content[i].artists === undefined)
399 this.content[i].artists = new Array();
400 this.content[i].artists[0] = "Unknown";
403 else if (typeof this.content[i].artists[0] !== "string")
404 this.content[i].artists[0] = "Unknown";
406 var trackText = {"text" : this.content[i].title, "xLoc" : canvasH + 20, "yLoc" : canvasH / 2.5 , "zLoc" : 0};
407 var artistText = {"text" : this.content[i].artists[0], "xLoc" : canvasH + 20, "yLoc" : canvasH / 1.1 , "zLoc" : 0};
409 if (this.content[i].coverArt && this.content[i].coverArt.complete && (this.content[i].coverArt.naturalWidth !== 0 || this.content[i].coverArt.naturalHeight !== 0) )
410 this.makeListBar(this.content[i].coverArt, i, artistText, trackText);
412 this.makeListBar(musicIcon, i, artistText, trackText);
414 // Set callback function for the new list item
415 $("#" + this.type + "CanvasNum" + i).click(function () {
418 audioPlayer.load($(this).parent().parent().index(), true);
422 console.log("MediaPlayer: load audio error " + err.message);