mplayer.c

来自「minigui PDA系统 可实现手机功能」· C语言 代码 · 共 394 行

C
394
字号
#include <stdio.h>
#include <stdlib.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <stdarg.h>
#include <errno.h>

#define IDC_BUTTON 100
#define IDL_DIR    100
#define IDL_FILE   110
#define IDC_PATH   120
static char fullpath2[PATH_MAX + 1];
static char sys[1024];
static BITMAP bmp_bkgnd;

/*static void mpUnloadBmps (void)
{
    UnloadBitmap (&bmp_bkgnd);
}*/
static DLGTEMPLATE DlgDelFiles =
{
		WS_BORDER | WS_CAPTION, WS_EX_NONE,
		0, 0, 320, 216, "选择播放的文件",
		0, 0, 7, NULL, 0
};
static CTRLDATA CtrlDelFiles[] =
{ 
		{
				CTRL_STATIC,
				WS_VISIBLE | SS_SIMPLE, 
				10, 2, 120, 25, IDC_STATIC,
		    		"目录列表", 0
		},
		{
				CTRL_LISTBOX,
				WS_VISIBLE | WS_VSCROLL | WS_BORDER | LBS_SORT | LBS_NOTIFY,
				10, 20, 140, 110, IDL_DIR, "", 0
		},
		{
				CTRL_STATIC,
				WS_VISIBLE | SS_SIMPLE, 
				165, 2, 120, 25, IDC_STATIC, 
				"文件列表", 0
		},
		{
				CTRL_LISTBOX,
				WS_VISIBLE | WS_VSCROLL | WS_BORDER | LBS_SORT | LBS_AUTOCHECKBOX,
				165, 20, 140, 110,
				IDL_FILE,
				"", 0
		},
		{
			CTRL_STATIC,
			WS_VISIBLE | SS_SIMPLE, 
			10, 150, 315, 0, 
			IDC_PATH, 
			"DIRECTION:",
			0
	},
	{
		"button",
		WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP | WS_GROUP,
		15, 145, 130, 25,
		IDOK, 
		"播 放",
		0
	},
	{
		"button",
		WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
		170, 145, 130, 25,
		IDCANCEL,
		"取 消",
		0
	},
};
//提取要播放的文件路径
static void fill_boxes1 (HWND hDlg, const char* path)
{
#ifdef __ECOS__
	LISTBOXITEMINFO lbii;

	lbii.string = "file.1";
	lbii.cmFlag = CMFLAG_BLANK;
	lbii.hIcon = 0;
	SendDlgItemMessage (hDlg, IDL_FILE, LB_ADDSTRING, 0, (LPARAM)&lbii);

	lbii.string = "file.2";
	SendDlgItemMessage (hDlg, IDL_FILE, LB_ADDSTRING, 0, (LPARAM)&lbii);

	lbii.string = "file.3";
	SendDlgItemMessage (hDlg, IDL_FILE, LB_ADDSTRING, 0, (LPARAM)&lbii);
#else
	struct dirent* dir_ent;
	DIR*   dir;
	struct stat ftype;
	char   fullpath [PATH_MAX + 1];

	SendDlgItemMessage (hDlg, IDL_DIR, LB_RESETCONTENT, 0, (LPARAM)0);
	SendDlgItemMessage (hDlg, IDL_FILE, LB_RESETCONTENT, 0, (LPARAM)0);
	SetWindowText (GetDlgItem (hDlg, IDC_PATH), path);
   
	if ((dir = opendir (path)) == NULL)
         return;

	while ( (dir_ent = readdir ( dir )) != NULL ) 
	{
		/* Assemble full path name. */
		strncpy (fullpath, path, PATH_MAX);
		strcat (fullpath, "/");
		strncpy(fullpath2, fullpath, PATH_MAX+1);
		strcat (fullpath, dir_ent->d_name);
		if (stat (fullpath, &ftype) < 0 ) {
		continue;
	}

	if (S_ISDIR (ftype.st_mode))
		SendDlgItemMessage (hDlg, IDL_DIR, LB_ADDSTRING, 0, (LPARAM)dir_ent->d_name);
	else if (S_ISREG (ftype.st_mode)) 
		{
			LISTBOXITEMINFO lbii;
			lbii.string = dir_ent->d_name;
			lbii.cmFlag = CMFLAG_BLANK;
			lbii.hIcon = 0;
			SendDlgItemMessage (hDlg, IDL_FILE, LB_ADDSTRING, 0, (LPARAM)&lbii);
		}
	}
	//printf("%s\n",fullpath2);
	closedir (dir);
#endif
}
//处理路径
static void dir_notif_proc1 (HWND hwnd, int id, int nc, DWORD add_data)
{
	if (nc == LBN_CLICKED || nc == LBN_ENTER) 
	{
		int cur_sel = SendMessage (hwnd, LB_GETCURSEL, 0, 0L);
		if (cur_sel >= 0) 
		{
			char cwd [MAX_PATH + 1];
			char dir [MAX_NAME + 1];
			GetWindowText (GetDlgItem (GetParent (hwnd), IDC_PATH), cwd, MAX_PATH);
			SendMessage (hwnd, LB_GETTEXT, cur_sel, (LPARAM)dir);

			if (strcmp (dir, ".") == 0)
				return;
			strcat (cwd, "/");
			//printf("cwd=%s\n",cwd);
			strcat (cwd, dir);
			//printf("cwd=%s\n",cwd);
			fill_boxes1 (GetParent (hwnd), cwd);
		}
	}
}

static void file_notif_proc1 (HWND hwnd, int id, int nc, DWORD add_data)
{
	/* Do nothing */
}
//提取要播放的文件
static void prompt1 (HWND hDlg)
{
	int i;
	char files [1024] = "您选择的文件时:\n";
	for (i = 0; i < SendDlgItemMessage (hDlg, IDL_FILE, LB_GETCOUNT, 0, 0L); i++)
	{
		char file [MAX_NAME + 1];
		int status = SendDlgItemMessage (hDlg, IDL_FILE, LB_GETCHECKMARK, i, 0);
		if (status == CMFLAG_CHECKED)
		{
			memset(sys,0,sizeof(sys));
			strcpy(sys,"./fun/mplayer/mplayer ");
			SendDlgItemMessage (hDlg, IDL_FILE, LB_GETTEXT, i, (LPARAM)file);
			strcat (files, file);
			strcat (files, "\n");
			//printf("fullpath2=%s\n",fullpath2);
			system("killall mplayer");
			strcat (sys, fullpath2);
			strcat (sys, file);
			strcat (sys, " ");
			strcat (sys, " -zoom -x 270 -y 215&");
			system(sys);
			//sys[27] = '\0';
		}
	}
	MessageBox (hDlg, files, "OK", MB_OK | MB_ICONINFORMATION);
}

static int DelFilesBoxProc (HWND hDlg, int message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case MSG_INITDIALOG:
		{
			char cwd [MAX_PATH + 1];
			SetNotificationCallback (GetDlgItem (hDlg, IDL_DIR), dir_notif_proc1);
			SetNotificationCallback (GetDlgItem (hDlg, IDL_FILE), file_notif_proc1);
			fill_boxes1 (hDlg, getcwd (cwd, MAX_PATH));
			return 1;
		}       
		case MSG_COMMAND:
		switch (wParam)
		{
			case IDOK:
			prompt1 (hDlg);
			case IDCANCEL:
			EndDialog (hDlg, wParam);
			break;
		}
		break; 
	}
	return DefaultDialogProc (hDlg, message, wParam, lParam);
}
//打开文件的函数
int MplayerUrl (HWND hWnd)
{

	DlgDelFiles.controls = CtrlDelFiles;
	DialogBoxIndirectParam (&DlgDelFiles, hWnd, DelFilesBoxProc, 0L);
	return 0;
}
//----------------------------------------------------------------------------------

//设置MPLAYER界面各个控件
static int HelloWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
		{
		case MSG_CREATE:			//设置4个按钮
			CreateWindow (CTRL_BUTTON,
			"文件",
			WS_CHILD | BS_PUSHBUTTON | BS_CHECKED | WS_VISIBLE,
			IDC_BUTTON,
			260, 50, 42, 25, hWnd, 0);

			CreateWindow (CTRL_BUTTON,
			"MP3",
			WS_CHILD | BS_PUSHBUTTON | BS_MULTLINE | WS_VISIBLE,
			IDC_BUTTON + 1,
			260, 80, 42, 25, hWnd, 0);

		/*	CreateWindow (CTRL_BUTTON,
			"暂停",
			WS_CHILD | BS_PUSHBUTTON | BS_MULTLINE | WS_VISIBLE,
			IDC_BUTTON + 2,
			260, 80, 42, 25, hWnd, 0);
		*/

			CreateWindow (CTRL_BUTTON,
			"停止",
			WS_CHILD | BS_PUSHBUTTON | BS_MULTLINE | WS_VISIBLE,
			IDC_BUTTON + 3,
			260, 110, 42, 25, hWnd, 0);
			
			CreateWindow (CTRL_BUTTON,
			"返回",
			WS_CHILD | BS_PUSHBUTTON | BS_MULTLINE | WS_VISIBLE,
			IDC_BUTTON + 4,
			260, 140, 42, 25, hWnd, 0);
		break;

		case MSG_ERASEBKGND:
		{	
		   	HDC hdc = (HDC)wParam;
	    			const RECT* clip = (const RECT*) lParam;
	    			BOOL fGetDC = FALSE;
	    			RECT rcTemp;
		    
	    			if (hdc == 0) {
					hdc = GetClientDC (hWnd);
					fGetDC = TRUE;
	    			}       
	    			if (clip) {
					rcTemp = *clip;
					ScreenToClient (hWnd, &rcTemp.left, &rcTemp.top);
					ScreenToClient (hWnd, &rcTemp.right, &rcTemp.bottom);
					IncludeClipRect (hdc, &rcTemp);
	   		 	}

	    			FillBoxWithBitmap (hdc, 0, 0, 320, 206, &bmp_bkgnd);

	    			if (fGetDC)
					ReleaseDC (hdc);
			return 0;
		}

		case MSG_CLOSE:				//关闭MPLAYER(全部)
			printf("close!!!\n");
			system("killall mplayer");
			DestroyMainWindow (hWnd);
			PostQuitMessage (hWnd);
			return 0;
			
		case MSG_COMMAND:
		//各个按钮的功能定义
			switch (wParam)
			{
				case IDC_BUTTON:			//文件按钮调用MplayerUrl函数
					MplayerUrl(hWnd);	
					printf("文件\n");
					break;
				case (IDC_BUTTON+1):
					printf("mp3!!!\n");
					//system("killall mplayer");	//播放指定路径的所有MP3文件
					system("killall lame");
					//system("./fun/mplayer/mplayer ./music/*.mp3 *.MP3 &");
					system("./fun/mplayer/lame ./music/*.mp3 *.MP3 &");
					printf("mp3\n");
					break;
		/*	case (IDC_BUTTON+2):
				system("killall mplayer");	//播放指定路径的所有AVI文件
				system("./mplayer ./music/*.avi -zoom -x 270 -y 215 &");
				printf("mp4\n");
				break;*/
				case (IDC_BUTTON + 3):		//停止按钮,关闭MPLAYER
					printf("stop!!!\n");
					system("killall mplayer");
					//system("killall lame");
					printf("停止\n");
					break;
				
				case (IDC_BUTTON + 4):
					DestroyMainWindow (hWnd);
					PostQuitMessage (hWnd);       	
					break;
			}
       		break;
			}
			
	return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
//MPALYER入口函数
int mplayer(HWND hWnd)
//int MiniGUIMain (int args, const char* arg[])
{

	int i=99;
	MSG Msg;
	HWND hMainWnd;
	MAINWINCREATE CreateInfo;


	CreateInfo.dwStyle = WS_VISIBLE | WS_BORDER | WS_CAPTION;
	CreateInfo.dwExStyle = WS_EX_NONE;
	CreateInfo.spCaption = "MPLAYER";
	CreateInfo.hMenu = 0;
	CreateInfo.hCursor = GetSystemCursor(0);
	CreateInfo.hIcon = 0;
	CreateInfo.MainWindowProc = HelloWinProc;
	CreateInfo.lx = 0;
	CreateInfo.ty = 0;
	CreateInfo.rx = 320;
	CreateInfo.by = 216;
	CreateInfo.iBkColor = COLOR_lightwhite;
	CreateInfo.dwAddData = 0;
	CreateInfo.hHosting = hWnd;

	if (LoadBitmap (HDC_SCREEN, &bmp_bkgnd, "image/background/bkgnd29.jpg"))	//设置MPLAYER的界面背景
	return 1;
	
	

	hMainWnd = CreateMainWindow (&CreateInfo);
	
	if (hMainWnd == HWND_INVALID)
	return -1;	//  -1

	ShowWindow(hMainWnd, SW_SHOWNORMAL);

	while (GetMessage(&Msg, hMainWnd)) 		//消息队列
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}

	UnloadBitmap (&bmp_bkgnd);
  	//mpUnloadBmps();
	MainWindowThreadCleanup (hMainWnd);
	return 0;
}




⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?