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) {
this.audioControls_ = new FullWindowAudioControls(
createChild(), this.advance_.bind(this), this.onError_.bind(this));
this.audioControls_.attachMedia(createChild('', 'audio'));
chrome.fileBrowserPrivate.getStrings(function(strings) {
......@@ -74,7 +73,7 @@ AudioPlayer.load = function() {
AudioPlayer.instance =
new AudioPlayer(document.querySelector('.audio-player'));
reload();
AudioPlayer.instance.load(window.appState);
};
util.addPageLoadHandler(AudioPlayer.load);
......@@ -91,11 +90,7 @@ function unload() {
* Reload the player.
*/
function reload() {
if (window.appState) {
util.saveAppState();
AudioPlayer.instance.load(window.appState);
return;
}
AudioPlayer.instance.load(window.appState);
}
/**
......@@ -107,8 +102,9 @@ AudioPlayer.prototype.load = function(playlist) {
this.audioControls_.pause();
this.currentTrack_ = -1;
// Save the app state, in case of restart.
window.appState = playlist;
// Save the app state, in case of restart. Make a copy of the object, so the
// playlist member is not changed after entries are resolved.
window.appState = JSON.parse(JSON.stringify(playlist));
util.saveAppState();
util.URLsToEntries(playlist.items, function(entries) {
......@@ -216,13 +212,9 @@ AudioPlayer.prototype.select_ = function(newTrack, opt_restoreState) {
this.currentTrack_ = newTrack;
if (window.appState) {
window.appState.position = this.currentTrack_;
window.appState.time = 0;
util.saveAppState();
} else {
util.platform.setPreference(AudioPlayer.TRACK_KEY, this.currentTrack_);
}
window.appState.position = this.currentTrack_;
window.appState.time = 0;
util.saveAppState();
this.scrollToCurrent_(false);
......
......@@ -490,21 +490,9 @@ MediaControls.prototype.encodeState = function() {
if (!this.media_.duration)
return;
if (window.appState) {
window.appState.time = this.media_.currentTime;
util.saveAppState();
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;
window.appState.time = this.media_.currentTime;
util.saveAppState();
return;
};
/**
......@@ -512,53 +500,23 @@ MediaControls.prototype.encodeState = function() {
* @return {boolean} True if decode succeeded.
*/
MediaControls.prototype.decodeState = function() {
if (window.appState) {
if (!('time' in window.appState))
return false;
// There is no page reload for apps v2, only app restart.
// Always restart in paused state.
this.media_.currentTime = appState.time;
this.pause();
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;
if (!('time' in window.appState))
return false;
// There is no page reload for apps v2, only app restart.
// Always restart in paused state.
this.media_.currentTime = appState.time;
this.pause();
return true;
};
/**
* Remove current state from the page URL or the app state.
*/
MediaControls.prototype.clearState = function() {
if (window.appState) {
if ('time' in window.appState)
delete window.appState.time;
util.saveAppState();
return;
}
var newLocation = document.location.origin + document.location.pathname +
document.location.search + '#';
document.location.href = newLocation;
if ('time' in window.appState)
delete window.appState.time;
util.saveAppState();
return;
};
/**
......
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