X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fjukebox.cpp;h=95ca6a05e9e2656dab7d85708c98d07e0606cafd;hb=2a685e572e2ab8d9f50129de3f0869628edb3d8e;hp=3cd5e080848ff0aeb517136774e2883aab9498bf;hpb=abef5eab53c6c75cd602fdabdec94259d1523858;p=libs%2Fal.git diff --git a/source/jukebox.cpp b/source/jukebox.cpp index 3cd5e08..95ca6a0 100644 --- a/source/jukebox.cpp +++ b/source/jukebox.cpp @@ -7,6 +7,7 @@ Distributed under the LGPL #include #include +#include #include "jukebox.h" #include "sound.h" @@ -18,6 +19,7 @@ namespace AL { Jukebox::Jukebox(): streamer(source), sound(0), + current_track(tracks.end()), shuffle(false) { } @@ -29,20 +31,25 @@ Jukebox::~Jukebox() void Jukebox::add_track(const string &trk) { - bool was_empty=tracks.empty(); + bool was_empty = tracks.empty(); tracks.push_back(trk); if(was_empty) - current_track=tracks.begin(); + { + current_track = tracks.begin(); + signal_track_changed.emit(*current_track); + } } void Jukebox::remove_track(const string &trk) { - list::iterator i=find(tracks.begin(), tracks.end(), trk); + list::iterator i = find(tracks.begin(), tracks.end(), trk); if(i!=tracks.end()) { if(i==current_track) next(); tracks.erase(i); + if(tracks.empty()) + current_track = tracks.end(); } } @@ -50,11 +57,19 @@ void Jukebox::clear_tracks() { stop(); tracks.clear(); + current_track = tracks.end(); +} + +const string &Jukebox::get_current_track() const +{ + if(tracks.empty()) + throw InvalidState("No current track"); + return *current_track; } void Jukebox::set_shuffle(bool s) { - shuffle=s; + shuffle = s; } void Jukebox::play() @@ -62,7 +77,7 @@ void Jukebox::play() if(tracks.empty() || sound) return; - sound=new AL::Sound; + sound = new AL::Sound; sound->open_file(*current_track); streamer.play(*sound); } @@ -76,7 +91,7 @@ void Jukebox::next() { while(1) { - list::iterator i=tracks.begin(); + list::iterator i = tracks.begin(); advance(i, rand()%tracks.size()); if(i!=current_track) { @@ -89,8 +104,25 @@ void Jukebox::next() { ++current_track; if(current_track==tracks.end()) - current_track=tracks.begin(); + current_track = tracks.begin(); } + signal_track_changed.emit(*current_track); + } + play(); +} + +void Jukebox::previous() +{ + if(shuffle) + return next(); + + stop(); + if(tracks.size()>1) + { + if(current_track==tracks.begin()) + current_track = tracks.end(); + --current_track; + signal_track_changed.emit(*current_track); } play(); } @@ -99,7 +131,7 @@ void Jukebox::stop() { streamer.stop(); delete sound; - sound=0; + sound = 0; } void Jukebox::tick()