c语言编写的播放器代码

作者在 2011-04-18 17:21:34 发布以下内容
#include "stdio.h"
#include <windows.h>
#include <windowsx.h>
#include "d:\resource.h"
#include "MainDlg.h"
#include <mmsystem.h>
#include <commdlg.h>
#include <fcntl.h>
#include <io.h>
static UINT PresentSongcurSel  = 0;   //当前播放歌曲
static UINT PauseFlag    = 0;   //暂停Flag 时为非暂停
static long CircleListFlag   = 0;   //列表循环Flag
static int totalSecond    = 0;   //当前歌曲播放时长 /s
static int TimerSecond           = 0;   //定时器总时间     /s
static TCHAR ShortPathName[256];    //文件最短路径
static UINT StylePlay            = 0;   //0 列表循环
            
//1单曲循环
            
//2随机播放
            
//3
static UINT OpenStytle    = 0;   //为1时打开自动播放
static char SongName[256];
void GetSongTime(HWND hwnd);
void RreadList(HWND hwnd);      //读取歌曲例表声明
void RreadList2(HWND hwnd);
void SsaveList(HWND hwnd);      //保歌歌曲例表声明
void AddSongs(HWND hwnd);      //添加歌曲声明
void GetSongsName(HWND hwnd, TCHAR *line, UINT Lenth);
void ClosePrensentSong(HWND hwnd);    //关闭当前播放歌曲
void PlaySong(HWND hwnd, UINT curSel);   //播放选中歌曲
void PausePrensentSong(HWND hwnd);    //暂停当前歌曲
void RePausePrensetSong(HWND hwnd);    //继续当前歌曲

void TotletimeSe(HWND hwnd);     //计算当前歌曲的总时间,并现实

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);
  HANDLE_MSG(hWnd,WM_CTLCOLORDLG,rupeng);
    }
    return FALSE;
}
void CALLBACK TimerProc(HWND hwnd,UINT nMsg,UINT nTimerid,DWORD dwTime)
{

if (PauseFlag == 0)
{
  TimerSecond++;
  if (TimerSecond >= totalSecond)
  {
   if (StylePlay == 0)
   {
    PlaySong(hwnd,(PresentSongcurSel+1));
   }
   else if (StylePlay == 1)
   {
    PlaySong(hwnd,PresentSongcurSel);
   }
   else if (StylePlay == 2)
   {
    HWND hwndCombo = GetDlgItem(hwnd,IDC_LIST);
    int sum = ListBox_GetCount(hwndCombo);
    int num = (rand() % sum);
    PlaySong(hwnd,num);
   }
   TimerSecond = 0;
  }
}
}
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
RreadList(hwnd);        //读取歌曲例表,并反回打开播放方式
RreadList2(hwnd);
FILE *fp = fopen("E:/泸诚音乐文件1.txt","a+");
rewind(fp);
TCHAR str[1];
str[0] = fgetc(fp);
str[2] = '\0';
OpenStytle = atoi(str);
if (OpenStytle == 1)
{
  PlaySong(hwnd,0);         //打开播放列表第一首歌
}
HWND hwndPlayStyle = GetDlgItem(hwnd,IDC_PLAYSTYLE);
SetWindowText(hwndPlayStyle,"列表");
return TRUE;
}
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{

switch(id)
{
  case IDC_ADDSONG:          //列表添加歌曲
  {
   AddSongs(hwnd);  
  }
   break;
  case IDC_PLAY:           //播放选中歌曲
  {
   HWND hwndCombo = GetDlgItem(hwnd,IDC_LIST);
   int curSel = ListBox_GetCurSel(hwndCombo);
   PlaySong(hwnd,curSel);
  }  
  break;
  case IDC_LIST:
  {
   switch(codeNotify)
   {
   case LBN_DBLCLK:         //播放双击选中歌曲
   {
    ClosePrensentSong(hwnd);
    HWND hwndCombo = GetDlgItem(hwnd,IDC_LIST);
    int curSel = ListBox_GetCurSel(hwndCombo);
    PlaySong(hwnd, curSel);
   }
    break;
   }
  }
  break;
  case IDC_UPSONG:          //上一首
   {
    ClosePrensentSong(hwnd);
    PlaySong(hwnd,PresentSongcurSel-1);
   }
   break;
  case IDC_DOWNSONG:          //下一首
   {
    ClosePrensentSong(hwnd);
    PlaySong(hwnd,PresentSongcurSel+1);
   }
   break;
  case IDC_PAUSE:           //暂停当前歌曲
   {
    if (PauseFlag==0)
    {
     PausePrensentSong(hwnd);
     HWND hwndCombo = GetDlgItem(hwnd,IDC_PAUSE);
     SetWindowText(hwndCombo,TEXT(":〈"));    //改变按钮显示
    }
    else
    {
     RePausePrensetSong(hwnd);
     SetTimer(hwnd,0,1000,TimerProc);
     HWND hwndCombo = GetDlgItem(hwnd,IDC_PAUSE);
     SetWindowText(hwndCombo,TEXT(" :-〉"));    //改变按钮显示
    }
   }
   break;
  case IDC_CLOSE:           //关闭当前歌曲
   {  
    ClosePrensentSong(hwnd);
    KillTimer(hwnd, 0);
   }
   break;
  case IDC_SAVESONG:          //保存列表
   {
    SsaveList(hwnd);
   }
   break;
  case IDC_DELETESONG:         //删除选中歌曲
  {
   HWND hwndCombo = GetDlgItem(hwnd,IDC_LIST);
   HWND hwndCombo2 = GetDlgItem(hwnd,IDC_LIST2);
   int curSel = ListBox_GetCurSel(hwndCombo);
   ListBox_DeleteString(hwndCombo,curSel);
   ListBox_SetCurSel(hwndCombo,curSel);
   ListBox_DeleteString(hwndCombo2,curSel);
   ListBox_SetCurSel(hwndCombo2,curSel);
  }
   break;
  case IDC_DELETEALL:          //清空列表
   {
    HWND hwndCom = GetDlgItem(hwnd,IDC_LIST);
    HWND hwndCombo2 = GetDlgItem(hwnd,IDC_LIST2);
    int  k = ListBox_GetCount(hwndCom);
    int i;
    for(i = (k-1);i >= 0;i--)    //*********不能用**********
    {            //for(i=0;i<sum;i++)
     ListBox_DeleteString(hwndCom,i);   //ListBox_DeleteString
     ListBox_DeleteString(hwndCombo2,i);   //因为从前面开始删,删第一项后,
    }           //第二项会成为第一项,在删第二
   }            //项时,就没有露了这一项
   break;          
  case ID_MENUITEMLIST:        //列表循环播放
  {
   StylePlay = 0;
   SetDlgItemText(hwnd,IDC_PLAYSTYLE,"列表循环");
  }
   break;
  case ID_MENUITEMSINGLE:         //单曲循环
   {
    StylePlay = 1;
    SetDlgItemText(hwnd,IDC_PLAYSTYLE, "单曲循环");
   }  
   break;
  case ID_MENUITEMSJ:          //随机播放
   {
    StylePlay = 2;
    SetDlgItemText(hwnd,IDC_PLAYSTYLE, "随机循环");
   }  
   break;
  case ID_MENUITEMSTYTLE:         //打开时的播发模式
   {
    
    if (OpenStytle == 0)    
    {
     FILE *fp;
     fp = fopen("E:/泸诚音乐文件1.txt","w");
     rewind(fp);
     fputc('1',fp);        //设置打开自动播放
     fclose(fp);
    }
    else if (OpenStytle == 1)
    {
     FILE *fp;
     fp = fopen("E:/泸诚音乐文件1.txt","w");
     rewind(fp);
     fputc('0',fp);        //设置打开不播放
     fclose(fp);
    }
   }
   break;
  case ID_MENUITEMHELP:
   {
    MessageBox(NULL,TEXT("Help,请联系泸诚,QQ:270871232:)"),TEXT("帮助"),MB_OK);
   }
   break;
  case ID_MENUITEMB:
   {
    MessageBox(NULL,TEXT("泸诚2.0正式版,2010年12月29号,21:09完成"),TEXT("版本"),MB_OK);
   }
   break;
        default:
  break;
    }
}
void Main_OnClose(HWND hwnd)
{
    EndDialog(hwnd,0);
}
HBRUSH g_hbrush = CreateSolidBrush(RGB(90,180,160));  //创建笔刷
HBRUSH rupeng(HWND hwnd, HDC hdc, HWND hwndChild, int type)
{
return g_hbrush;
}
//************读取歌曲列表****************
void RreadList(HWND hwnd)
{
FILE *fp;
fp = fopen("E:/泸诚音乐文件.txt","a+");
rewind(fp);
TCHAR line[256];
int i=0;
for (i=0;!feof(fp);i++)        
{
  
  line[i] = fgetc(fp);        

  if (line[i] == '*')
  {
   line[i] = '\0';
   HWND hwndCombo = GetDlgItem(hwnd,IDC_LIST);
   ListBox_AddString(hwndCombo,line);
   i=(-1);
  }
}
if (i != 1)            //空文件时,fp会加1,所以读出数
{
  line[i] = '\0';
  HWND hwndCombo = GetDlgItem(hwnd,IDC_LIST);
  MessageBox(NULL,line,TEXT("2"),MB_OK);
  ListBox_InsertString(hwndCombo,-1,line);
}
fclose(fp);
}
void RreadList2(HWND hwnd)
{
FILE *fp;
fp = fopen("E:/泸诚音乐文件2.txt","a+");
rewind(fp);
TCHAR line[256];
int i=0;
for (i=0;!feof(fp);i++)        
{
  
  line[i] = fgetc(fp);        

  if (line[i] == '*')
  {
   line[i] = '\0';
   HWND hwndCombo = GetDlgItem(hwnd,IDC_LIST2);
   ListBox_AddString(hwndCombo,line);
   i=(-1);
  }
}
if (i != 1)            //空文件时,fp会加1,所以读出数
{
  line[i] = '\0';
  HWND hwndCombo = GetDlgItem(hwnd,IDC_LIST2);
  ListBox_InsertString(hwndCombo,-1,line);
}
fclose(fp);
}
//************保存歌曲列表****************
void SsaveList(HWND hwnd)
{
FILE *fp = fopen("E:/泸诚音乐文件.txt","w");
HWND hwndCombo = GetDlgItem(hwnd,IDC_LIST);
int Count = ListBox_GetCount(hwndCombo);
TCHAR SongPath[256];
TCHAR SongPathX[256];
int i = 0;
for (i = 0; i < Count; i++)
{
  ListBox_GetText(hwndCombo,i,SongPath);
  wsprintf(SongPathX,"%s*",SongPath);
  fputs(SongPathX,fp);
}
fclose(fp);
FILE *fp2 = fopen("E:/泸诚音乐文件2.txt","w");
HWND hwndCombo2 = GetDlgItem(hwnd,IDC_LIST2);
int Count2 = ListBox_GetCount(hwndCombo2);
TCHAR SongPath2[256];
TCHAR SongPathX2[256];
int i2 = 0;
for (i2 = 0; i2 < Count2; i2++)
{
  ListBox_GetText(hwndCombo2,i2,SongPath2);
  wsprintf(SongPathX2,"%s*",SongPath2);
  fputs(SongPathX2,fp2);
}
fclose(fp2);
}
//************添加歌曲****************
void AddSongs(HWND hwnd)
{
  OPENFILENAME ofn;
  TCHAR szOpenFileNames[80*MAX_PATH];
  TCHAR szPath[MAX_PATH];
  TCHAR szFileName[MAX_PATH];  
  TCHAR szFileNamePath[MAX_PATH];
  TCHAR* p;
  int nLen = 0;
  ZeroMemory( &ofn, sizeof(ofn) );
  ofn.Flags = OFN_EXPLORER | OFN_ALLOWMULTISELECT;
  ofn.lStructSize = sizeof(ofn);
  ofn.lpstrFile = szOpenFileNames;
  ofn.nMaxFile = sizeof(szOpenFileNames);
  ofn.lpstrFile[0] = '\0';
  ofn.lpstrFilter = TEXT("ALL\0*.*\0MP3\0*.MP3\0");
  if( GetOpenFileName( &ofn ) )
  {  
  lstrcpyn(szPath, szOpenFileNames, ofn.nFileOffset );
  szPath[ ofn.nFileOffset ] = '\0';
     nLen = lstrlen(szPath);
  
     if( szPath[nLen-1] != '\\' )   //如果选了多个文件,则必须加上'\\'
  {
   lstrcat(szPath, TEXT("\\"));
  }
  
     p = szOpenFileNames + ofn.nFileOffset; //把指针移到第一个文件
  
     ZeroMemory(szFileName, sizeof(szFileName));
  ZeroMemory(szFileNamePath, sizeof(szFileNamePath));
     while( *p )
  {  
   lstrcpy(szFileNamePath, szPath); //给文件加上路径
   lstrcpy(szFileName, p);    //只加上文件名
   lstrcat(szFileNamePath, p);   //给文件加上名字
   HWND hwndCombo2 = GetDlgItem(hwnd,IDC_LIST2);
   ListBox_InsertString(hwndCombo2,-1,szFileNamePath);
    HWND hwndCombo = GetDlgItem(hwnd,IDC_LIST);
   ListBox_InsertString(hwndCombo,-1,szFileName);
   p += lstrlen(p) +1;     //移至下一个文件
  }
  }
}

//******************播放歌曲**********************
void PlaySong(HWND hwnd, UINT curSel)
{
TCHAR str[256];
TCHAR strPath[256];
TCHAR cmd[MAX_PATH +10];
ClosePrensentSong(hwnd);
PresentSongcurSel = curSel;
HWND hwndCombo = GetDlgItem(hwnd,IDC_LIST);
UINT sum = ListBox_GetCount(hwndCombo);
if (PresentSongcurSel < sum)
{
  HWND hwndCombo2 = GetDlgItem(hwnd,IDC_LIST2);
  HWND hwndSong = GetDlgItem(hwnd,IDC_SONGNAME);    
  ListBox_GetText(hwndCombo2,curSel,strPath);  
  ListBox_GetText(hwndCombo,curSel,str);
  ListBox_SetCurSel(hwndCombo,PresentSongcurSel);
  SetWindowText(hwndSong,str);       //显示歌曲名字
  GetShortPathName(strPath,ShortPathName,sizeof(ShortPathName));
  TotletimeSe(hwnd);
  wsprintf(cmd,"play %s",ShortPathName);
  mciSendString(cmd,"",0,NULL);  
  SetTimer(hwnd,0,1000,TimerProc);
  if (PauseFlag == 1)
  {    
   HWND hwndCombo = GetDlgItem(hwnd,IDC_PAUSE);
   SetWindowText(hwndCombo,TEXT(" :-〉"));
  }
}
else
{
  PlaySong(hwnd,0);
}
}
//*****************暂停当前歌曲********************
void PausePrensentSong(HWND hwnd)
{
TCHAR cmd[MAX_PATH +10];
wsprintf(cmd,"pause %s",ShortPathName);
mciSendString(cmd,"",0,NULL);
PauseFlag = 1;
}
//*****************继续当前歌曲*********************
void RePausePrensetSong(HWND hwnd)
{
TCHAR cmd[MAX_PATH +10];
wsprintf(cmd,"resume %s",ShortPathName);
mciSendString(cmd, NULL, 0, NULL);
PauseFlag = 0;
}

//*************关闭当前播放歌曲**********************
void ClosePrensentSong(HWND hwnd)
{
TCHAR cmd[MAX_PATH +10];
wsprintf(cmd,"close %s",ShortPathName);
mciSendString(cmd,"",0,NULL);
}
/*****    
函数名称:  MusicTotalTime  
函数功能: 计算一首歌的总时间
****
*/
void TotletimeSe(HWND hwnd)
{
TCHAR musicTotalLength[256];
int intSecond;

默认分类 | 阅读 1738 次
文章评论,共8条
尤慕思
2011-04-24 22:42
1
这样的代码现在还不能接受,<br />
给自己加油——加油!<img src="image/face/2.gif" class="face">
fucyou
2011-04-27 21:42
2
<img src="image/face/8.gif" class="face"> 编译有错误啊,大哥
陈潍溢
2011-05-04 14:03
3
代码太长了,不过很强大啊<img src="image/face/16.gif" class="face">
变幻小子
2011-05-19 07:35
4
太强了。<br />
为自己好好加油
松涛雨露
2011-05-28 11:03
5
代码好像不完整
kemoo
2011-06-04 11:25
6
这个好像有点问题啊
plm240195
2011-06-05 08:35
7
就是!
wanxl688517
2011-06-27 19:33
8
的确不完整&nbsp;&nbsp;加油
游客请输入验证码
浏览1738次
文章分类
文章归档