| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Overrided metadata worker's path. | 6 * Overrided metadata worker's path. |
| 7 * @type {string} | 7 * @type {string} |
| 8 */ | 8 */ |
| 9 ContentMetadataProvider.WORKER_SCRIPT = '/js/metadata_worker.js'; | 9 ContentMetadataProvider.WORKER_SCRIPT = '/js/metadata_worker.js'; |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * @param {Element} container Container element. | 12 * @param {Element} container Container element. |
| 13 * @constructor | 13 * @constructor |
| 14 */ | 14 */ |
| 15 function AudioPlayer(container) { | 15 function AudioPlayer(container) { |
| 16 this.container_ = container; | 16 this.container_ = container; |
| 17 this.volumeManager_ = new VolumeManagerWrapper( | 17 this.volumeManager_ = new VolumeManagerWrapper( |
| 18 VolumeManagerWrapper.NonNativeVolumeStatus.ENABLED); | 18 VolumeManagerWrapper.NonNativeVolumeStatus.ENABLED); |
| 19 this.metadataModel_ = MetadataModel.create(this.volumeManager_); | 19 this.metadataModel_ = MetadataModel.create(this.volumeManager_); |
| 20 this.selectedEntry_ = null; | 20 this.selectedEntry_ = null; |
| 21 this.invalidTracks_ = {}; | 21 this.invalidTracks_ = {}; |
| 22 | 22 |
| 23 this.model_ = new AudioPlayerModel(); | 23 this.model_ = new AudioPlayerModel(); |
| 24 Object.observe(this.model_, function(changes) { | 24 Object.observe(this.model_, function(changes) { |
| 25 for (var i = 0; i < changes.length; i++) { | 25 for (var i = 0; i < changes.length; i++) { |
| 26 var change = changes[i]; | 26 var change = changes[i]; |
| 27 if (change.name == 'expanded' && change.type == 'update') { | 27 if (change.name == 'expanded' && |
| 28 (change.type == 'add' || change.type == 'update')) { |
| 28 this.onModelExpandedChanged(change.oldValue, change.object.expanded); | 29 this.onModelExpandedChanged(change.oldValue, change.object.expanded); |
| 29 break; | 30 break; |
| 30 } | 31 } |
| 31 } | 32 } |
| 32 }.bind(this)); | 33 }.bind(this)); |
| 33 | 34 |
| 34 this.entries_ = []; | 35 this.entries_ = []; |
| 35 this.currentTrackIndex_ = -1; | 36 this.currentTrackIndex_ = -1; |
| 36 this.playlistGeneration_ = 0; | 37 this.playlistGeneration_ = 0; |
| 37 | 38 |
| 38 /** | 39 /** |
| 39 * Whether if the playlist is expanded or not. This value is changed by | 40 * Whether if the playlist is expanded or not. This value is changed by |
| 40 * this.syncExpanded(). | 41 * this.syncExpanded(). |
| 41 * True: expanded, false: collapsed, null: unset. | 42 * True: expanded, false: collapsed, null: unset. |
| 42 * | 43 * |
| 43 * @type {?boolean} | 44 * @type {?boolean} |
| 44 * @private | 45 * @private |
| 45 */ | 46 */ |
| 46 this.isExpanded_ = null; // Initial value is null. It'll be set in load(). | 47 this.isExpanded_ = null; // Initial value is null. It'll be set in load(). |
| 47 | 48 |
| 48 this.player_ = | 49 this.player_ = |
| 49 /** @type {AudioPlayerElement} */ (document.querySelector('audio-player')); | 50 /** @type {AudioPlayerElement} */ (document.querySelector('audio-player')); |
| 50 // TODO(yoshiki): Move tracks into the model. | 51 // TODO(yoshiki): Move tracks into the model. |
| 51 this.player_.tracks = []; | 52 this.player_.tracks = []; |
| 52 this.player_.model = this.model_; | 53 this.model_.initialize(function() { |
| 54 this.player_.model = this.model_; |
| 55 }.bind(this)); |
| 53 | 56 |
| 54 // Run asynchronously after an event of model change is delivered. | 57 // Run asynchronously after an event of model change is delivered. |
| 55 setTimeout(function() { | 58 setTimeout(function() { |
| 56 this.errorString_ = ''; | 59 this.errorString_ = ''; |
| 57 this.offlineString_ = ''; | 60 this.offlineString_ = ''; |
| 58 chrome.fileManagerPrivate.getStrings(function(strings) { | 61 chrome.fileManagerPrivate.getStrings(function(strings) { |
| 59 container.ownerDocument.title = strings['AUDIO_PLAYER_TITLE']; | 62 container.ownerDocument.title = strings['AUDIO_PLAYER_TITLE']; |
| 60 this.errorString_ = strings['AUDIO_ERROR']; | 63 this.errorString_ = strings['AUDIO_ERROR']; |
| 61 this.offlineString_ = strings['AUDIO_OFFLINE']; | 64 this.offlineString_ = strings['AUDIO_OFFLINE']; |
| 62 AudioPlayer.TrackInfo.DEFAULT_ARTIST = | 65 AudioPlayer.TrackInfo.DEFAULT_ARTIST = |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 */ | 112 */ |
| 110 AudioPlayer.prototype.load = function(playlist) { | 113 AudioPlayer.prototype.load = function(playlist) { |
| 111 this.playlistGeneration_++; | 114 this.playlistGeneration_++; |
| 112 this.currentTrackIndex_ = -1; | 115 this.currentTrackIndex_ = -1; |
| 113 | 116 |
| 114 // Save the app state, in case of restart. Make a copy of the object, so the | 117 // Save the app state, in case of restart. Make a copy of the object, so the |
| 115 // playlist member is not changed after entries are resolved. | 118 // playlist member is not changed after entries are resolved. |
| 116 window.appState = JSON.parse(JSON.stringify(playlist)); // cloning | 119 window.appState = JSON.parse(JSON.stringify(playlist)); // cloning |
| 117 util.saveAppState(); | 120 util.saveAppState(); |
| 118 | 121 |
| 119 this.isExpanded_ = this.model_.expanded; | 122 this.isExpanded_ = this.player_.expanded; |
| 120 | 123 |
| 121 // Resolving entries has to be done after the volume manager is initialized. | 124 // Resolving entries has to be done after the volume manager is initialized. |
| 122 this.volumeManager_.ensureInitialized(function() { | 125 this.volumeManager_.ensureInitialized(function() { |
| 123 util.URLsToEntries(playlist.items, function(entries) { | 126 util.URLsToEntries(playlist.items, function(entries) { |
| 124 this.entries_ = entries; | 127 this.entries_ = entries; |
| 125 | 128 |
| 126 var position = playlist.position || 0; | 129 var position = playlist.position || 0; |
| 127 var time = playlist.time || 0; | 130 var time = playlist.time || 0; |
| 128 | 131 |
| 129 if (this.entries_.length == 0) | 132 if (this.entries_.length == 0) |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 * Selects a new track to play. | 214 * Selects a new track to play. |
| 212 * @param {number} newTrack New track number. | 215 * @param {number} newTrack New track number. |
| 213 * @param {number} time New playback position (in second). | 216 * @param {number} time New playback position (in second). |
| 214 * @private | 217 * @private |
| 215 */ | 218 */ |
| 216 AudioPlayer.prototype.select_ = function(newTrack, time) { | 219 AudioPlayer.prototype.select_ = function(newTrack, time) { |
| 217 if (this.currentTrackIndex_ == newTrack) return; | 220 if (this.currentTrackIndex_ == newTrack) return; |
| 218 | 221 |
| 219 this.currentTrackIndex_ = newTrack; | 222 this.currentTrackIndex_ = newTrack; |
| 220 this.player_.currentTrackIndex = this.currentTrackIndex_; | 223 this.player_.currentTrackIndex = this.currentTrackIndex_; |
| 221 this.player_.audioController.time = time; | 224 this.player_.time = time; |
| 222 | 225 |
| 223 // Run asynchronously after an event of current track change is delivered. | 226 // Run asynchronously after an event of current track change is delivered. |
| 224 setTimeout(function() { | 227 setTimeout(function() { |
| 225 if (!window.appReopen) | 228 if (!window.appReopen) |
| 226 this.player_.audioElement.play(); | 229 this.player_.audioElement.play(); |
| 227 | 230 |
| 228 window.appState.position = this.currentTrackIndex_; | 231 window.appState.position = this.currentTrackIndex_; |
| 229 window.appState.time = 0; | 232 window.appState.time = 0; |
| 230 util.saveAppState(); | 233 util.saveAppState(); |
| 231 | 234 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 /** | 280 /** |
| 278 * Toggles the expanded mode when resizing. | 281 * Toggles the expanded mode when resizing. |
| 279 * | 282 * |
| 280 * @param {Event} event Resize event. | 283 * @param {Event} event Resize event. |
| 281 * @private | 284 * @private |
| 282 */ | 285 */ |
| 283 AudioPlayer.prototype.onResize_ = function(event) { | 286 AudioPlayer.prototype.onResize_ = function(event) { |
| 284 if (!this.isExpanded_ && | 287 if (!this.isExpanded_ && |
| 285 window.innerHeight >= AudioPlayer.EXPANDED_MODE_MIN_HEIGHT) { | 288 window.innerHeight >= AudioPlayer.EXPANDED_MODE_MIN_HEIGHT) { |
| 286 this.isExpanded_ = true; | 289 this.isExpanded_ = true; |
| 287 this.model_.expanded = true; | 290 this.player_.expanded = true; |
| 288 } else if (this.isExpanded_ && | 291 } else if (this.isExpanded_ && |
| 289 window.innerHeight < AudioPlayer.EXPANDED_MODE_MIN_HEIGHT) { | 292 window.innerHeight < AudioPlayer.EXPANDED_MODE_MIN_HEIGHT) { |
| 290 this.isExpanded_ = false; | 293 this.isExpanded_ = false; |
| 291 this.model_.expanded = false; | 294 this.player_.expanded = false; |
| 292 } | 295 } |
| 293 }; | 296 }; |
| 294 | 297 |
| 295 /** | 298 /** |
| 296 * Handles keydown event to open inspector with shortcut keys. | 299 * Handles keydown event to open inspector with shortcut keys. |
| 297 * | 300 * |
| 298 * @param {Event} event KeyDown event. | 301 * @param {Event} event KeyDown event. |
| 299 * @private | 302 * @private |
| 300 */ | 303 */ |
| 301 AudioPlayer.prototype.onKeyDown_ = function(event) { | 304 AudioPlayer.prototype.onKeyDown_ = function(event) { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 util.saveAppState(); | 379 util.saveAppState(); |
| 377 } | 380 } |
| 378 }; | 381 }; |
| 379 | 382 |
| 380 /** | 383 /** |
| 381 * @private | 384 * @private |
| 382 */ | 385 */ |
| 383 AudioPlayer.prototype.syncHeight_ = function() { | 386 AudioPlayer.prototype.syncHeight_ = function() { |
| 384 var targetHeight; | 387 var targetHeight; |
| 385 | 388 |
| 386 if (this.model_.expanded) { | 389 if (this.player_.expanded) { |
| 387 // Expanded. | 390 // Expanded. |
| 388 if (!this.lastExpandedHeight_ || | 391 if (!this.lastExpandedHeight_ || |
| 389 this.lastExpandedHeight_ < AudioPlayer.EXPANDED_MODE_MIN_HEIGHT) { | 392 this.lastExpandedHeight_ < AudioPlayer.EXPANDED_MODE_MIN_HEIGHT) { |
| 390 var expandedListHeight = | 393 var expandedListHeight = |
| 391 Math.min(this.entries_.length, AudioPlayer.DEFAULT_EXPANDED_ITEMS) * | 394 Math.min(this.entries_.length, AudioPlayer.DEFAULT_EXPANDED_ITEMS) * |
| 392 AudioPlayer.TRACK_HEIGHT; | 395 AudioPlayer.TRACK_HEIGHT; |
| 393 targetHeight = AudioPlayer.CONTROLS_HEIGHT + expandedListHeight; | 396 targetHeight = AudioPlayer.CONTROLS_HEIGHT + expandedListHeight; |
| 394 this.lastExpandedHeight_ = targetHeight; | 397 this.lastExpandedHeight_ = targetHeight; |
| 395 } else { | 398 } else { |
| 396 targetHeight = this.lastExpandedHeight_; | 399 targetHeight = this.lastExpandedHeight_; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 */ | 452 */ |
| 450 AudioPlayer.TrackInfo.prototype.setMetadata = function( | 453 AudioPlayer.TrackInfo.prototype.setMetadata = function( |
| 451 metadata, error) { | 454 metadata, error) { |
| 452 // TODO(yoshiki): Handle error in better way. | 455 // TODO(yoshiki): Handle error in better way. |
| 453 // TODO(yoshiki): implement artwork (metadata.thumbnail) | 456 // TODO(yoshiki): implement artwork (metadata.thumbnail) |
| 454 this.title = metadata.mediaTitle || this.getDefaultTitle(); | 457 this.title = metadata.mediaTitle || this.getDefaultTitle(); |
| 455 this.artist = error || metadata.mediaArtist || this.getDefaultArtist(); | 458 this.artist = error || metadata.mediaArtist || this.getDefaultArtist(); |
| 456 }; | 459 }; |
| 457 | 460 |
| 458 // Starts loading the audio player. | 461 // Starts loading the audio player. |
| 459 window.addEventListener('polymer-ready', function(e) { | 462 window.addEventListener('DOMContentLoaded', function(e) { |
| 460 AudioPlayer.load(); | 463 AudioPlayer.load(); |
| 461 }); | 464 }); |
| OLD | NEW |