00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifdef __HAVE_MMSMEDIA__
00034 #include "mmscore/mmsmusicmanager.h"
00035 #include <stdlib.h>
00036
00037 MMSMusicManager::MMSMusicManager() :
00038 onNextSong(NULL),
00039 onPrevSong(NULL),
00040 cont(false),
00041 repeat(false),
00042 shuffle(false) {
00043
00044 MMSMusicInterface interface;
00045 interface.setManager(this);
00046 this->player.onPlaybackFinished.connect(sigc::mem_fun(this, &MMSMusicManager::next));
00047 }
00048
00049 MMSMusicManager::~MMSMusicManager() {
00050 if(this->player.isPlaying())
00051 this->player.stop();
00052 }
00053
00054 void MMSMusicManager::init(PLAYLIST list, int offset) {
00055 this->mutex.lock();
00056 this->playlist = list;
00057 this->offset = offset;
00058 this->alreadyPlayed.clear();
00059 for(unsigned int i = 0; i < playlist.size(); i++)
00060 this->alreadyPlayed.push_back(false);
00061 this->mutex.unlock();
00062
00063 DEBUGMSG("MMSMusicManager", "got playlist size: %d offset: %d", list.size(), offset);
00064 }
00065
00066 void MMSMusicManager::stopAll() {
00067 this->mutex.lock();
00068 if(player.isPlaying())
00069 player.stop();
00070 this->cont=false;
00071 this->mutex.unlock();
00072 }
00073
00074 void MMSMusicManager::next() {
00075 this->mutex.lock();
00076 if(this->shuffle && (this->playlist.size() > 2)) {
00077 int newOffset;
00078 do {
00079 newOffset = int((double(rand())/RAND_MAX) * (this->playlist.size() - 1));
00080 }
00081 while(this->alreadyPlayed.at(newOffset));
00082 this->offset = newOffset;
00083 }
00084 else {
00085 this->offset++;
00086 if(this->offset>=(int)this->playlist.size()) {
00087 if(!this->repeat) return;
00088 this->offset=0;
00089 }
00090 }
00091 string file = this->playlist.at(this->offset);
00092
00093 if(player.isPlaying())
00094 player.stop();
00095 player.startPlaying(file, false);
00096 this->alreadyPlayed.at(this->offset) = true;
00097 if(this->onNextSong)
00098 this->onNextSong->emit(this->offset);
00099
00100 this->mutex.unlock();
00101
00102 }
00103
00104 void MMSMusicManager::prev() {
00105 this->mutex.lock();
00106 if(player.isPlaying())
00107 player.stop();
00108 if(this->shuffle && (this->playlist.size() > 2)) {
00109 int newOffset;
00110 do {
00111 newOffset = int((double(rand())/RAND_MAX) * (this->playlist.size() - 1));
00112 }
00113 while(this->alreadyPlayed.at(newOffset));
00114 this->offset = newOffset;
00115 }
00116 else {
00117 this->offset--;
00118 if(this->offset < 0)
00119 this->offset = this->playlist.size() - 1;
00120 }
00121 string file = this->playlist.at(this->offset);
00122 player.startPlaying(file,false);
00123 this->alreadyPlayed.at(this->offset) = true;
00124 if(this->onPrevSong)
00125 this->onPrevSong->emit(this->offset);
00126 this->mutex.unlock();
00127 }
00128
00129 void MMSMusicManager::play() {
00130 if(this->cont) {
00131 player.play();
00132 }
00133 else {
00134 if(this->playlist.size() > (size_t)this->offset) {
00135 string file = this->playlist.at(this->offset);
00136 if(player.isPlaying()) player.stop();
00137 player.startPlaying(file, cont);
00138 this->alreadyPlayed.at(this->offset) = true;
00139 }
00140 }
00141 }
00142
00143 void MMSMusicManager::pause() {
00144 this->mutex.lock();
00145 player.pause();
00146 cont=true;
00147 this->mutex.unlock();
00148 }
00149
00150 bool MMSMusicManager::hasPlaylist() {
00151 this->mutex.lock();
00152 if(this->playlist.size()>0) {
00153 this->mutex.unlock();
00154 return true;
00155 }
00156 this->mutex.unlock();
00157 return false;
00158 }
00159
00160 PLAYLIST MMSMusicManager::getPlaylist() {
00161 return this->playlist;
00162 }
00163
00164 int MMSMusicManager::getPlaylistOffset() {
00165 return this->offset;
00166
00167 }
00168
00169 bool MMSMusicManager::isPlaying() {
00170 return player.isPlaying();
00171 }
00172
00173 bool MMSMusicManager::isPaused() {
00174 return player.isPaused();
00175 }
00176
00177 bool MMSMusicManager::getTimes(int *pos, int *length) {
00178 return player.getTimes(pos, length);
00179 }
00180
00181 void MMSMusicManager::setOnNextSong(sigc::signal<void, int> *onNextSong) {
00182 this->onNextSong = onNextSong;
00183 }
00184
00185 void MMSMusicManager::setOnPrevSong(sigc::signal<void, int> *onPrevSong) {
00186 this->onPrevSong = onPrevSong;
00187 }
00188
00189 void MMSMusicManager::setRepeat(bool repeat) {
00190 this->repeat = repeat;
00191 }
00192
00193 void MMSMusicManager::setShuffle(bool shuffle) {
00194 this->shuffle = shuffle;
00195 }
00196 #endif