c语言播放器源代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include "stdafx.h"
#include
#include
#include "resource.h"
#include "MainDlg.h"
#include
#include
#include "ABOUT.h"
#include
#include
#include
static int n,m,e=0,g=0;
static nowplay[MAX_PATH];
static int k=0,shunxu=0,delyn=0;
static TCHAR shortp[MAX_PATH];
static TCHAR ret[256],er[512];
static TCHAR length[256];
static TCHAR time2[512],time1[512];
static TCHAR str[512],vol[256];
static TCHAR buf[MAX_PATH],volstr[256],bmode[256],xh[512],xh1[512],xh2[512];
static int alltime,minute,second,alltime1,minute1,second1,alltime5;
static int i=1,tempcc,suiji5;
static int xunh=1,duquyn=0;
void playg();
void xunhuan();
void danxun();
void suiji();
void del();
void savelist();
void loadlist();
void loadset();
void saveset();
HWND listhwnd,hwnd;
HWND hwndc;
static int volume1=500;
static TCHAR length1[512];
static TCHAR ret1[512],szVolume[256];
static WPARAM wParam;
static struct song
{
char name[MAX_PATH];
char lujing[MAX_PATH];
}songs[1000];
static struct temp
{
char name1[MAX_PATH];
}song1[1000];
static struct savesetting
{
int save_i;
int vol_save;
int xunh_save;
int save_n;
}st;
BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
HANDLE_MSG(hWnd, WM_CLOSE, Main_OnClose);
}

return FALSE;
}
VOID CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime);
void CALLBACK sliderTimerProc(HWND hwnd,UINT message,UINT iTimerId,DWORD dwTime);
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
return TRUE;
}
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{

strcpy(xh,"全部循环");
strcpy(xh1,"单曲循环");
strcpy(xh2,"随机播放");
listhwnd=GetDlgItem(hwnd,IDC_LIST1);
ZeroMemory(nowplay,sizeof(nowplay));
switch(id)
{
case IDC_LIST1:
{
switch(codeNotify)
{
case LBN_DBLCLK:
{
if(e)//判断是否正在播放,防止多首一起播放
{

GetShortPathName(songs[g].lujing,shortp,sizeof(shortp));
TCHAR str5[512];
wsprintf(str5,"close %s",shortp);
mciSendString(str5,buf,0,NULL);

}
e=1;
g=ListBox_GetCurSel(listhwnd);//播放选中歌曲
ListBox_SetCurSel(listhwnd,g);
playg();
SetTimer(hwnd,0,1000,TimerProc);
SetDlgItemText(hwnd,IDC_EDIT1,songs[g].name);

}
break;
}
}break;
case IDC_LOADLIST:
{
loadset();
volume1=st.vol_save;
i=st.save_i;
n=st.save_n;
xunh=st.xunh_save;
duquyn=1;
loadlist();
}break;
case ID_TIANJIA:
{

OPENFILENAME ofn;
TCHAR szFile[MAX_PATH*1000];
ZeroMemory(&ofn,sizeof(ofn));

ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = TEXT('\0');
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = TEXT("所有支持文件(*.wav,*.mp3,*.wma)\0*.wav;*.mp3;"
"*.wma\0所有文件\0*.*\0WAVE文件(*.wav)\0*.wav\0MP3文件(*.mp3)\0*.mp3\0WMA文件(*.wma)\0*.wma\0\0");
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.hwndOwner = hwnd;
ofn.Flags = OFN_EXPLORER |OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST|OFN_ALLOWMULTISELECT;
TCHAR szPath[MAX_PATH];//存储文件名前面的路径
TCHAR* p;
int nLen = 0;
if(GetOpenFileName(&ofn))//&表示取ofn的指针
{
lstrcpyn(szPath,szFile, ofn.nFileOffset );
szPath[ ofn.nFileOffset ] ='\0';
nLen = lstrlen(szPath);
if( szPath[nLen-1] != '\\' )
{
lstrcat(szPath,TEXT("\\"));
}
p = szFile + ofn.nFileOffset;
while( *p )
{

lstrcat(songs[n].lujing, szPath);
lstrcat(songs[n].lujing,p);
itoa(i,songs[n].name,10);
lstrcat(songs[n].name,".");
lstrcat(songs[n].name,p);
lstrcpyn(song1[n].name1,p,sizeof(song1[n].name1));//1
ListBox_InsertString(listhwnd,-1,songs[n].name);
p += lstrlen(p) +1;
i++;
n++;
}
}

}break;

case IDC_JIA:
{
if(volume1==1000)
{
MessageBox(hwnd,TEXT("已经是最大声音"),TEXT("警告"),MB_OK|MB_ICONERROR);
break;
}
wsprintf(szVolume,"setaudio %s volume to %d",shortp,volume1);
volume1=volume1+10;

}break;
case IDC_JIE:
{
if(volume1==0)//
{
MessageBox(hwnd,TEXT("已经是最小声音"),TEXT("警告"),MB_OK|MB_ICONERROR);
break;
}
volume1=volume1-10;
wsprintf(szVolume,"setaudio %s volume to %d",shortp,volume1);
}break;
case IDC_SHUNXU:
{
xunh=1;
}break;
case IDC_DANXUN:
{
xunh=0;
}break;
case IDC_SUIJI:
{
xunh=2;
suiji5=1;
}break;
case IDC_OK:
{

if(e)//判断是否正在播放,防止多首一起播放
{

GetShortPathName(songs[g].lujing,shortp,sizeof(shortp));
TCHAR str5[512];
wsprintf(str5,"close %s",shortp);
mciSendString(str5,buf,0,NULL);
}
e=1;
g=ListBox_GetCurSel(listhwnd);//播放选中歌曲
ListBox_SetCurSel(listhwnd,g);
playg();
SetDlgItemText(hwnd,IDC_EDIT1,songs[g].name);
SetTimer(hwnd,0,1000,TimerProc);//尚不知有用没
}
break;
case IDC_DEL:
{
del();
}break;
case IDC_ZANTING:
{
e=0;//告诉没有停止
GetShortPathName(songs[g].lujing,shortp,sizeof(shortp));
TCHAR str5[512];
wsprintf(str5,"pause %s",shortp);
mciSendString(str5,"",0,NULL);
}break;
ca

se IDC_TINZHI:
{
GetShortPathName(songs[g].lujing,shortp,sizeof(shortp));
TCHAR str6[512];
wsprintf(str6,"close %s",shortp);
mciSendString(str6,"",0,NULL);
e=0;//告诉歌曲已停止
wsprintf(time2,"00:00", minute, second);
SetDlgItemText(hwnd,IDC_EDIT5,time2);
}break;
case IDC_XIASHOU:
{
int geshu;
TCHAR str[512];
GetShortPathName(songs[g].lujing,shortp,sizeof(shortp));
wsprintf(str,"close %s",shortp);
mciSendString(str,"",0,NULL);

int o;
o=g;
geshu=ListBox_GetCount(listhwnd)-1;
if(o==geshu)//判断歌曲位置
{
MessageBox(hwnd,TEXT("已经是最后一首"),TEXT("警告"),MB_OK|MB_ICONERROR);
g=0;
ListBox_SetCurSel(listhwnd,g);//跳到第一首
break;
}

g++;
ListBox_SetCurSel(listhwnd,g);//跳到下首
if(e)
{
playg();
}
SetDlgItemText(hwnd,IDC_EDIT1,songs[g].name);

SetTimer(hwnd,0,1000,TimerProc);//用于播放完当前继续播放下首
}
break;
case IDC_UPSHOU:
{

TCHAR str8[512];
GetShortPathName(songs[g].lujing,shortp,sizeof(shortp));//暂停当前播放的歌曲
wsprintf(str8,"close %s",shortp);
mciSendString(str8,"",0,NULL);
int o;
o=g;
if(o==0)
{
MessageBox(hwnd,TEXT("当前是第一首"),TEXT("警告"),MB_OK|MB_ICONERROR);
break;
}
g--;
ListBox_SetCurSel(listhwnd,g);
playg();

SetTimer(hwnd,0,1000,TimerProc);
}
break;
case ID_ABOUT:
{
HINSTANCE hInstance=(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE);
DialogBox(hInstance,MAKEINTRESOURCE(IDD_ABOUT),NULL,ABOUT_PROC);

}break;
default:
break;

}}
void Main_OnClose(HWND hwnd)
{
saveset();
savelist();
EndDialog(hwnd, 0);
}
void playg()
{

GetShortPathName(songs[g].lujing,shortp,sizeof(shortp));
wsprintf(str,"play %s",shortp);
mciSendString(str,"",0,NULL);
wsprintf(bmode,"status %s mode", shortp);//播放状态
mciSendString(bmode,buf,sizeof(buf),NULL);
wsprintf(ret,"status %s length",shortp);//
SetDlgItemText(hwnd,IDC_EDIT1,songs[g].name);
mciSendString(ret,length,sizeof(length), NULL);//歌曲总长
alltime=atoi(length)/1000;
minute=alltime/60; /////得到分
second=alltime%60; ////得到秒
wsprintf(time2,"%02d:%02d", minute, second);
SetDlgItemText(hwnd,IDC_EDIT4,time2);

SetTimer(hwnd,0,1000,TimerProc);//播放后继续播放
ListBox_SetCurSel(listhwnd,g);
}
void xunhuan()//顺序播放
{
if(0 == lstrcmp(buf,"stopped"))
{
g++;
if(g>n)
g=0;
playg();

}
}
void danxun()
{
if(0 == lstrcmp(buf,"stopped"))
{
g=g;
playg();

}
}
void suiji()
{
if(0 == lstr

cmp(buf,"stopped"))
{
if((n+1)==2)
{
g++;
if(g>n)
g=0;
}
(int)g=(g+5+int(second/10)+(second%10))/2;
if(g<0)
{
g=-g;
}
if(g>n)
{
g=n-g;
}
playg();
}
}
VOID CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
if(e)
{

wsprintf(ret1,"status %s position",shortp);
mciSendString(ret1,length1, sizeof(length1),0);//时间进度
alltime1=atoi(length1)/1000;
minute1=alltime1/60; /////得到分
second1=alltime1%60; ////得到秒
wsprintf(time1, "%02d:%02d", minute1, second1);
SetDlgItemText(hwnd,IDC_EDIT5,time1);
wsprintf(szVolume,"setaudio %s volume to %d",shortp,volume1);//动态调节音量
wsprintf(bmode,"status %s mode", shortp);//动态获取播放状态检查是否播放完
mciSendString(bmode,buf,sizeof(buf),NULL);
mciSendString(szVolume,"",0,NULL);//音量调节
st.vol_save=volume1;
wsprintf(volstr,"%d",volume1/10);
SetDlgItemText(hwnd,IDC_VOL,volstr);//显示当前音量
if(xunh==1)
{
xunhuan();
SetDlgItemText(hwnd,IDC_EDIT3,xh);
}
else if(xunh==0)
{
danxun();
SetDlgItemText(hwnd,IDC_EDIT3,xh1);
}
else if(xunh==2)
{
suiji();
SetDlgItemText(hwnd,IDC_EDIT3,xh2);
}
GetShortPathName(songs[g].lujing,shortp,sizeof(shortp));//获得当前歌曲总时长
mciSendString(ret,length,sizeof(length), NULL);
alltime=atoi(length)/1000;
minute=alltime/60; /////得到分
second=alltime%60; ////得到秒
wsprintf(time2,"%02d:%02d", minute, second);
//以下为才添加 对进度条的实现
mciSendString(ret1,length,sizeof(length)/sizeof(TCHAR),NULL);
SendDlgItemMessage(hwnd,IDC_SLIDER1,TBM_SETRANGEMAX,false,(LPARAM)alltime);
SendDlgItemMessage(hwnd,IDC_SLIDER1,TBM_SETRANGEMIN,false,(LPARAM)0);
SendDlgItemMessage(hwnd,IDC_SLIDER1,TBM_SETPOS,true,(LPARAM)alltime1);
//
SetDlgItemText(hwnd,IDC_EDIT4,time2);
SetDlgItemText(hwnd,IDC_EDIT1,songs[g].name);//动态显示歌名
st.xunh_save=xunh;
st.save_n=n;
st.save_i=i;
}
}
//尚未完成
void del()
{
int xzdel=ListBox_GetCurSel(listhwnd);
int tempxz;
if(xzdel>=0||xzdel<=n)
{
tempxz=xzdel;
if(xzdel==0)
{
ListBox_DeleteString(listhwnd,0);
ZeroMemory(songs[0].name,sizeof(songs[0].name));
ZeroMemory(songs[0].lujing,sizeof(songs[0].lujing));
ZeroMemory(song1[0].name1,sizeof(song1[0].name1));
n=0;
i=1;
}
for(tempxz;tempxz<=(n-1);tempxz++)
{
ListBox_DeleteString(listhwnd,tempxz);
ZeroMemory(songs[tempxz].name,sizeof(songs[tempxz].name));
ZeroMemory(songs[tempxz].lujing,sizeof(songs[tempxz].lujing));
itoa(tempxz+1,songs[tempxz].name,10);
lstrcat(songs[tempxz].name,".");
lstrcat(songs[tempxz].name,song1[tempxz+1].name1);
lstrcpyn(songs[tempxz].lujing,songs[tempxz+1].lujing,sizeof(songs[tempxz].lujing));
ListBox_InsertString(listhwnd,tempxz,songs[tempxz].name);


}
i=i-1;
n=n-1;
if(n<0)
{
n=0;
}
if(i<1)
{
i=1;
}
}
}
void loadset()
{
FILE *fp;
if(fp=fopen("C:\\乐ting\\saveset","rb"))
{
fread(&st,sizeof(struct savesetting),1,fp);
}
fclose(fp);
}
void saveset()
{
FILE *fp;
if(fp=fopen("C:\\乐ting\\saveset","wb"))
{
fwrite(&st,sizeof(struct savesetting),1,fp);
}
fclose(fp);
}
void savelist()
{
FILE *fp;
if(fp=fopen("C:\\乐ting\\savelist","wb"))
{
int gs=0;
for(gs=0;gs<=n;gs++)
{
fwrite(&songs[gs],sizeof(struct song),1,fp);
}
}
fclose(fp);
}
//未完成
void loadlist()
{
int gs=0;
FILE *fp;
if(fp=fopen("C:\\乐ting\\savelist","rb"))
{

for(gs;gs<=n;++gs)
{
fread(&songs[gs],sizeof(struct song),1,fp);
ListBox_InsertString(listhwnd,-1,songs[gs].name);
}
}
fclose(fp);
}

相关文档
最新文档