I made some functions to make it easy undestand and add a lot of comments.
Ok make a new empty console project in VC++ add these files to your project. You have to link or add this to your project in order not to have linker errors
#pragma comment(lib,"winmm.lib")
MP3Player.cpp: //in this cpp we declare all the functions related with our mp3Player 3 of them are commented i will explain them later.
Code:
#include "Mp3Player.h" MPlayer SearchFilesInFolder(MPlayer obj) { static char temp[255],Path[255]; char *ext; sprintf(Path,obj.Path); // we copy the folder path in a temp buffer it will be something like C:\myfolder WIN32_FIND_DATA FindFileData; strcpy(temp,Path); // copy again so we can do a search strcat(temp,"\\"); // temp path will be for ex C:\myfolder\ / strcat(Path,"\\*.*"); // Path will be C:\myfolder\*.* with this we can search trough all files in the folder HANDLE hfind=FindFirstFile(Path,&FindFileData); obj.mp3 = 0; WIN32_FIND_DATAA dat; BOOL a = FindNextFile(hfind,&dat); //return 1 if sucess or -1 if no more files to search while (a) // while keep finding files { ext=RetExt(dat.cFileName); // with this function we return extension of files in search if (!strcmp(ext,"mp3")) // compare extension with mp3 { sprintf(obj.SongName[obj.mp3],"%s",dat.cFileName); //array of names of songs sprintf(obj.PlayList[obj.mp3++],"%s%s.mp3",temp,dat.cFileName); //We make an array of paths and songs } a = FindNextFile(hfind,&dat); obj.NumSongs++; // Number of files } /* cout <<"Number of files: "<<obj.NumSongs<<endl; cout <<"Number of mp3: "<<obj.mp3<<endl; for (int i=0;i<obj.mp3;i++) { cout<<obj.SongName[i]<<endl; } */ return obj; } int _SoundOpen(LPCSTR filename) { char buffer[255]; sprintf(buffer,"open \"%s\" alias MUSIK",filename); MCIERROR rc = mciSendString(buffer,NULL,NULL,NULL); return rc; } int _SoundPlay(LPCSTR alias) { char buffer[255]; sprintf(buffer,"play %s",alias); MCIERROR rc = mciSendString(buffer,NULL,NULL,NULL); return rc; } int _SoundClose(LPCSTR alias) { char buffer[255]; sprintf(buffer,"close %s",alias); MCIERROR rc= mciSendString(buffer,NULL,NULL,NULL); return rc; } /* char* _SoundPos(LPCSTR alias) { char buffer[255]; char valor[255]; sprintf(buffer,"set %s time format ms",alias); MCIERROR rc = mciSendString(buffer,NULL,0,NULL); sprintf(buffer,"status %s position",alias); mciSendString(buffer,valor,255,NULL); memset(buffer,0,sizeof(buffer)); strcpy(buffer,_SoundTicksToTime(valor)); return buffer; } char* _Soundlen(LPCSTR alias) { char buffer[255]; char valor[255]; sprintf(buffer,"status %s length",alias); mciSendString(buffer,valor,255,NULL); int hora,min,sec; memset(buffer,0,sizeof(buffer)); strcpy(buffer, _SoundTicksToTime(valor)); return buffer; } MPlayer FindInFolders(MPlayer obj) { char temp[MAX_PATH]; WIN32_FIND_DATA FindFileData; WIN32_FIND_DATAA dat; for (int i=0;i<20;i++) { if (strcmp(obj.Folders[i],"")) { printf("%s\n",obj.Folders[i]); strcpy(temp,obj.Folders[i]); strcat(temp,"\\*.mp3"); HANDLE hfind = FindFirstFile(temp,&FindFileData); BOOL a = FindNextFile(hfind,&dat); printf("%s",dat.cFileName); while(a) { sprintf(obj.name[obj.mp3],"%s",dat.cFileName); sprintf(obj.Playlist[obj.mp3],"%s\\%s",obj.Folders,dat.cFileName); printf("\n obj mp3 %i \n",obj.mp3); printf("\n play list %s\n",obj.Playlist[obj.mp3++]); system("pause"); a = FindNextFile(hfind,&dat); } memset(temp,0,sizeof(temp)); } } return obj; } */ char* RetExt(char* path) // we return the extension of the file { static char value[255]; char * n; n = strtok (path,"."); int i=0; while (n != NULL) { strcpy(value,n); n = strtok (NULL, "."); } return value; } char* _SoundTicksToTime(char len[255]) { char *buffer = new char[255]; memset(buffer,0,255); int a=atoi(len); int hora,min,sec; a = a/1000; hora = a/3600; a = a%3600; min = a/60; sec = a%60; sprintf(buffer,"%02i:%02i:%02i",hora,min,sec); // we get sound pos in hh:mm:ss return buffer; } char *PidlBrowse() //File Select Folder Function { char *Ret = new char[MAX_PATH]; char Path[MAX_PATH]; BROWSEINFO Browse; Browse.hwndOwner = 0; Browse.pidlRoot = NULL; Browse.pszDisplayName = Path; Browse.lpszTitle = "Select Music Folder"; Browse.ulFlags = BIF_RETURNONLYFSDIRS; Browse.lpfn = NULL; Browse.lParam = 0; Browse.iImage = 0; Browse.pidlRoot = SHBrowseForFolder(&Browse); SHGetPathFromIDList(Browse.pidlRoot, Path); strcpy(Ret,Path); return Ret; }
MP3Player.h : //has the structure of our mp3player and the declarations of the functions.
Code:
#include <windows.h> #include <stdio.h> #include <iostream> #include <shlobj.h> #include <stdlib.h> using namespace std; //#pragma comment(lib,"winmm.lib") // in visual studio remove the comment row or link that lib to your project struct MPlayer //Struct thaw will store all the data of our MUSIC PLAYER { int index,NumSongs,mp3,vol; char PlayList[255][MAX_PATH],SongName[255][255],SoundPos[255],Path[MAX_PATH],SoundLen[255]; }; char* RetExt(char* path); // Return extension of file char* _SoundTicksToTime(char len[255]); // Return sound pos in ms MPlayer SearchFilesInFolder(MPlayer obj); // Search for mp3 files in any folder int _SoundOpen(LPCSTR filename); // Open sound for playing char *PidlBrowse(); // Open FileSelect folder to select the folder where the music files will be founded int _SoundPlay(LPCSTR alias); // Play sound by alias int _SoundClose(LPCSTR alias); // Close sound by alias
main.cpp:
Code:
#include "Mp3Player.h" int main() //will make work all together { MPlayer obj; // we make an obj for our struct memset(&obj,0,sizeof(obj)); // set all values to 0 strcpy(obj.Path,PidlBrowse()); // We get the path of the music folder obj = SearchFilesInFolder(obj); // we search for mp3 files in previous selected folder obj.index = 0; //index of current song ex (the songs are stored in an array something like this {"C:\MusicFolder\someMusic.mp3","C:\MusicFolder\someMusic2.mp3",...} the first is index 0 for example) obj.vol = 100; // Volume strcpy(obj.SoundPos,""); int SoundErr; SoundErr = _SoundOpen(obj.PlayList[obj.index]); //open the first song in the playlist if (SoundErr != 0) printf("Error Opening the file \n"); SoundErr= _SoundPlay("MUSIK"); //play it if (SoundErr != 0) printf("Error Playing the file \n"); mciSendString("setaudio MUSIK volume to 100",NULL,NULL,NULL); //set volume to 100 (100 to 1000) while (1){ static char len[255],pos[255],volume[255]; //declare buffers to storage length, position , and volume values MCIERROR rc; mciSendString("status MUSIK length",len,255,0); // Musick len; strcpy(obj.SoundLen,_SoundTicksToTime(len)); mciSendString("status MUSIK position",pos,255,0); //Music Pos in ms strcpy(obj.SoundPos,_SoundTicksToTime(pos)); // we transform that ms in hh:mm:ss and we storaged that value in soundpos if (!strcmp(obj.SoundLen,obj.SoundPos)) // if pos == len then next song { _SoundClose("MUSIK"); obj.index++; if (obj.index>obj.mp3-1) obj.index = 0; //if index > number of songs (we reach the last song) we make a loop to continue playing _SoundOpen(obj.PlayList[obj.index]); _SoundPlay("MUSIK"); } if(GetAsyncKeyState(VK_RIGHT)&1) // if key right next song { system("cls"); _SoundClose("MUSIK"); obj.index++; cout <<obj.index<<endl; if (obj.index>obj.mp3-1) obj.index = 0; cout <<obj.index<<endl; _SoundOpen(obj.PlayList[obj.index]); _SoundPlay("MUSIK"); } if(GetAsyncKeyState(VK_LEFT)&1) //if key left previous song { _SoundClose("MUSIK"); obj.index--; if (obj.index<0) obj.index = obj.mp3-1; _SoundOpen(obj.PlayList[obj.index]); _SoundPlay("MUSIK"); } if(GetAsyncKeyState(VK_UP)&1)//up volume { sprintf(volume,"setaudio MUSIK volume to %i",obj.vol); rc= mciSendString(volume,NULL,NULL,0); if (obj.vol==1000) obj.vol=1000; else obj.vol+=100; } if(GetAsyncKeyState(VK_DOWN)&1) // down volume { sprintf(volume,"setaudio MUSIK volume to %i",obj.vol); rc= mciSendString(volume,NULL,NULL,0); if (obj.vol==100) obj.vol=100; else obj.vol-=100; } system("cls"); //Print some data =) printf("\r Folder: %s \n" "\r N Songs: %i \n" "\r Playing: %s \n" "\r Pos: %s | %s \n" "\r Next Song: %s \n" "\r Volume: %i \n" ,obj.Path,obj.mp3,obj.SongName[obj.index],obj.SoundPos,obj.SoundLen,obj.SongName[obj.index+1],(obj.vol/10)); Sleep(100); } }
This music player will only find mp3 music in one folder at once. Why don't you try to modify the code and make it work with all formats (mp4,wav,etc) and to search in different folders inside the one you selected (clue you can modify the function FindInFolders which is commented) also use getFileAttributes to find out if it is a file or a folder.
Best Wishes and happy codding