[MFC] MFC音乐播放器 傻瓜级教程 网络 搜索歌曲 下载

 

 

         》目录《

》——————————————————————《

1、建立工程 

1、建立一个MFC工程,命名为Tao_Music 

2、选择为基本对话框 

3、包含Windows Sockts 

4、使用静态链接库 [方便一会直接生成的exe文件单独可以发布] 

2、 搭建界面 

1、 拖入控件: 

2、 控件拖入并摆好 

3、 控件属性设置: 

3、 写代码实现功能 

1、变量绑定: 

2、加入图片资源 

3、新建mp3类: 

4、修改Tao_MusicDlg.h 

5、修改Tao_Music.cpp 

6、功能实现: 

7、加一个TImer消息使时间跑起来! 

8、给音量控制滑块加消息,使音量控制实现 

4、编译运行完工! 

 

》——————————————————————《

1、建立工程

1、建立一个MFC工程,命名为Tao_Music

 

2、选择为基本对话框

 

3、包含Windows Sockts

[因为要用网络功能]

 

4、使用静态链接库 [方便一会直接生成的exe文件单独可以发布]

 

 

2、搭建界面

1、拖入控件:

拖入7个用于播放控制的button,一个picture控件,一个控制音量的滑块。一个Group Box用于盛放搜索功能各个控件及Group Box中2个静态文本,2个可编辑文本,一个搜索按钮。最下面的是一个List Control用于显示歌曲的。最终效果如下:

 

2、控件拖入并摆好

 

3、控件属性设置:

选中Button1右键选择属性,弹出:

 

将其改为:

 

相同的道理将音乐控制的7个按钮和搜索按钮的Styles和Extended Styles都设置成相同的风格。

Button2-8的General属性依次为:【注意大小写】

 

 

 

相应的这里picture控件的属性为:

 

 

 

音量滑块的属性为:

 

Group Box的属性为:

 

 

2个静态文本的属性只要改General就行啦:

 

 

2个文本编辑框也只要改General就行:

 

 

最后是list control控件的属性:

 

 

 

好啦,最终的效果就是这样的:前面这么啰嗦大家千万别弄错啦!接下来就是写代码啦!

 

3、写代码实现功能

1、变量绑定:

查看->建立类向导->Member Variables

 

将其设置为:

【双击对应蓝条即出现设置对话框】最后别忘点确定!

 

2、加入图片资源

【首先用格式工厂转换出32X32的24张bmp图片,放入工程文件的res文件中->进入Resource界面->右键Tao_Music resources->insert->弹出的对话框点击导入然后将你的24张bmp图片一个一个导入,注意这里最多每次导入8个,要分批导入!导入后的ID为IDB_BITMAP1----IDB_BITMAP24】

 

3、新建mp3类:

【插入->新建类】

 

 1 //音乐播放控制类
 2 
 3 #include "mmsystem.h"//必须同时引入
 4 #pragma comment(lib,"Winmm.lib")
 5 
 6 class mp3  
 7 {
 8 public:
 9     mp3();
10     virtual ~mp3();
11     HWND m_hWnd;//3个变量
12     DWORD DeviceID;
13     MCI_OPEN_PARMS mciopenparms; 
14     void Load(HWND hwnd,CString Strfilepath);
15     DWORD getinformation(DWORD item);
16     void Play();
17     void Pause();
18     void resum();
19     void Stop();
20     DWORD Setvolumn(DWORD vol);
21 };
mp3.h
 1 #include "StdAfx.h"
 2 #include "mp3.h"
 3 #include "Digitalv.h" 
 4 
 5 //---------------------------------------------------------------------
 6 mp3::mp3()
 7 {
 8 }
 9 //---------------------------------------------------------------------
10 mp3::~mp3()
11 {
12 }
13 //---------------------------------------------------------------------
14 //加载函数
15 //---------------------------------------------------------------------
16 void mp3::Load(HWND hwnd,CString Strfilepath)
17 {
18     //MessageBox(hwnd,Strfilepath,"43",MB_ICONHAND);
19     m_hWnd=hwnd;
20     mciSendCommand(DeviceID,MCI_CLOSE,0,0);//在加载文件前先清空上一次播放的设备
21     mciopenparms.lpstrElementName=Strfilepath;//将音乐文件路径传给设备
22     DWORD dwReturn;
23     if (dwReturn=mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_ELEMENT|MCI_WAIT,(DWORD)(LPVOID)&mciopenparms))
24     {
25         //如果打开文件失败,则将出错信息储存在buffer,并显示出错警告
26         char buffer[256]; 
27         mciGetErrorString(dwReturn,buffer,256);
28         //MessageBox(hwnd,buffer,"HeHe,You Are Wrong!",MB_ICONHAND|MB_ICONERROR|MB_ICONSTOP);
29     }
30     //打开文件成功就关联文件到设备
31     DeviceID=mciopenparms.wDeviceID;
32 }
33 //---------------------------------------------------------------------
34 DWORD mp3::getinformation(DWORD item)
35 {
36     MCI_STATUS_PARMS mcistatusparms;
37     mcistatusparms.dwItem=item;
38     mcistatusparms.dwReturn=0;
39     mciSendCommand(DeviceID,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)&mcistatusparms);
40     return mcistatusparms.dwReturn; 
41 }
42 //---------------------------------------------------------------------
43 //播放函数
44 //---------------------------------------------------------------------
45 void mp3::Play()
46 {
47     MCI_PLAY_PARMS mciplayparms;
48     mciplayparms.dwCallback=(DWORD)m_hWnd;
49     mciplayparms.dwFrom=0;//每次播放都是从0开始播放
50     mciSendCommand(DeviceID,MCI_PLAY,MCI_FROM|MCI_NOTIFY,(DWORD)(LPVOID)&mciplayparms);
51 }
52 //---------------------------------------------------------------------
53 //暂停
54 //---------------------------------------------------------------------
55 void mp3::Pause()
56 {
57     mciSendCommand(DeviceID,MCI_PAUSE,0,0);
58 }
59 //---------------------------------------------------------------------
60 //重播
61 //---------------------------------------------------------------------
62 void mp3::resum()
63 {
64     mciSendCommand(DeviceID,MCI_RESUME,0,0);
65 }
66 //---------------------------------------------------------------------
67 //停止
68 //---------------------------------------------------------------------
69 void mp3::Stop()
70 {
71     mciSendCommand(DeviceID,MCI_STOP,0,0);
72     mciSendCommand(DeviceID,MCI_CLOSE,0,0);
73     //当点击停止按钮时,将所有的信息都清除掉
74 }
75 //---------------------------------------------------------------------
76 //设置音量
77 //---------------------------------------------------------------------
78 DWORD mp3::Setvolumn(DWORD vol)
79 {
80     MCI_DGV_SETAUDIO_PARMS mcisetvolumn;
81     mcisetvolumn.dwCallback=(DWORD)m_hWnd;
82     mcisetvolumn.dwItem=MCI_DGV_SETAUDIO_VOLUME;
83     mcisetvolumn.dwValue=vol;
84     mciSendCommand(DeviceID,MCI_SETAUDIO,MCI_DGV_SETAUDIO_VALUE|MCI_DGV_SETAUDIO_ITEM,(DWORD)(LPVOID)&mcisetvolumn);
85 
86     //    return mcisetvolumn.dwValue;
87     return 0;
88 }
mp3.cpp

 

 

4、修改Tao_MusicDlg.h

在CTao_MusicDlg类里加入下面函数和变量

 1 public:
 3 void Show(int cnt);
 5 void addsong(TCHAR * name);
 7 void suiji();
 9 void pre();
11 void next();
13 void drawpic(int nTimerID);
15 BOOL AnalyseLrc(TCHAR* LrcFile);//歌词解析函数
17 BOOL DownLoad(TCHAR* Url, TCHAR* SaveName);//下载资源函数
19 bool down(TCHAR* song,TCHAR* songer,TCHAR* getstr);//下载XML资源函数
20 
21  
23 public:
25 int hour,minute,second;
27 CString cursong;
29 int showstr;
31 int donghuakind;//动画种类[初始化时给一个随机数,选择显示的动画种类]
33 CImageList m_imList;
35 typedef struct _LRC_INFO//定义歌词结构体
37 {
39 int Time;
41 TCHAR Lyric[256];
43 }LRC_INFO;
45 LRC_INFO LrcInfo[500];

 

5、修改Tao_Music.cpp

在include下面再引入下面几个文件和库:

1 #include "mp3.h"
2 #include "mmsystem.h"
3 #include "digitalv.h"
4 #include <afxinet.h>
5 #include <shlwapi.h>
6 #pragma comment(lib,"Winmm.lib")
7 #pragma comment(lib,"wininet.lib")   
8 #pragma comment(lib, "shlwapi.lib")

 

紧接着在全局声明一个mp3型的全局变量:

 1 mp3 Mp3;                                                                                                                                           

转到OnInitDialog()函数处在return 前加入如下代码:

 1 // TODO: Add extra initialization here
 2 SetWindowText("MP3播放器");//标题
 3 m_slider.SetRange(0,1000); //移动范围
 4 m_slider.SetPos(500);//滑块指针的初始位置
 5 GetDlgItem(IDC_open)->EnableWindow(FALSE);
 6 GetDlgItem(IDC_pause)->EnableWindow(FALSE);
 7 GetDlgItem(IDC_del)->EnableWindow(FALSE);
 8 //List 初始化---------------------
 9 // 设置CListCtrl控件扩展风格:整行选中\子项目图标列表|LVS_EX_GRIDLINES
10 DWORD dwStyle; 
11 dwStyle =m_StoreItems.GetExtendedStyle();  
12 dwStyle = dwStyle|LVS_EX_FULLROWSELECT|LVS_EX_SUBITEMIMAGES ;
13 m_StoreItems.SetExtendedStyle(dwStyle);   
14 // 载入32*32像素 24位真彩(ILC_COLOR24)图片
15 m_imList.Create(32,32,ILC_COLOR24,10,20);    // 创建图像序列CImageList对象
16 // 设置CImageList图像列表与CListCtrl控件关联 LVSIL_SMALL小图标列表
17 m_StoreItems.SetImageList(&m_imList,LVSIL_SMALL);
18 // 向列表视图控件InsertColumn插入3列数据 
19 CRect mRect;
20 m_StoreItems.GetWindowRect(&mRect);                     // 获取控件矩形区域
21 int length = mRect.Width()-3;
22 m_StoreItems.InsertColumn(0, _T("图片"), LVCFMT_CENTER,40, -1);
23 m_StoreItems.InsertColumn(1, _T("信息"), LVCFMT_LEFT, length-60, -1);
24 //--------------
25 cursong="";
26 showstr=0;
27 donghuakind=rand()%4;
28 // TODO: Add extra initialization here

 

在文件最后依次添加如下函数:

  1 //------------------------------------------------------------------------------------------------
  2 //显示函数
  3 //显示cnt=0为open
  4 //1为delete
  5 //2为stop
  6 //3为timer
  7 //------------------------------------------------------------------------------------------------
  8 void CTao_MusicDlg::Show(int cnt)
  9 {
 10     CClientDC dc(this);
 11     CString mtime;
 12     TCHAR temp[19];
 13     DWORD cdf,cdfrom;
 14     int showstrlen=18,i;
 15     int tposx,tposy,sposx,sposy;
 16     tposx=12,tposy=128;
 17     sposx=12,sposy=81;
 18     switch(cnt){
 19     case 0:
 20         hour=0;minute=0;second=0;
 21         dc.SetBkColor(RGB(124,252,0));//设置放置计数器区域的外观
 22         dc.SetTextColor(RGB(255,255,203));//设置数字显示的颜色
 23         mtime.Format("%02d:%02d:%02d",hour,minute,second);//显示时间进度
 24         dc.TextOut(tposx,tposy,mtime);
 25         for(i=0;i<showstrlen;i++){
 26             temp[i]=cursong[(i+showstr)%cursong.GetLength()];
 27         }temp[i]='\0';
 28         showstr=(showstr+1)%cursong.GetLength();
 29         mtime.Format("----------------------------       ");//覆盖上次显示
 30         dc.TextOut(sposx,sposy,mtime);
 31         dc.TextOut(sposx,sposy,temp);
 32         break;
 33     case 1:
 34         dc.TextOut(sposx,sposy,"");
 35         KillTimer(0);
 36         KillTimer(1);
 37         KillTimer(2);
 38         hour=0;minute=0;second=0;//歌曲时间置0
 39         break;
 40     case 2:
 41         KillTimer(0);//取消计数器的显示
 42         KillTimer(1);
 43         KillTimer(2);
 44         hour=0;minute=0;second=0;
 45         dc.SetBkColor(RGB(124,252,0));//设置放置计数器区域的外观
 46         dc.SetTextColor(RGB(255,255,203));//设置数字显示的颜色
 47         mtime.Format("%02d:%02d:%02d",hour,minute,second);//显示时间进度
 48         dc.TextOut(tposx,tposy,mtime);
 49         for(i=0;i<showstrlen;i++){
 50             temp[i]=cursong[(i+showstr)%cursong.GetLength()];
 51         }temp[i]='\0';
 52         showstr=(showstr+1)%cursong.GetLength();
 53         mtime.Format("----------------------------       ");//覆盖上次显示
 54         dc.TextOut(sposx,sposy,mtime);
 55         dc.TextOut(sposx,sposy,temp);
 56         break;
 57     case 3:
 58         second++;
 59         dc.SetBkColor(RGB(124,252,0));//设置放置计数器区域的外观
 60         dc.SetTextColor(RGB(255,255,203));//设置数字显示的颜色
 61         if(second==60){//设置钟表的显示
 62             minute++;second=0;
 63         }if(minute==60){
 64             hour++;minute=0;
 65         }
 66         //mtime.Format("%02d:%02d:%02d",hour,minute,second);//显示时间进度
 67         //dc.TextOut(280,128,mtime);
 68          cdf=Mp3.getinformation(MCI_STATUS_LENGTH);//获得当前毫秒值MCI_STATUS_POSITION
 69         if(cdf<=Mp3.getinformation(MCI_STATUS_POSITION)){//如果停止就进行换歌
 70             CString strtemp;
 71             GetDlgItemText(IDC_exit,strtemp);//获取按钮状态
 72             if (strtemp.Compare("单曲")==0){
 73                 Mp3.Setvolumn(1000-m_slider.GetPos());//声音设为滑块指示的地方
 74                 Mp3.Load(this->m_hWnd,cursong);
 75                 Mp3.Play();
 76                 hour=0;minute=0;second=0;
 77             }else if(strtemp.Compare("顺序")==0){
 78                 next();
 79             }else if(strtemp.Compare("随机")==0){
 80                 suiji();
 81             }    
 82         }
 83          cdfrom=MCI_MAKE_MSF(MCI_MSF_MINUTE(cdf),MCI_MSF_SECOND(cdf),MCI_MSF_FRAME(cdf));//获取当前播放文件的信息
 84         mtime.Format("%02d:%02d:%02d / %02d:%02d",hour,minute,second,cdf/1000/60,cdf/1000%60);
 85         dc.TextOut(tposx,tposy,mtime);
 86         for(i=0;i<showstrlen;i++){
 87             temp[i]=cursong[(i+showstr)%cursong.GetLength()];
 88         }temp[i]='\0';
 89         showstr=(showstr+1)%cursong.GetLength();
 90         mtime.Format("----------------------------       ");//覆盖上次显示
 91         dc.TextOut(sposx,sposy,mtime);
 92         dc.TextOut(sposx,sposy,temp);
 93         break;
 94     default:break;
 95     }
 96 }
 97 //------------------------------------------------------------------------------------------------
 98 //添加歌词
 99 //------------------------------------------------------------------------------------------------
100 void CTao_MusicDlg::addsong(TCHAR * name)
101 {
102     //wsprintf(strNumber,_T("IDB_BITMAP%d"),rand()%24+162);
103     CBitmap * pBmp = NULL;
104     pBmp = new CBitmap();
105     pBmp->LoadBitmap(rand()%24+IDB_BITMAP1);                // 载入位图162
106     m_imList.Add(pBmp,RGB(0,0,0));  
107     delete pBmp; 
108 
109     // 添加数据 InsertItem向列表中插入主项数据 SetItemText向列表中的子项写入数据
110     LVITEM lvItem={0};                                // 列表视图控 LVITEM用于定义"项"的结构
111     lvItem.mask = LVIF_IMAGE|LVIF_TEXT;                // 文字、图片
112     lvItem.iItem = m_StoreItems.GetItemCount();                            // 行号
113     lvItem.iImage = m_StoreItems.GetItemCount();                        // 图片索引号(第一幅图片 IDB_BITMAP1)
114     lvItem.iSubItem = 0;                            // 子列号
115     m_StoreItems.InsertItem(&lvItem);               // 第一列为图片
116     m_StoreItems.SetItemText(m_StoreItems.GetItemCount()-1,1,name);       // 第二列为名字
117 }
118 //------------------------------------------------------------------------------------------------
119 //随机函数
120 //------------------------------------------------------------------------------------------------
121 void CTao_MusicDlg::suiji()
122 {
123     int index=m_StoreItems.GetSelectionMark();//获取选中的文本
124     if(index==-1)
125     {
126         MessageBox("请添加音乐");
127         return;
128     }
129     index=rand()%m_StoreItems.GetItemCount();
130     CString strfilename;
131     char str[300];
132     m_StoreItems.GetItemText(index,1,str,sizeof(str));
133     strfilename.Format(_T("%s"),str);
134     cursong=strfilename;
135 
136     m_StoreItems.EnsureVisible(index,FALSE);//选中
137     m_StoreItems.SetItemState(index,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED);   //选中行
138     m_StoreItems.SetSelectionMark(index);
139     m_StoreItems.SetFocus();
140 
141     //SetDlgItemText(IDC_filename,strfilename);
142     Mp3.Stop();
143     Mp3.Load(this->m_hWnd,strfilename);
144     Mp3.Play();
145     Mp3.Setvolumn(1000-m_slider.GetPos());//声音设为滑块指示的地方
146     SetTimer(0,1000,NULL);
147     SetTimer(1,100,NULL);
148     SetTimer(2,100,NULL);
149 
150     GetDlgItem(IDC_open)->EnableWindow(TRUE);
151     GetDlgItem(IDC_pause)->EnableWindow(TRUE);
152      GetDlgItem(IDC_del)->EnableWindow(TRUE);
153 
154     Show(0);
155 }
156 //------------------------------------------------------------------------------------------------
157 //上一曲
158 //------------------------------------------------------------------------------------------------
159 void CTao_MusicDlg::pre()
160 {
161     int index=m_StoreItems.GetSelectionMark();//获取选中的文本
162     if(index==-1)
163     {
164         MessageBox("请添加音乐");
165         return;
166     }
167     index=(index-1+m_StoreItems.GetItemCount())%m_StoreItems.GetItemCount();
168     CString strfilename;
169     char str[300];
170     m_StoreItems.GetItemText(index,1,str,sizeof(str));
171     strfilename.Format(_T("%s"),str);
172     cursong=strfilename;
173 
174     m_StoreItems.EnsureVisible(index,FALSE);//选中
175     m_StoreItems.SetItemState(index,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED);   //选中行
176     m_StoreItems.SetSelectionMark(index);
177     m_StoreItems.SetFocus();
178 
179     //SetDlgItemText(IDC_filename,strfilename);
180     Mp3.Stop();
181     Mp3.Load(this->m_hWnd,strfilename);
182     Mp3.Play();
183     Mp3.Setvolumn(1000-m_slider.GetPos());//声音设为滑块指示的地方
184     SetTimer(0,1000,NULL);
185     SetTimer(1,100,NULL);
186     SetTimer(2,100,NULL);    
187 
188     GetDlgItem(IDC_open)->EnableWindow(TRUE);
189     GetDlgItem(IDC_pause)->EnableWindow(TRUE);
190      GetDlgItem(IDC_del)->EnableWindow(TRUE);
191 
192     Show(0);
193 }
194 //------------------------------------------------------------------------------------------------
195 //下一曲
196 //------------------------------------------------------------------------------------------------
197 void CTao_MusicDlg::next()
198 {
199     int index=m_StoreItems.GetSelectionMark();//获取选中的文本
200     if(index==-1)
201     {
202         MessageBox("请添加音乐");
203         return;
204     }
205     index=(index+1)%m_StoreItems.GetItemCount();
206     CString strfilename;
207     char str[300];
208     m_StoreItems.GetItemText(index,1,str,sizeof(str));
209     strfilename.Format(_T("%s"),str);
210     cursong=strfilename;
211 
212     m_StoreItems.EnsureVisible(index,FALSE);//选中
213     m_StoreItems.SetItemState(index,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED);   //选中行
214     m_StoreItems.SetSelectionMark(index);
215     m_StoreItems.SetFocus();
216 
217     //SetDlgItemText(IDC_filename,strfilename);
218     Mp3.Stop();
219     Mp3.Load(this->m_hWnd,strfilename);
220     Mp3.Play();
221     Mp3.Setvolumn(1000-m_slider.GetPos());//声音设为滑块指示的地方
222     SetTimer(0,1000,NULL);
223     SetTimer(1,100,NULL);
224     SetTimer(2,100,NULL);
225 
226     GetDlgItem(IDC_open)->EnableWindow(TRUE);
227     GetDlgItem(IDC_pause)->EnableWindow(TRUE);
228      GetDlgItem(IDC_del)->EnableWindow(TRUE);
229 
230     Show(0);
231 }
232 //----------------------------------------------------------------------
233 //解析歌词
234 //----------------------------------------------------------------------
235 BOOL CTao_MusicDlg::AnalyseLrc(TCHAR* LrcFile)
236 {
237     //读取文件到缓冲区中
238     TCHAR* LrcBuf = NULL;
239     FILE* fp = fopen(LrcFile, "rb");
240     if( fp == NULL){
241         return FALSE;
242     }
243     fseek(fp, 0L, SEEK_END);
244     long LrcLen = ftell(fp);
245     if(LrcLen == 0){
246         return FALSE;
247     }
248     LrcBuf = (TCHAR*) malloc(sizeof(TCHAR) * LrcLen + 1); //开辟缓冲区
249     if(LrcBuf == NULL){
250         return FALSE;
251     }
252     fseek(fp, 0L, SEEK_SET);
253     fread(LrcBuf, LrcLen + 1, 1, fp);
254     if(LrcBuf == NULL){
255         return FALSE;
256     }
257     fclose(fp);
258     //分析缓冲区中内容
259     TCHAR *p1 = NULL;
260     TCHAR *p2 = NULL;
261     int m, n;
262     int i, j = 0;
263     TCHAR* Lrc = LrcBuf;
264     TCHAR CurTime[100];
265     TCHAR Lyric[1024];
266     int nCurTime;
267     while(1){
268         //解析时间
269         p1 = strchr(Lrc, '[');
270         m = p1-Lrc;
271         p2 = strchr(Lrc, ']');
272         n = p2-Lrc;
273         for(i = m + 1; i < n; i++){
274             CurTime[i - m - 1] = Lrc[i];
275         }
276         CurTime[i - m - 1] = '\0';
277         //解析歌词
278         Lrc = p2;
279         p1 = strchr(Lrc, ']');
280         m = p1-Lrc;
281         p2 = strchr(Lrc, '[');
282         n = p2-Lrc;
283         
284         for( i = m + 1; i < n; i++){
285             Lyric[i - m - 1] = Lrc[i];
286         }
287         Lyric[i - m - 1] = '\0';
288         
289         if(p2 == NULL){
290             break;
291         }
292         Lrc = p2;
293         if(lstrlen(Lyric) == 1 || lstrlen(Lyric) == 2){
294             continue;
295         }
296         //计算时间
297         TCHAR* ptime = strchr(CurTime, ':');
298         int k = ptime-CurTime;
299         TCHAR temp[100];
300         for(i = k + 1; i < lstrlen(CurTime); i++){
301             temp[i - k - 1] = CurTime[i];
302         }
303         temp[i - k - 1] = '\0';
304         nCurTime = (((CurTime[0] - '0') * 10 + (CurTime[1] - '0')) * 60 + atoi(temp) ) * 1000;
305         
306         LrcInfo[j].Time = nCurTime;
307         lstrcpy(LrcInfo[j].Lyric, Lyric);
308         j++;
309     }
310     free(LrcBuf);
311     LrcBuf = NULL;
312     return TRUE;
313 }
314 //----------------------------------------------------------------------
315 //下载资源函数
316 //----------------------------------------------------------------------
317 BOOL CTao_MusicDlg::DownLoad(TCHAR* Url, TCHAR* SaveName)
318 {
319     DWORD byteread = 0;
320     TCHAR buffer[100000];
321     memset(buffer, 0,100000);
322     HINTERNET internetopen;
323     internetopen =InternetOpen("Testing", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
324     
325     if (internetopen == NULL){
326         return FALSE;
327     }
328     
329     HINTERNET internetopenurl;
330     internetopenurl = InternetOpenUrl(internetopen, Url, NULL, 0, INTERNET_FLAG_RELOAD, 0);
331     if (internetopenurl == NULL){
332         InternetCloseHandle(internetopen);
333         return FALSE;
334     }
335     BOOL hwrite;
336     DWORD written;
337     
338     HANDLE createfile;
339     createfile = CreateFile(SaveName, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
340     if (createfile == INVALID_HANDLE_VALUE){
341         InternetCloseHandle(internetopenurl);
342         return FALSE;
343     }
344     BOOL internetreadfile;
345     while(1){
346         internetreadfile = InternetReadFile(internetopenurl, buffer, sizeof(buffer), &byteread);
347         if(byteread==0)
348             break;
349         hwrite = WriteFile(createfile, buffer, sizeof(buffer), &written, NULL);
350         if (hwrite == 0){
351             CloseHandle(createfile);
352             return FALSE;
353         }
354     }
355     CloseHandle(createfile);
356     return TRUE;
357 }
358 //----------------------------------------------------------------------
359 //下载XML保存在getstr里
360 //----------------------------------------------------------------------
361 bool CTao_MusicDlg::down(TCHAR* song,TCHAR* songer,TCHAR* getstr)
362 {
363     //http://box.zhangmen.baidu.com/x?op=12&count=1&title=歌曲$$歌手$$$$
364     TCHAR* Url=new TCHAR[strlen(song)+strlen(songer)+100];
365     sprintf(Url,"http://box.zhangmen.baidu.com/x?op=12&count=1&title=%s$$%s$$$$",song,songer);
366 
367     DWORD byteread = 0;
368     TCHAR buffer[100000];
369     memset(buffer, 0,100000);
370     HINTERNET internetopen;
371     internetopen =InternetOpen("Testing", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
372     if (internetopen == NULL){
373         delete[] Url;
374         return FALSE;
375     }
376     HINTERNET internetopenurl;
377     internetopenurl = InternetOpenUrl(internetopen, Url, NULL, 0, INTERNET_FLAG_RELOAD, 0);
378     if (internetopenurl == NULL){
379         InternetCloseHandle(internetopen);
380         delete[] Url;
381         return FALSE;
382     }
383 
384     while(1)
385     {
386         InternetReadFile(internetopenurl, buffer, sizeof(buffer), &byteread);
387         if(byteread==0)
388             break;
389         sprintf(getstr,"%s",buffer);
390     }
391     delete[] Url;
392     return TRUE;
393 }
其它函数

 

6、功能实现:

进入form界面:双击添加按钮:

 

点击ok在新产生的函数中加入代码:

 1 CFileDialog dlg(TRUE);//打开CFileDialog对象
 2 dlg.m_ofn.Flags |= OFN_ALLOWMULTISELECT | 
 3                    OFN_ENABLESIZING | 
 4                    OFN_HIDEREADONLY;
 5 dlg.m_ofn.lpstrFilter = _T("Mp3 Files(*.mp3)\0*.mp3\0Wave Audio Files(*.wav)\0*.wav\0MIDI Files(*.mid)\0*.mid\0AVI Files(*.avi)\0*.avi\0All Files(*.*)\0*.*\0\0");
 6 dlg.m_ofn.lpstrTitle="添加音乐";
 7 dlg.m_ofn.nMaxFile=100*MAX_PATH;//最多100个文件            
 8 dlg.m_ofn.lpstrFile =new TCHAR[dlg.m_ofn.nMaxFile];
 9 ZeroMemory(dlg.m_ofn.lpstrFile,sizeof(TCHAR)*dlg.m_ofn.nMaxFile);
10 
11 //显示文件对话框,获得文件名集合
12 int retval=dlg.DoModal();
13 
14 if(retval==IDCANCEL)return;
15 if (retval==IDOK)
16 {
17     int i = 0;
18     CString strfilepath[101];
19     CString strfilename[101];
20     POSITION pos = dlg.GetStartPosition();//获取第一个文件位置
21     while (pos)
22     {
23         strfilename[i] = dlg.GetNextPathName(pos);
24         char *str=(LPSTR)(LPCTSTR)strfilename[i];
25         addsong(str);//加入mylist列表
26         i++;
27     }
28     //实现和上面功能一样的代码:转到+选中+高亮
29     m_StoreItems.EnsureVisible(m_StoreItems.GetItemCount()-1,FALSE);
30     m_StoreItems.SetItemState(m_StoreItems.GetItemCount()-1,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED);   //选中行
31     m_StoreItems.SetSelectionMark(m_StoreItems.GetItemCount()-1);
32     m_StoreItems.SetFocus();
33 
34     GetDlgItem(IDC_open)->EnableWindow(TRUE);
35     GetDlgItem(IDC_del)->EnableWindow(TRUE);
36 }
37 delete[] dlg.m_ofn.lpstrFile;    
View Code

 

哈哈,运行一下看看第一个按钮的功能实现没!!!

同样第二个按钮有:

 1 UpdateData(TRUE);
 2 
 3 int index=m_StoreItems.GetSelectionMark();//获取选中的文本
 4 CString strfilename;
 5 char str[300];
 6 m_StoreItems.GetItemText(index,1,str,sizeof(str));
 7 strfilename.Format(_T("%s"),str);
 8 if(strfilename==cursong){//如果删除的是当前歌曲则停止
 9     Mp3.Stop();
10     //SetDlgItemText(IDC_filename,"");
11     Show(1);
12 }
13 if(index!=CB_ERR)
14 {
15     m_StoreItems.DeleteItem(index);
16     if(m_StoreItems.GetItemCount()){//选中下一个
17         //实现和上面功能一样的代码:转到+选中+高亮
18         m_StoreItems.EnsureVisible(index%m_StoreItems.GetItemCount(),FALSE);
19         m_StoreItems.SetItemState(index%m_StoreItems.GetItemCount(),LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED);   //选中行
20         m_StoreItems.SetSelectionMark(index%m_StoreItems.GetItemCount());
21         m_StoreItems.SetFocus();
22     }else{
23         GetDlgItem(IDC_del)->EnableWindow(FALSE);//失能暂停键
24     }
25 }
View Code

 

第3个按钮:

 

1 pre();

 

第4个按钮:

 

 1 int index=m_StoreItems.GetSelectionMark();
 2 if(index==-1)
 3 {
 4     MessageBox("请添加音乐");
 5     return;
 6 }
 7 CString strfilename;
 8 char str[300];
 9 m_StoreItems.GetItemText(index,1,str,sizeof(str));
10 strfilename.Format(_T("%s"),str);
11 cursong=strfilename;
12 Mp3.Stop();
13 Mp3.Load(this->m_hWnd,strfilename);
14 Mp3.Play();
15 Mp3.Setvolumn(1000-m_slider.GetPos());//声音设为滑块指示的地方
16 SetTimer(0,1000,NULL);
17 SetTimer(1,100,NULL);
18 SetTimer(2,100,NULL);
19 
20 GetDlgItem(IDC_open)->EnableWindow(TRUE);
21 GetDlgItem(IDC_pause)->EnableWindow(TRUE);
22 GetDlgItem(IDC_del)->EnableWindow(TRUE);
23 
24 Show(0);    
View Code

 

第5个按钮:

 

1 next();

 

第6个按钮:

 

 1 // TODO: Add your control notification handler code here
 2 CString strtemp;
 3 GetDlgItemText(IDC_pause,strtemp);//获取按钮状态
 4 if (strtemp.Compare("暂停")==0)
 5 {
 6     Mp3.Pause();
 7     SetDlgItemText(IDC_pause,"继续");
 8     KillTimer(0);//取消计数器的显示
 9     KillTimer(1);
10     KillTimer(2);
11 }
12 if (strtemp.Compare("继续")==0)
13 {
14     Mp3.resum();
15     SetTimer(0,1000,NULL);
16     SetTimer(1,100,NULL);
17     SetTimer(2,100,NULL);
18     SetDlgItemText(IDC_pause,"暂停");
19 }        
View Code

 

第7个按钮:

 

1 CString strtemp;
2 GetDlgItemText(IDC_exit,strtemp);//获取按钮状态
3 if (strtemp.Compare("单曲")==0){
4     SetDlgItemText(IDC_exit,"顺序");
5 }else if(strtemp.Compare("顺序")==0){
6     SetDlgItemText(IDC_exit,"随机");
7 }else if(strtemp.Compare("随机")==0){
8     SetDlgItemText(IDC_exit,"单曲");
9 }    
View Code

 

第8个按钮:

 

 1 TCHAR* str=new TCHAR[1000000];
 2 TCHAR songdown[1000];
 3 TCHAR cidown[1000];
 4 GetDlgItemText(IDC_SONG,m_song);
 5 GetDlgItemText(IDC_SONGER,m_songer);
 6 if(down((LPSTR)(LPCTSTR)m_song,(LPSTR)(LPCTSTR)m_songer,str)){
 7     int i,j;
 8     int thisnum=0;//计算'<'括号数
 9     for(i=0;i<strlen(str);i++){
10         if(str[i]=='<'){
11             thisnum++;
12             if(thisnum==4){//第4个括号前是否为1判断是否有资源
13                 if(str[i-1]=='0'){
14                     MessageBox((LPSTR)(LPCTSTR)("木有 : "+m_songer+"--"+m_song),"Tao Tao Say",MB_ICONHAND|MB_ICONWARNING);
15                     break;
16                 }
17             }else if(thisnum==7){//第7个括号时判断是否出现http
18                 while(str[i]!='h')i++;//找到开始位置
19                 j=i;//找结束的']'及保存最后一个斜杠的位置
20                 int endpos;
21                 while(str[j]!=']'){
22                     songdown[j-i]=str[j];//先把所有[]中的数据放入songdown中
23                     if(str[j]=='/')endpos=j;
24                     j++;
25                 }
26                 //i=j;//优化
27                 j=endpos-i+1;//将songdown指针放在最后一个斜杠之后
28             }else if(thisnum==10){//第10个<时接上要下载的内容
29                 int from=j;
30                 for(j=0;;j++){
31                     if(str[j+i+9]==']'){
32                         i=j+i+9;
33                         songdown[j+from]='\0';
34                         break;
35                     }
36                     songdown[j+from]=str[j+i+9];
37                 }
38             }else if(thisnum==14){//第14个取得歌词地址
39                 int sumnum=0;
40                 for(j=i+7;;j++){
41                     if(str[j]=='<'){
42                         sprintf(cidown,"http://box.zhangmen.baidu.com/bdlrc/%d/%d.lrc",sumnum/100,sumnum);
43                         break;
44                     }
45                     sumnum*=10;
46                     sumnum+=(int)(str[j]-'0');
47                 }
48             }else if(thisnum>14){
49                 break;
50             }
51         }
52     }
53     CClientDC dc(this);
54     delete[] str;
55 
56     CString  strFolderPath="D:\\Tao_Music";//判断是否有相应的文件夹,没有则创建
57     if(!PathIsDirectory(strFolderPath))   
58     {
59         while(!CreateDirectory(strFolderPath,NULL)){}   
60     }  
61 
62     if(DownLoad(cidown,(LPSTR)(LPCTSTR)("D:\\Tao_Music\\"+m_song+".txt"))
63         &&DownLoad(songdown,(LPSTR)(LPCTSTR)("D:\\Tao_Music\\"+m_song+".mp3"))){
64         //dc.TextOut(20,160,cidown);    
65         //dc.TextOut(20,120,songdown);
66         addsong((LPSTR)(LPCTSTR)("D:\\Tao_Music\\"+m_song+".mp3"));//加入mylist列表    
67         //实现和上面功能一样的代码:转到+选中+高亮
68         m_StoreItems.EnsureVisible(m_StoreItems.GetItemCount()-1,FALSE);
69         m_StoreItems.SetItemState(m_StoreItems.GetItemCount()-1,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED);   //选中行
70         //m_StoreItems.SetSelectionMark(m_StoreItems.GetItemCount()-1);
71         m_StoreItems.SetFocus();
72             AnalyseLrc((LPSTR)(LPCTSTR)("D:\\Tao_Music\\"+m_song+".txt"));
73     }
74 }
View Code

 

到目前为止已经可以搜索、加载音乐播放、暂停....功能都有啦!

 

似乎音量还不能起作用,下面的列表不能双击播放【上面的显示条有点偏上,大家可以调一下上面的按钮,使两个文本条带正好在黑框里,也可以改动代码,代码在show函数里,通过改动 int tposx,tposy,sposx,sposy;四个变量的值来控制文本显示位置,其中t开头的控制下面的,s开头的控制上面的】

 

7、加一个TImer消息使时间跑起来!

查看->类向导->Message Maps->在Messages中找到WM_TIMER双击,在Member functions将出现对应的消息函数:

 

 

双击对应函数,进入代码编辑区:

加入代码:

1 if(nIDEvent==0)Show(3);

 

现在时间可以跑啦!

 

8、给list添加消息函数,使双击可以播放:

 

点击ok接着双击Member Function对应的函数进入代码编辑区:加入代码:

 1 // TODO: Add your control notification handler code here
 2 int index=m_StoreItems.GetSelectionMark();//获取选中的文本
 3 CString strfilename;
 4 char str[300];
 5 m_StoreItems.GetItemText(index,1,str,sizeof(str));
 6 strfilename.Format(_T("%s"),str);
 7 cursong=strfilename;
 8 
 9 //SetDlgItemText(IDC_filename,strfilename);
10 Mp3.Stop();
11 Mp3.Load(this->m_hWnd,strfilename);
12 Mp3.Play();
13 Mp3.Setvolumn(1000-m_slider.GetPos());//声音设为滑块指示的地方
14 SetTimer(0,1000,NULL);
15 SetTimer(1,100,NULL);
16 SetTimer(2,100,NULL);    
17 
18 GetDlgItem(IDC_open)->EnableWindow(TRUE);
19 GetDlgItem(IDC_pause)->EnableWindow(TRUE);
20 //GetDlgItem(IDC_stop)->EnableWindow(TRUE);
21 GetDlgItem(IDC_del)->EnableWindow(TRUE);
22 
23 Show(0);
24 *pResult = 0;
View Code

 

 

9、给音量控制滑块加消息,使音量控制实现

双击member function对应的函数,进入代码编辑区,加入代码:

1 Mp3.Setvolumn(1000-m_slider.GetPos());
2 UpdateData(false);

 

 

双击member function对应的函数,进入代码编辑区,加入代码:

1 Mp3.Setvolumn(1000-m_slider.GetPos());

 

 

 

 

编译运行完工!

哈哈,终于所有的任务完成,编译运行,听听自己做的音乐播放器咋样!【如果想加一个最小化按钮,就点击form的属性,做相应的修改】

 

 

 

 

posted @ 2014-06-04 21:29  beautifulzzzz  阅读(2951)  评论(3编辑  收藏  举报