/* MP3PLAYER (c) 2001-2004 DG2HL, Roger Luff (rluff@_REMOVE_THIS_BEFORE_YOU_SEND_THE_MAIL_gmx.de) http://visumod.freeshell.org. See the readme.txt file for details. This small piece of software mainly plays mp3 files and above that it interpretes a playlist, plays songs in a dirctory selected by wildcards (*.mp3) if you like in randon sequence and obviously it plays single mp3 files. */ #include #include #include #include #include #include #include #include #include #include #include "mp3player.hpp" void main (int ArgC, char *ArgV[]) { Player play; long entries; bool randomplay = false; bool copy = false; int LV_Option; play.printinfo (); if (ArgC < 2) play.printerror(ArgV[0]); for(int i = 2; i 0) play.playmp3(entries,randomplay,copy); cout << "End reached, program terminated normally."<< endl; }; Globals::Globals() { } Globals::~Globals() { } bool Globals::copyFile (const CString& sourceFilename, const CString& destinationFilename) { const int BUF_SIZE = 10480; ifstream sourceFile; ofstream destinationFile; unsigned char *buffer = NULL; errno = 0; //cout << "copy file: " << sourceFilename << " to " << destinationFilename<< endl; sourceFile.open((const char *)sourceFilename,ios::nocreate); if (sourceFile.fail()) { cout << "Error opening source file: " << sourceFilename << " Reason: " << strerror(errno) << endl; return false; } sourceFile.setmode(filebuf::binary); destinationFile.open((const char *)destinationFilename); if (destinationFile.fail()) { cout << "Error opening destination file: " << destinationFilename << " Reason: " << strerror(errno) << endl; return false; } destinationFile.setmode(filebuf::binary ); buffer = new unsigned char[BUF_SIZE]; while (!sourceFile.eof()) { sourceFile.read(buffer, BUF_SIZE); if ((sourceFile.fail()) && (!sourceFile.eof())) { delete buffer; cout << "Error reading source file: " << sourceFilename << " Reason: " << strerror(errno) << endl; return false; } if (sourceFile.eof()) destinationFile.write(buffer, sourceFile.gcount()); else destinationFile.write(buffer, BUF_SIZE); if (destinationFile.fail()) { delete buffer; cout << "Error writing to destination file: " << destinationFilename << " Reason: " << strerror(errno) << endl; return false; } } // End-while (!sourceFile.eof()) sourceFile.close(); destinationFile.close(); delete buffer; return true; } void Globals::removeFile (const CString& filename) { //cout << "remove file: " << filename << endl; if (filename.GetLength() > 0) { errno = 0; if (remove((const char *)filename) != 0) { cout << "Error deleting file: " << filename << " Reason: " << strerror(errno) << endl; exit (-1); } } } Player::Player() { srand( (unsigned)time( NULL ) ); } Player::~Player() { } void Player::printinfo () { cout << "==========================================================================" << endl; cout << "=== Simple mp3 player for WIN-DOS ===" << endl; cout << "=== Version 1.2 ===" << endl; cout << "=== R. Luff - (c)DG2HL ( cout << "r"; cout << "l"; cout << "u"; cout << "f"; cout << "f"; cout << "@"; cout << "g"; cout << "m"; cout << "x"; cout << "."; cout << "d"; cout << "e"; cout << ") (http://visumod.freeshell.org) ===" << endl; cout << "==========================================================================" << endl < 0 ) || (file.Find( ".m3u" ) > 0 )) { //load a playlist char buffer[200]; ifstream ifile (file); if (!ifile.fail()) { cout << "Loading list file: " << file << " ....."; while ((!ifile.eof()) && (i < MAX_FILES)) { ifile.getline(buffer,sizeof(buffer)); if (buffer[0] != '#') { playlist[i] = buffer; i++; } } cout << " done!" << endl; lv_entries = i-1; } else { cout << "Error loading list-file" << endl; exit (-1); } } if (file.Find(".mp3") > 0) { cout << "Getting playlist from directory: " << file << " ....."; //load the directory struct _finddata_t c_file; long hFile; int lastslash; CString path =""; //a path given ?? separate it lastslash = file.ReverseFind('\\'); if (lastslash > 0) { path = file.Left(lastslash+1); } // Find first file in current directory if( (hFile = _findfirst( file, &c_file )) == -1L ) { cout << endl << "No file: " << file << " found !" << endl; exit (-1); } else { playlist[i] = path + c_file.name; /* Find the rest of the files */ while( _findnext( hFile, &c_file ) == 0 ) { i++; playlist[i] = path + c_file.name; } _findclose( hFile ); lv_entries = i+1; } cout << " done!" << endl; } for ( i = 0; i < lv_entries; i++ ) { randomindex[i] = 0; } return lv_entries; } void Player::playmp3(long entries, bool randomplay, bool copy) { long i; long stop = 0; long random; int ch; CString title; //this path is exists everywhere and prevents me to search environment variables CString tmpplayfile = "c:\\mp3player.mp3"; CString mci_command; bool OK; LPTSTR lpszReturnString =" "; DWORD fdwError = 0; UINT cchErrorText=50; i = 0; do { if (randomplay) { do { //play playlist in random order, but play all titles once random = rand()%entries; } while (randomindex[random] != 0); randomindex[random] = 1; title = playlist[random]; } else { //play playlist in normal order title = playlist[i]; } i = i + 1; OK = false; if (copy) { OK = Globals::copyFile(title, tmpplayfile); } else { //when the file do not needed to copy, mciSendString do not like the long names with spaces, convert it: char buf[256]; if (GetShortPathName( title, buf, 256 ) > 0) { tmpplayfile = buf; OK = true; } } if (OK) { ch = ' '; cout << "Playing: " << title << " " << i << " of " << entries << endl; mci_command = "open "; mci_command += tmpplayfile; mci_command += " type MPEGVideo Alias Mp3 wait"; //mciSendString("open c:\\TEMP\\player\\Debug\\a.mp3 type MPEGVideo Alias Mp3", "", 0, 0); mciSendString(mci_command, "", 0, 0); mciSendString("play Mp3", "", 0, 0); fdwError = mciSendString("status Mp3 mode",lpszReturnString,50, 0); while ((strcmp(lpszReturnString,"playing") == 0) && (stop == 0)) { Sleep(2000); fdwError = mciSendString("status Mp3 mode",lpszReturnString,50, 0); //mciGetErrorString(fdwError, lpszReturnString, cchErrorText ); if( _kbhit() ) { ch = _getch(); ch = toupper( ch ); if (ch == 'Q') { cout << "Note: Normal termination by user. Playlist not completely finnished" << endl; stop = 1; } if (ch == 'S') { cout << "Note: Song skipped by user" << endl; stop = 1; } if (ch == 'P') { mciSendString("pause Mp3", "", 0, 0); cout << "Note: Pause, press any key to continue" << endl; ch = _getch(); mciSendString("play Mp3", "", 0, 0); } } //cout << "TEST>>" << lpszReturnString<< "<