16,550
社区成员
发帖
与我相关
我的任务
分享#include <windows.h>
#include <stdio.h>
#include <mmsystem.h>
#include <shellapi.h>
#include <AFXCOM_.H>
#pragma comment(lib,"winmm.lib")
unsigned long getmusictime(char *s)//利用MCI命令,*S为文件名,返回值为文件时间的秒数
{
unsigned long m_dLength;
int m_wDeviceID;
char buf[128];
MCI_OPEN_PARMS mciOpen;
MCI_STATUS_PARMS mciStatusParms;
MCIERROR mciError;
mciOpen.lpstrElementName=(LPCTSTR)s;//取得文件名
mciError=mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciOpen);//打开文件
if(mciError) //出错处理
{
mciGetErrorString(mciError,buf,128);
printf("%s\n",buf);
getchar();
}
m_wDeviceID=mciOpen.wDeviceID;
mciStatusParms.dwItem=MCI_STATUS_LENGTH;
mciSendCommand(m_wDeviceID,MCI_STATUS,MCI_WAIT|MCI_STATUS_ITEM,(DWORD)(LPVOID)&mciStatusParms);//关键,取得长度
m_dLength=mciStatusParms.dwReturn;
mciSendCommand(m_wDeviceID,MCI_CLOSE,0,NULL);//关闭文件
return m_dLength;
}
void main()
{
char *filename="E:\\Music\\原谅我一次.mp3";//filename 为实际的文件名
cout<<getmusictime(filename)<<endl; //调用上面的getmuisctime()
unsigned long time=getmusictime(filename);
unsigned long temp=time/1000;//毫秒换成秒,我的小学数学基础扎实
cout<<temp/3600;//这里开始输出HH:MM:SS(时分秒格式)
cout<<':';
temp=temp%3600;
cout<<temp/60;
temp=temp%60;
cout<<':';
cout<<temp;
cout<<endl;
getchar();
}