mirror of
https://github.com/clangen/musikcube.git
synced 2024-10-02 13:02:35 +00:00
After toggling shuffle, track down the new playback index and prepare the next track.
This commit is contained in:
parent
f574d11ca7
commit
01a41531c6
@ -106,6 +106,10 @@ TrackPtr TrackList::Get(size_t index) {
|
|||||||
return query->Result();
|
return query->Result();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DBID TrackList::GetId(size_t index) {
|
||||||
|
return this->ids.at(index);
|
||||||
|
}
|
||||||
|
|
||||||
void TrackList::CopyFrom(TrackList& from) {
|
void TrackList::CopyFrom(TrackList& from) {
|
||||||
this->Clear();
|
this->Clear();
|
||||||
|
|
||||||
@ -115,6 +119,11 @@ void TrackList::CopyFrom(TrackList& from) {
|
|||||||
std::back_inserter(this->ids));
|
std::back_inserter(this->ids));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int TrackList::IndexOf(DBID id) {
|
||||||
|
auto it = std::find(this->ids.begin(), this->ids.end(), id);
|
||||||
|
return (it == this->ids.end()) ? -1 : it - this->ids.begin();
|
||||||
|
}
|
||||||
|
|
||||||
void TrackList::Shuffle() {
|
void TrackList::Shuffle() {
|
||||||
std::random_shuffle(this->ids.begin(), this->ids.end());
|
std::random_shuffle(this->ids.begin(), this->ids.end());
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,8 @@ namespace musik {
|
|||||||
size_t Count();
|
size_t Count();
|
||||||
void Add(const DBID& id);
|
void Add(const DBID& id);
|
||||||
musik::core::TrackPtr Get(size_t index);
|
musik::core::TrackPtr Get(size_t index);
|
||||||
|
DBID GetId(size_t index);
|
||||||
|
int IndexOf(DBID id);
|
||||||
void ClearCache();
|
void ClearCache();
|
||||||
void Clear();
|
void Clear();
|
||||||
void Swap(TrackList& list);
|
void Swap(TrackList& list);
|
||||||
|
@ -133,16 +133,33 @@ void PlaybackService::SetRepeatMode(RepeatMode mode) {
|
|||||||
|
|
||||||
void PlaybackService::ToggleShuffle() {
|
void PlaybackService::ToggleShuffle() {
|
||||||
boost::recursive_mutex::scoped_lock lock(this->playlistMutex);
|
boost::recursive_mutex::scoped_lock lock(this->playlistMutex);
|
||||||
if (this->unshuffled.Count() > 0) {
|
|
||||||
|
/* remember the ID of the playing track -- we're going to need to look
|
||||||
|
it up after the shuffle */
|
||||||
|
DBID id = -1;
|
||||||
|
if (this->index < this->playlist.Count()) {
|
||||||
|
id = this->playlist.GetId(this->index);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this->unshuffled.Count() > 0) { /* shuffled -> unshuffled */
|
||||||
this->playlist.Clear();
|
this->playlist.Clear();
|
||||||
this->playlist.Swap(this->unshuffled);
|
this->playlist.Swap(this->unshuffled);
|
||||||
this->Shuffled(false);
|
this->Shuffled(false);
|
||||||
}
|
}
|
||||||
else {
|
else { /* unshuffled -> shuffle */
|
||||||
this->unshuffled.CopyFrom(this->playlist);
|
this->unshuffled.CopyFrom(this->playlist);
|
||||||
this->playlist.Shuffle();
|
this->playlist.Shuffle();
|
||||||
this->Shuffled(true);
|
this->Shuffled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* find the new playback index and prefetch the next track */
|
||||||
|
if (id != -1) {
|
||||||
|
int index = this->playlist.IndexOf(id);
|
||||||
|
if (index != -1) {
|
||||||
|
this->index = index;
|
||||||
|
POST(this, MESSAGE_PREPARE_NEXT_TRACK, 0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlaybackService::ProcessMessage(IMessage &message) {
|
void PlaybackService::ProcessMessage(IMessage &message) {
|
||||||
|
Loading…
Reference in New Issue
Block a user