1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 * TODO(mtomasz): Rewrite the entire audio player.
10 * @param {HTMLElement} container Container element.
13 function AudioPlayer(container) {
14 this.container_ = container;
15 this.currentTrack_ = -1;
16 this.playlistGeneration_ = 0;
17 this.selectedEntry_ = null;
18 this.volumeManager_ = new VolumeManagerWrapper(
19 VolumeManagerWrapper.DriveEnabledStatus.DRIVE_ENABLED);
20 this.metadataCache_ = MetadataCache.createFull(this.volumeManager_);
22 this.container_.classList.add('collapsed');
24 function createChild(opt_className, opt_tag) {
25 var child = container.ownerDocument.createElement(opt_tag || 'div');
27 child.className = opt_className;
28 container.appendChild(child);
32 // We create two separate containers (for expanded and compact view) and keep
33 // two sets of TrackInfo instances. We could fiddle with a single set instead
34 // but it would make keeping the list scroll position very tricky.
35 this.trackList_ = createChild('track-list');
36 this.trackStack_ = createChild('track-stack');
38 createChild('title-button collapse').addEventListener(
39 'click', this.onExpandCollapse_.bind(this));
41 this.audioControls_ = new FullWindowAudioControls(
42 createChild(), this.advance_.bind(this), this.onError_.bind(this));
43 this.audioControls_.attachMedia(createChild('', 'audio'));
45 chrome.fileBrowserPrivate.getStrings(function(strings) {
46 container.ownerDocument.title = strings['AUDIO_PLAYER_TITLE'];
47 this.errorString_ = strings['AUDIO_ERROR'];
48 this.offlineString_ = strings['AUDIO_OFFLINE'];
49 AudioPlayer.TrackInfo.DEFAULT_ARTIST =
50 strings['AUDIO_PLAYER_DEFAULT_ARTIST'];
53 this.volumeManager_.addEventListener('externally-unmounted',
54 this.onExternallyUnmounted_.bind(this));
56 window.addEventListener('resize', this.onResize_.bind(this));
58 // Show the window after DOM is processed.
59 var currentWindow = chrome.app.window.current();
60 setTimeout(currentWindow.show.bind(currentWindow), 0);
64 * Initial load method (static).
66 AudioPlayer.load = function() {
67 document.ondragstart = function(e) { e.preventDefault() };
69 AudioPlayer.instance =
70 new AudioPlayer(document.querySelector('.audio-player'));
71 AudioPlayer.instance.load(window.appState);
74 util.addPageLoadHandler(AudioPlayer.load);
80 if (AudioPlayer.instance)
81 AudioPlayer.instance.onUnload();
88 AudioPlayer.instance.load(window.appState);
92 * Load a new playlist.
93 * @param {Playlist} playlist Playlist object passed via mediaPlayerPrivate.
95 AudioPlayer.prototype.load = function(playlist) {
96 this.playlistGeneration_++;
97 this.audioControls_.pause();
98 this.currentTrack_ = -1;
100 // Save the app state, in case of restart. Make a copy of the object, so the
101 // playlist member is not changed after entries are resolved.
102 window.appState = JSON.parse(JSON.stringify(playlist));
105 // Resolving entries has to be done after the volume manager is initialized.
106 this.volumeManager_.ensureInitialized(function() {
107 util.URLsToEntries(playlist.items, function(entries) {
108 this.entries_ = entries;
109 this.invalidTracks_ = {};
110 this.cancelAutoAdvance_();
112 if (this.entries_.length <= 1)
113 this.container_.classList.add('single-track');
115 this.container_.classList.remove('single-track');
119 this.trackList_.textContent = '';
120 this.trackStack_.textContent = '';
122 this.trackListItems_ = [];
123 this.trackStackItems_ = [];
125 if (this.entries_.length == 0)
128 for (var i = 0; i != this.entries_.length; i++) {
129 var entry = this.entries_[i];
130 var onClick = this.select_.bind(this, i, false /* no restore */);
131 this.trackListItems_.push(
132 new AudioPlayer.TrackInfo(this.trackList_, entry, onClick));
133 this.trackStackItems_.push(
134 new AudioPlayer.TrackInfo(this.trackStack_, entry, onClick));
137 this.select_(playlist.position, !!playlist.time);
139 // This class will be removed if at least one track has art.
140 this.container_.classList.add('noart');
142 // Load the selected track metadata first, then load the rest.
143 this.loadMetadata_(playlist.position);
144 for (i = 0; i != this.entries_.length; i++) {
145 if (i != playlist.position)
146 this.loadMetadata_(i);
153 * Load metadata for a track.
154 * @param {number} track Track number.
157 AudioPlayer.prototype.loadMetadata_ = function(track) {
159 this.entries_[track], this.displayMetadata_.bind(this, track));
163 * Display track's metadata.
164 * @param {number} track Track number.
165 * @param {Object} metadata Metadata object.
166 * @param {string=} opt_error Error message.
169 AudioPlayer.prototype.displayMetadata_ = function(track, metadata, opt_error) {
170 this.trackListItems_[track].
171 setMetadata(metadata, this.container_, opt_error);
172 this.trackStackItems_[track].
173 setMetadata(metadata, this.container_, opt_error);
177 * Closes audio player when a volume containing the selected item is unmounted.
178 * @param {Event} event The unmount event.
181 AudioPlayer.prototype.onExternallyUnmounted_ = function(event) {
182 if (!this.selectedEntry_)
185 if (this.volumeManager_.getVolumeInfo(this.selectedEntry_) ===
192 * Called on window is being unloaded.
194 AudioPlayer.prototype.onUnload = function() {
195 this.audioControls_.cleanup();
196 this.volumeManager_.dispose();
200 * Select a new track to play.
201 * @param {number} newTrack New track number.
202 * @param {boolean=} opt_restoreState True if restoring the play state from URL.
205 AudioPlayer.prototype.select_ = function(newTrack, opt_restoreState) {
206 if (this.currentTrack_ == newTrack) return;
208 this.changeSelectionInList_(this.currentTrack_, newTrack);
209 this.changeSelectionInStack_(this.currentTrack_, newTrack);
211 this.currentTrack_ = newTrack;
213 window.appState.position = this.currentTrack_;
214 window.appState.time = 0;
217 this.scrollToCurrent_(false);
219 var currentTrack = this.currentTrack_;
220 var entry = this.entries_[currentTrack];
221 this.fetchMetadata_(entry, function(metadata) {
222 if (this.currentTrack_ != currentTrack)
224 this.audioControls_.load(entry, opt_restoreState);
226 // Resolve real filesystem path of the current audio file.
227 this.selectedEntry_ = entry;
232 * @param {Entry} entry Track file entry.
233 * @param {function(object)} callback Callback.
236 AudioPlayer.prototype.fetchMetadata_ = function(entry, callback) {
237 this.metadataCache_.get(entry, 'thumbnail|media|streaming',
238 function(generation, metadata) {
239 // Do nothing if another load happened since the metadata request.
240 if (this.playlistGeneration_ == generation)
242 }.bind(this, this.playlistGeneration_));
246 * @param {number} oldTrack Old track number.
247 * @param {number} newTrack New track number.
250 AudioPlayer.prototype.changeSelectionInList_ = function(oldTrack, newTrack) {
251 this.trackListItems_[newTrack].getBox().classList.add('selected');
254 this.trackListItems_[oldTrack].getBox().classList.remove('selected');
259 * @param {number} oldTrack Old track number.
260 * @param {number} newTrack New track number.
263 AudioPlayer.prototype.changeSelectionInStack_ = function(oldTrack, newTrack) {
264 var newBox = this.trackStackItems_[newTrack].getBox();
265 newBox.classList.add('selected'); // Put on top immediately.
266 newBox.classList.add('visible'); // Start fading in.
269 var oldBox = this.trackStackItems_[oldTrack].getBox();
270 oldBox.classList.remove('selected'); // Put under immediately.
271 setTimeout(function() {
272 if (!oldBox.classList.contains('selected')) {
273 // This will start fading out which is not really necessary because
274 // oldBox is already completely obscured by newBox.
275 oldBox.classList.remove('visible');
282 * Scrolls the current track into the viewport.
284 * @param {boolean} keepAtBottom If true, make the selected track the last
285 * of the visible (if possible). If false, perform minimal scrolling.
288 AudioPlayer.prototype.scrollToCurrent_ = function(keepAtBottom) {
289 var box = this.trackListItems_[this.currentTrack_].getBox();
290 this.trackList_.scrollTop = Math.max(
291 keepAtBottom ? 0 : Math.min(box.offsetTop, this.trackList_.scrollTop),
292 box.offsetTop + box.offsetHeight - this.trackList_.clientHeight);
296 * @return {boolean} True if the player is be displayed in compact mode.
299 AudioPlayer.prototype.isCompact_ = function() {
300 return this.container_.classList.contains('collapsed') ||
301 this.container_.classList.contains('single-track');
305 * Go to the previous or the next track.
306 * @param {boolean} forward True if next, false if previous.
307 * @param {boolean=} opt_onlyIfValid True if invalid tracks should be selected.
310 AudioPlayer.prototype.advance_ = function(forward, opt_onlyIfValid) {
311 this.cancelAutoAdvance_();
313 var newTrack = this.currentTrack_ + (forward ? 1 : -1);
314 if (newTrack < 0) newTrack = this.entries_.length - 1;
315 if (newTrack == this.entries_.length) newTrack = 0;
316 if (opt_onlyIfValid && this.invalidTracks_[newTrack])
318 this.select_(newTrack);
322 * Media error handler.
325 AudioPlayer.prototype.onError_ = function() {
326 var track = this.currentTrack_;
328 this.invalidTracks_[track] = true;
331 this.entries_[track],
333 var error = (!navigator.onLine && metadata.streaming) ?
334 this.offlineString_ : this.errorString_;
335 this.displayMetadata_(track, metadata, error);
336 this.scheduleAutoAdvance_();
341 * Schedule automatic advance to the next track after a timeout.
344 AudioPlayer.prototype.scheduleAutoAdvance_ = function() {
345 this.cancelAutoAdvance_();
346 this.autoAdvanceTimer_ = setTimeout(
348 this.autoAdvanceTimer_ = null;
349 // We are advancing only if the next track is not known to be invalid.
350 // This prevents an endless auto-advancing in the case when all tracks
351 // are invalid (we will only visit each track once).
352 this.advance_(true /* forward */, true /* only if valid */);
358 * Cancel the scheduled auto advance.
361 AudioPlayer.prototype.cancelAutoAdvance_ = function() {
362 if (this.autoAdvanceTimer_) {
363 clearTimeout(this.autoAdvanceTimer_);
364 this.autoAdvanceTimer_ = null;
369 * Expand/collapse button click handler. Toggles the mode and updates the
370 * height of the window.
374 AudioPlayer.prototype.onExpandCollapse_ = function() {
375 if (!this.isCompact_()) {
376 this.setExpanded_(false);
377 this.lastExpandedHeight_ = window.innerHeight;
379 this.setExpanded_(true);
385 * Toggles the current expand mode.
387 * @param {boolean} on True if on, false otherwise.
390 AudioPlayer.prototype.setExpanded_ = function(on) {
392 this.container_.classList.remove('collapsed');
393 this.scrollToCurrent_(true);
395 this.container_.classList.add('collapsed');
400 * Toggles the expanded mode when resizing.
402 * @param {Event} event Resize event.
405 AudioPlayer.prototype.onResize_ = function(event) {
406 if (this.isCompact_() &&
407 window.innerHeight >= AudioPlayer.EXPANDED_MODE_MIN_HEIGHT) {
408 this.setExpanded_(true);
409 } else if (!this.isCompact_() &&
410 window.innerHeight < AudioPlayer.EXPANDED_MODE_MIN_HEIGHT) {
411 this.setExpanded_(false);
415 /* Keep the below constants in sync with the CSS. */
418 * Window header size in pixels.
422 AudioPlayer.HEADER_HEIGHT = 28;
425 * Track height in pixels.
429 AudioPlayer.TRACK_HEIGHT = 58;
432 * Controls bar height in pixels.
436 AudioPlayer.CONTROLS_HEIGHT = 35;
439 * Default number of items in the expanded mode.
443 AudioPlayer.DEFAULT_EXPANDED_ITEMS = 5;
446 * Minimum size of the window in the expanded mode in pixels.
450 AudioPlayer.EXPANDED_MODE_MIN_HEIGHT = AudioPlayer.CONTROLS_HEIGHT +
451 AudioPlayer.TRACK_HEIGHT * 2;
454 * Set the correct player window height.
457 AudioPlayer.prototype.syncHeight_ = function() {
460 if (!this.isCompact_()) {
462 if (this.lastExpandedHeight_) {
463 targetHeight = this.lastExpandedHeight_;
465 var expandedListHeight =
466 Math.min(this.entries_.length, AudioPlayer.DEFAULT_EXPANDED_ITEMS) *
467 AudioPlayer.TRACK_HEIGHT;
468 targetHeight = AudioPlayer.CONTROLS_HEIGHT + expandedListHeight;
472 targetHeight = AudioPlayer.CONTROLS_HEIGHT + AudioPlayer.TRACK_HEIGHT;
475 window.resizeTo(window.innerWidth, targetHeight + AudioPlayer.HEADER_HEIGHT);
479 * Create a TrackInfo object encapsulating the information about one track.
481 * @param {HTMLElement} container Container element.
482 * @param {Entry} entry Track entry.
483 * @param {function} onClick Click handler.
486 AudioPlayer.TrackInfo = function(container, entry, onClick) {
489 var doc = container.ownerDocument;
491 this.box_ = doc.createElement('div');
492 this.box_.className = 'track';
493 this.box_.addEventListener('click', onClick);
494 container.appendChild(this.box_);
496 this.art_ = doc.createElement('div');
497 this.art_.className = 'art blank';
498 this.box_.appendChild(this.art_);
500 this.img_ = doc.createElement('img');
501 this.art_.appendChild(this.img_);
503 this.data_ = doc.createElement('div');
504 this.data_.className = 'data';
505 this.box_.appendChild(this.data_);
507 this.title_ = doc.createElement('div');
508 this.title_.className = 'data-title';
509 this.data_.appendChild(this.title_);
511 this.artist_ = doc.createElement('div');
512 this.artist_.className = 'data-artist';
513 this.data_.appendChild(this.artist_);
517 * @return {HTMLDivElement} The wrapper element for the track.
519 AudioPlayer.TrackInfo.prototype.getBox = function() { return this.box_ };
522 * @return {string} Default track title (file name extracted from the entry).
524 AudioPlayer.TrackInfo.prototype.getDefaultTitle = function() {
525 // TODO(mtomasz): Reuse ImageUtil.getDisplayNameFromName().
526 var name = this.entry_.name;
527 var dotIndex = name.lastIndexOf('.');
528 var title = dotIndex >= 0 ? name.substr(0, dotIndex) : name;
533 * TODO(kaznacheev): Localize.
535 AudioPlayer.TrackInfo.DEFAULT_ARTIST = 'Unknown Artist';
538 * @return {string} 'Unknown artist' string.
540 AudioPlayer.TrackInfo.prototype.getDefaultArtist = function() {
541 return AudioPlayer.TrackInfo.DEFAULT_ARTIST;
545 * @param {Object} metadata The metadata object.
546 * @param {HTMLElement} container The container for the tracks.
547 * @param {string} error Error string.
549 AudioPlayer.TrackInfo.prototype.setMetadata = function(
550 metadata, container, error) {
552 this.art_.classList.add('blank');
553 this.art_.classList.add('error');
554 container.classList.remove('noart');
555 } else if (metadata.thumbnail && metadata.thumbnail.url) {
556 this.img_.onload = function() {
557 // Only display the image if the thumbnail loaded successfully.
558 this.art_.classList.remove('blank');
559 container.classList.remove('noart');
561 this.img_.src = metadata.thumbnail.url;
563 this.title_.textContent = (metadata.media && metadata.media.title) ||
564 this.getDefaultTitle();
565 this.artist_.textContent = error ||
566 (metadata.media && metadata.media.artist) || this.getDefaultArtist();
570 * Audio controls specific for the Audio Player.
572 * @param {HTMLElement} container Parent container.
573 * @param {function(boolean)} advanceTrack Parameter: true=forward.
574 * @param {function} onError Error handler.
577 function FullWindowAudioControls(container, advanceTrack, onError) {
578 AudioControls.apply(this, arguments);
580 document.addEventListener('keydown', function(e) {
581 if (e.keyIdentifier == 'U+0020') {
582 this.togglePlayState();
588 FullWindowAudioControls.prototype = { __proto__: AudioControls.prototype };
591 * Enable play state restore from the location hash.
592 * @param {FileEntry} entry Source Entry.
593 * @param {boolean} restore True if need to restore the play state.
595 FullWindowAudioControls.prototype.load = function(entry, restore) {
596 this.media_.src = entry.toURL();
598 this.restoreWhenLoaded_ = restore;
602 * Save the current state so that it survives page/app reload.
604 FullWindowAudioControls.prototype.onPlayStateChanged = function() {
609 * Restore the state after page/app reload.
611 FullWindowAudioControls.prototype.restorePlayState = function() {
612 if (this.restoreWhenLoaded_) {
613 this.restoreWhenLoaded_ = false; // This should only work once.
614 if (this.decodeState())