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;
202 var trackTimeText = new TextObject(trackTimeCTX,{"text" : timeText, "xLoc" : textStartX, "yLoc" : 50, "zLoc" : 0,
203 "width" : mediaNameCanvas.width - textStartX, "height" : 20, "lineHeight" : 20, "wordWrap" : false});
205 trackTimeText.applyTemplate(timeTemplate);
206 trackTimeCTX.clearRect(0, 0, trackTimeCanvas.width, trackTimeCanvas.height);
208 trackTimeText.yLoc = ((playBarHeight - boxWidth) / 2) + boxWidth;
209 trackTimeText.drawObj();
213 * Plays the currently loaded media file
216 AudioPlayer.prototype.playLoadedMedia = function()
218 console.log("MediaPlayer in playLoadedMedia");
219 this.currentFileLoaded = true;
221 //Only store track to localStorage if it's a local file, and current menu is the Audio menu
222 if (!this.content[this.listIndex].remoteFile && localStorage.prevMenu === "mainMusicButton")
224 localStorage.prevAudioTrack = this.content[this.listIndex].contentURI;
228 localStorage.prevAudioTime = undefined;
229 localStorage.prevAudioTrack = undefined;
232 this.currentAudioContent = this.content[this.listIndex];
234 if (this.loadPrevAudio)
236 this.playerControls.currentTime = localStorage.prevAudioTime;
237 this.loadPrevAudio = false;
241 if (this.loadAndPlay)
244 this.loadAndPlay = false;
247 this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
248 this.currentFileLoaded = true;
252 * Sort the file list alphabetically
255 AudioPlayer.prototype.sortByAlpha = function()
257 console.log("MediaPlayer in sortByAlpha");
259 this.emptyTimeouts();
261 this.content.sort(function (a,b)
263 var first = a.title.toLowerCase();
264 var second = b.title.toLowerCase();
274 this.fillMediaList();
278 * Sort the file list by artist name
281 AudioPlayer.prototype.sortByArtist = function()
283 console.log("MediaPlayer in sortByArtist");
285 this.emptyTimeouts();
287 this.content.sort(function (a,b)
289 var first = a.artists[0].toLowerCase();
290 var second = b.artists[0].toLowerCase();
300 this.fillMediaList();
304 * Sort the file list by album name
307 AudioPlayer.prototype.sortByAlbum =function()
309 console.log("MediaPlayer in sortByAlbum");
311 this.emptyTimeouts();
313 this.content.sort(function (a,b)
315 var first = a.album.toLowerCase();
316 var second = b.album.toLowerCase();
326 this.fillMediaList();
330 * Callback for when the local content has been successfully read using tizen.content.
333 AudioPlayer.prototype.onContentLoaded = function()
337 if (localStorage.prevAudioTrack && localStorage.prevAudioTrack !== "undefined")
339 for (var i = 0; i < this.content.length; i++)
342 if (this.content[i].contentURI === localStorage.prevAudioTrack)
345 console.log("MediaPlayer: Previous audio found, loading " + this.content[this.listIndex].contentURI);
346 this.loadPrevAudio = true;
347 this.fillMediaList();
354 console.log("MediaPlayer: No previous audio found, loading first");
356 this.updateMediaName(this.content[this.listIndex].artists[0], this.content[this.listIndex].title, this.content[this.listIndex].coverArt);
358 if (currentMenu === "audio")
359 this.fillMediaList();
364 for (var i = 0; i < this.content.length; i++)
366 var iconURI = (this.content[i].thumbnailURIs !== undefined && this.content[i].thumbnailURIs !== null) ? this.content[i].thumbnailURIs[0] : "images/musicIcon.png";
367 this.content[i].coverArtURI = iconURI;
374 console.log("MediaPlayer: Error when parsing audioContent");
379 * Create and draw an item in the media list
382 AudioPlayer.prototype.makeListItem = function(j, k)
384 var canvasH = mediaListItemH * 0.95 ;
386 for (var i = j; (i < (j+k) && i < this.content.length); i++)
388 if (typeof this.content[i].title !== "string")
389 this.content[i].title = "Unknown";
391 //Check if album art is done loading, if not draw default until it is
392 if (this.content[i].artists === undefined)
394 this.content[i].artists = new Array();
395 this.content[i].artists[0] = "Unknown";
398 else if (typeof this.content[i].artists[0] !== "string")
399 this.content[i].artists[0] = "Unknown";
401 var trackText = {"text" : this.content[i].title, "xLoc" : canvasH + 20, "yLoc" : canvasH / 2.5 , "zLoc" : 0};
402 var artistText = {"text" : this.content[i].artists[0], "xLoc" : canvasH + 20, "yLoc" : canvasH / 1.1 , "zLoc" : 0};
404 if (this.content[i].coverArt && this.content[i].coverArt.complete && (this.content[i].coverArt.naturalWidth !== 0 || this.content[i].coverArt.naturalHeight !== 0) )
405 this.makeListBar(this.content[i].coverArt, i, artistText, trackText);
407 this.makeListBar(musicIcon, i, artistText, trackText);
409 // Set callback function for the new list item
410 $("#" + this.type + "CanvasNum" + i).click(function () {
413 audioPlayer.load($(this).parent().parent().index(), true);
417 console.log("MediaPlayer: load audio error " + err.message);