Commit 6f868d6a authored by mtomasz@chromium.org's avatar mtomasz@chromium.org

Play automatically when reloading the audio player with new songs.

Because of introducing an async call during initialization, the new appstate was being changed. Playing a song adds a 'time' field to the appstate. Having a time field causes pausing the audio player once the initialization is finished.

To avoid this, a copy is made, to avoid altering the input 'playlist' object.

TEST=Tested manually. Partly browser tests.
BUG=335422

Review URL: https://codereview.chromium.org/141273004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@245493 0039d316-1c4b-4281-b951-d872f2087c98
parent 7c0c67a3
...@@ -40,7 +40,6 @@ function AudioPlayer(container) { ...@@ -40,7 +40,6 @@ function AudioPlayer(container) {
this.audioControls_ = new FullWindowAudioControls( this.audioControls_ = new FullWindowAudioControls(
createChild(), this.advance_.bind(this), this.onError_.bind(this)); createChild(), this.advance_.bind(this), this.onError_.bind(this));
this.audioControls_.attachMedia(createChild('', 'audio')); this.audioControls_.attachMedia(createChild('', 'audio'));
chrome.fileBrowserPrivate.getStrings(function(strings) { chrome.fileBrowserPrivate.getStrings(function(strings) {
...@@ -74,7 +73,7 @@ AudioPlayer.load = function() { ...@@ -74,7 +73,7 @@ AudioPlayer.load = function() {
AudioPlayer.instance = AudioPlayer.instance =
new AudioPlayer(document.querySelector('.audio-player')); new AudioPlayer(document.querySelector('.audio-player'));
reload(); AudioPlayer.instance.load(window.appState);
}; };
util.addPageLoadHandler(AudioPlayer.load); util.addPageLoadHandler(AudioPlayer.load);
...@@ -91,11 +90,7 @@ function unload() { ...@@ -91,11 +90,7 @@ function unload() {
* Reload the player. * Reload the player.
*/ */
function reload() { function reload() {
if (window.appState) { AudioPlayer.instance.load(window.appState);
util.saveAppState();
AudioPlayer.instance.load(window.appState);
return;
}
} }
/** /**
...@@ -107,8 +102,9 @@ AudioPlayer.prototype.load = function(playlist) { ...@@ -107,8 +102,9 @@ AudioPlayer.prototype.load = function(playlist) {
this.audioControls_.pause(); this.audioControls_.pause();
this.currentTrack_ = -1; this.currentTrack_ = -1;
// Save the app state, in case of restart. // Save the app state, in case of restart. Make a copy of the object, so the
window.appState = playlist; // playlist member is not changed after entries are resolved.
window.appState = JSON.parse(JSON.stringify(playlist));
util.saveAppState(); util.saveAppState();
util.URLsToEntries(playlist.items, function(entries) { util.URLsToEntries(playlist.items, function(entries) {
...@@ -216,13 +212,9 @@ AudioPlayer.prototype.select_ = function(newTrack, opt_restoreState) { ...@@ -216,13 +212,9 @@ AudioPlayer.prototype.select_ = function(newTrack, opt_restoreState) {
this.currentTrack_ = newTrack; this.currentTrack_ = newTrack;
if (window.appState) { window.appState.position = this.currentTrack_;
window.appState.position = this.currentTrack_; window.appState.time = 0;
window.appState.time = 0; util.saveAppState();
util.saveAppState();
} else {
util.platform.setPreference(AudioPlayer.TRACK_KEY, this.currentTrack_);
}
this.scrollToCurrent_(false); this.scrollToCurrent_(false);
......
...@@ -490,21 +490,9 @@ MediaControls.prototype.encodeState = function() { ...@@ -490,21 +490,9 @@ MediaControls.prototype.encodeState = function() {
if (!this.media_.duration) if (!this.media_.duration)
return; return;
if (window.appState) { window.appState.time = this.media_.currentTime;
window.appState.time = this.media_.currentTime; util.saveAppState();
util.saveAppState(); return;
return;
}
var playState = JSON.stringify({
play: this.isPlaying(),
time: this.media_.currentTime
});
var newLocation = document.location.origin + document.location.pathname +
document.location.search + '#' + playState;
document.location.href = newLocation;
}; };
/** /**
...@@ -512,53 +500,23 @@ MediaControls.prototype.encodeState = function() { ...@@ -512,53 +500,23 @@ MediaControls.prototype.encodeState = function() {
* @return {boolean} True if decode succeeded. * @return {boolean} True if decode succeeded.
*/ */
MediaControls.prototype.decodeState = function() { MediaControls.prototype.decodeState = function() {
if (window.appState) { if (!('time' in window.appState))
if (!('time' in window.appState)) return false;
return false; // There is no page reload for apps v2, only app restart.
// There is no page reload for apps v2, only app restart. // Always restart in paused state.
// Always restart in paused state. this.media_.currentTime = appState.time;
this.media_.currentTime = appState.time; this.pause();
this.pause(); return true;
return true;
}
var hash = document.location.hash.substring(1);
if (hash) {
try {
var playState = JSON.parse(hash);
if (!('time' in playState))
return false;
this.media_.currentTime = playState.time;
if (playState.play)
this.play();
else
this.pause();
return true;
} catch (e) {
console.warn('Cannot decode play state');
}
}
return false;
}; };
/** /**
* Remove current state from the page URL or the app state. * Remove current state from the page URL or the app state.
*/ */
MediaControls.prototype.clearState = function() { MediaControls.prototype.clearState = function() {
if (window.appState) { if ('time' in window.appState)
if ('time' in window.appState) delete window.appState.time;
delete window.appState.time; util.saveAppState();
util.saveAppState(); return;
return;
}
var newLocation = document.location.origin + document.location.pathname +
document.location.search + '#';
document.location.href = newLocation;
}; };
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment