Music Player with directory mp3 lookup

This commit is contained in:
Jason Hooks 2010-11-01 17:06:13 -04:00
parent 74aba13053
commit a7c3a29ffd
2 changed files with 40 additions and 11 deletions

View file

@ -56,19 +56,43 @@ void OMW::Engine::executeLocalScripts()
mIgnoreLocalPtr = MWWorld::Ptr(); mIgnoreLocalPtr = MWWorld::Ptr();
} }
void OMW::Engine::MP3Lookup()
{
boost::filesystem::directory_iterator dir_iter(mDataDir / "Music/Explore/"), dir_end;
//std::list<boost::filesystem::path> files;
nFiles = 0;
std::string mp3extension = ".mp3";
for(;dir_iter != dir_end; dir_iter++)
{
if(boost::filesystem::extension(*dir_iter) == mp3extension)
{
files.push_front((*dir_iter));
nFiles++;
}
}//*/
}
void OMW::Engine::startRandomTitle() void OMW::Engine::startRandomTitle()
{ {
char number[100];
/* initialize random seed: */
srand ( time(NULL) );
/* generate secret number: */ std::list<boost::filesystem::path>::iterator fileIter;
int r = rand() % 7 + 1; if(nFiles > 0)
{
fileIter = files.begin();
srand ( time(NULL) );
sprintf(number, "Music/Explore/mx_explore_%d.mp3", r);
//std::string music = (mDataDir / "Music/Explore/mx_explore_5.mp3").file_string(); int r = rand() % nFiles + 1; //old random code
std::string music = (mDataDir / number).file_string();
//int lowest=1, highest=nFiles;
//int range=(highest-lowest)+1;
//int r = lowest+int(range*rand()/(highest + 1.0));
for(int i = 1; i < r; i++)
{
fileIter++;
}
std::string music = fileIter->file_string();
try try
{ {
std::cout << "Playing " << music << "\n"; std::cout << "Playing " << music << "\n";
@ -78,13 +102,12 @@ void OMW::Engine::startRandomTitle()
{ {
std::cout << " Music Error: " << e.what() << "\n"; std::cout << " Music Error: " << e.what() << "\n";
} }
}
} }
bool OMW::Engine::frameStarted(const Ogre::FrameEvent& evt) bool OMW::Engine::frameStarted(const Ogre::FrameEvent& evt)
{ {
// if(! (mEnvironment.mSoundManager->isMusicPlaying()))
MWWorld::Environment test = mEnvironment;
if(! (test.mSoundManager->isMusicPlaying()))
{ {
// Play some good 'ol tunes // Play some good 'ol tunes
startRandomTitle(); startRandomTitle();
@ -250,6 +273,8 @@ void OMW::Engine::go()
assert (!mCellName.empty()); assert (!mCellName.empty());
assert (!mMaster.empty()); assert (!mMaster.empty());
MP3Lookup();
std::cout << "Data directory: " << mDataDir << "\n"; std::cout << "Data directory: " << mDataDir << "\n";

View file

@ -52,6 +52,8 @@ namespace OMW
class Engine : private Ogre::FrameListener class Engine : private Ogre::FrameListener
{ {
std::list<boost::filesystem::path> files;
int nFiles;
boost::filesystem::path mDataDir; boost::filesystem::path mDataDir;
OEngine::Render::OgreRenderer mOgre; OEngine::Render::OgreRenderer mOgre;
std::string mCellName; std::string mCellName;
@ -78,6 +80,8 @@ namespace OMW
/// add resources directory /// add resources directory
/// \note This function works recursively. /// \note This function works recursively.
void OMW::Engine::MP3Lookup();
void startRandomTitle();
void addResourcesDirectory (const boost::filesystem::path& path); void addResourcesDirectory (const boost::filesystem::path& path);
/// Load all BSA files in data directory. /// Load all BSA files in data directory.