Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #32119 from justindarc/bug1209785
Browse files Browse the repository at this point in the history
Bug 1209785 - The playlist will restart when the next song button is …
  • Loading branch information
justindarc committed Sep 30, 2015
2 parents d9a4ad6 + 4110406 commit 5321c3d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
14 changes: 13 additions & 1 deletion apps/music/js/app.js
Expand Up @@ -52,6 +52,18 @@ var client = bridge.client({

client.on('play', () => isPlaying = true);

client.on('stop', () => {
isPlaying = false;

var isPlayerView = viewStack.activeView &&
viewStack.activeView.url === VIEWS.PLAYER.URL;

if (isPlayerView) {
viewStack.popView(true);
window.history.back();
}
});

client.on('databaseChange', () => updateOverlays());

client.on('databaseUpgrade', () => upgradeOverlay.hidden = false);
Expand Down Expand Up @@ -104,7 +116,7 @@ header.addEventListener('action', (evt) => {
}

var isPlayerView = viewStack.activeView &&
viewStack.activeView.frame.src.endsWith(VIEWS.PLAYER.URL);
viewStack.activeView.url === VIEWS.PLAYER.URL;

if (viewStack.views.length > 1) {
// Don't destroy the popped view if it is the "Player" view.
Expand Down
56 changes: 46 additions & 10 deletions apps/music/js/endpoint.js
Expand Up @@ -7,10 +7,11 @@ var audio = document.getElementById('audio');

var loadQueueSettings = PlaybackQueue.loadSettings();

var currentFilePath;
var currentQueue;
var isInterrupted = false;
var isFastSeeking = false;
var currentFilePath = null;
var currentQueue = null;
var isInterrupted = false;
var isFastSeeking = false;
var isStopped = true;

var service = bridge.service('music-service')
.method('play', play)
Expand Down Expand Up @@ -103,6 +104,10 @@ function play(filePath) {
}

getSongFile(filePath).then((file) => {
if (isStopped) {
return;
}

currentFilePath = filePath;

audio.src = null;
Expand All @@ -125,6 +130,20 @@ function pause() {
audio.pause();
}

function stop() {
isStopped = true;

audio.pause();

currentFilePath = null;
currentQueue = null;

audio.src = null;
audio.load();

service.broadcast('stop');
}

function seek(time) {
time = parseInt(time, 10);
audio.currentTime = time;
Expand Down Expand Up @@ -165,7 +184,8 @@ function getPlaybackStatus() {
duration: audio.duration,
elapsedTime: audio.currentTime,
isInterrupted: isInterrupted,
isFastSeeking: isFastSeeking
isFastSeeking: isFastSeeking,
stopped: isStopped
});
}

Expand All @@ -192,7 +212,10 @@ function nextSong(automatic = false) {
return Promise.reject();
}

currentQueue.next(automatic);
var hasNextSong = currentQueue.next(automatic);
if (!hasNextSong) {
return Promise.resolve(stop());
}

return currentSong().then(song => play(song.name));
}
Expand All @@ -203,7 +226,10 @@ function queueArtist(filePath) {
var index = songs.findIndex(song => song.name === filePath);
currentQueue = new PlaybackQueue.StaticQueue(songs, index);

return currentSong().then(song => play(song.name));
return currentSong().then((song) => {
isStopped = false;
play(song.name);
});
});
});
}
Expand All @@ -214,7 +240,10 @@ function queueAlbum(filePath) {
var index = songs.findIndex(song => song.name === filePath);
currentQueue = new PlaybackQueue.StaticQueue(songs, index);

return currentSong().then(song => play(song.name));
return currentSong().then((song) => {
isStopped = false;
play(song.name);
});
});
});
}
Expand All @@ -229,7 +258,10 @@ function queuePlaylist(id, filePath) {
(playlist.shuffle ? Math.floor(Math.random() * songs.length) : 0);
currentQueue = new PlaybackQueue.StaticQueue(songs, index);

return currentSong().then(song => play(song.name));
return currentSong().then((song) => {
isStopped = false;
play(song.name);
});
});
});
});
Expand All @@ -241,7 +273,10 @@ function queueSong(filePath) {
var index = songs.findIndex(song => song.name === filePath);
currentQueue = new PlaybackQueue.StaticQueue(songs, index);

return currentSong().then(song => play(song.name));
return currentSong().then((song) => {
isStopped = false;
play(song.name);
});
});
});
}
Expand Down Expand Up @@ -445,6 +480,7 @@ function open(blob) {
return AudioMetadata.parse(blob).then((metadata) => {
var filePath = blob.name;

isStopped = false;
play(filePath);

return {
Expand Down
6 changes: 4 additions & 2 deletions apps/music/js/remote.js
Expand Up @@ -4,6 +4,7 @@

var Remote = (function() {
const PLAY_STATUS_INTERRUPTED = 'mozinterruptbegin';
const PLAYSTATUS_STOPPED = 'STOPPED';
const PLAY_STATUS_PAUSED = 'PAUSED';
const PLAY_STATUS_PLAYING = 'PLAYING';

Expand Down Expand Up @@ -42,8 +43,8 @@ var Remote = (function() {
updatePlaybackStatus: function() {
if (Remote.enabled) {
client.method('getPlaybackStatus').then((status) => {
var playStatus = status.paused ?
PLAY_STATUS_PAUSED : PLAY_STATUS_PLAYING;
var playStatus = status.stopped ? PLAYSTATUS_STOPPED :
(status.paused ? PLAY_STATUS_PAUSED : PLAY_STATUS_PLAYING);

if (status.isInterrupted) {
playStatus = PLAY_STATUS_INTERRUPTED;
Expand Down Expand Up @@ -166,6 +167,7 @@ var Remote = (function() {

client.on('play', Remote.updatePlaybackStatus);
client.on('pause', Remote.updatePlaybackStatus);
client.on('stop', Remote.updatePlaybackStatus);
client.on('elapsedTimeChange', Remote.updatePlaybackStatus);
client.on('interruptBegin', Remote.updatePlaybackStatus);
client.on('interruptEnd', Remote.updatePlaybackStatus);
Expand Down

0 comments on commit 5321c3d

Please sign in to comment.