# include <stdio.h>
# include <conio.h>
# include <windows.h>
# include <stdlib.h>
# include <string.h>
# include <io.h>
# pragma comment (lib , "WINMM.LIB")
void GetFileName (char * FilePath , char *FileType)
{
FILE * fp = fopen ("MusicName.txt" , "w+");
if (!fp)
return ;
_finddata_t FileInfo;
long findhandle;
char cmd[260];
sprintf (cmd , "%s%s" , FilePath , FileType);
findhandle = _findfirst (cmd , &FileInfo);
fputs (FileInfo.name , fp);
while (! _findnext (findhandle , &FileInfo))
{
fputs ("\n" , fp);
fputs (FileInfo.name , fp);
//
printf ("%d\n" , FileInfo.size);
}
fputs ("\n" , fp);
fclose (fp);
}
void PlayAMusic (char * FilePath , char * FileName)
{
char buf[64];
char cmd[1024];
int n = 0;
for (int i = 0; FileName[i] != '\0'; i++)
{
if (FileName[i] == ' ')
{
n = 1;
break;
}
}
if (n == 1)
{
sprintf (cmd , "%s%s%s%s" , "open \"" , FilePath , FileName , "\" alias MyMusic");
}
else
sprintf (cmd , "%s%s%s%s" , "open " , FilePath , FileName , " alias MyMusic");
MCIERROR mciError = mciSendStringA (cmd , NULL , 0 , NULL);
if (mciError)
{
mciGetErrorStringA (mciError , buf , sizeof (buf));
MessageBoxA (NULL , buf , "警告" , 1);
}
mciSendStringA ("play MyMusic wait" , NULL , 0 , NULL);
mciSendStringA ("close MyMusic" , NULL , 0 , NULL);
}
void PlayAllTheMusic (char * FilePath)
{
FILE *fp = fopen ("MusicName.txt" , "r");
if (!fp)
return ;
char FileName[260];
while (fgets (FileName , 260 , fp) != 0)
{
int n = strlen (FileName);
FileName[n-1] = '\0';
puts ("正在播放的歌曲是:\n");
puts (FileName);
PlayAMusic (FilePath , FileName);
}
fclose (fp);
}
int main (void)
{
char FilePath[] = "D:\\KuGou\\";
char FileType[] = "*.mp3";
GetFileName (FilePath , FileType);
PlayAllTheMusic (FilePath);
return 0;
}
终于是可以播放了, 非常感谢
……
[此贴子已经被作者于2017-10-19 00:49编辑过]