首页
社区
课程
招聘
[求助]如何播放VC的资源中的midi音乐? _已解答
发表于: 2006-12-9 19:17 6250

[求助]如何播放VC的资源中的midi音乐? _已解答

2006-12-9 19:17
6250

对于硬盘上的midi文件,很容易播放。
但若把midi存入资源,然后加载该资源,应如何播放的资料始终没有找到。


[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 7
支持
分享
最新回复 (3)
雪    币: 217
活跃值: (99)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
2
我写过一个播放内存中MIDI文件的程序,对于资源一样适用:
http://bbs.pediy.com/showthread.php?s=&threadid=19415
2006-12-9 21:59
0
雪    币: 248
活跃值: (1096)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\advapi32.inc
include \masm32\include\ole32.inc
include \masm32\include\oleaut32.inc
include \masm32\include\comctl32.inc
include \masm32\include\winmm.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\ole32.lib
includelib \masm32\lib\oleaut32.lib
includelib \masm32\lib\advapi32.lib
includelib \masm32\lib\comctl32.lib
includelib \masm32\lib\winmm.lib

include \masm32\macros\macros.asm

S MACRO text
	local lbl
.const
	lbl db text,0
.code
	exitm   <offset lbl>
ENDM

.const
	WINDOW1_ICON_BIG        equ        101
	WINDOW1_PICTURE         equ        102
	IDR_ACCEL_TABLE         equ        103
	MIDI_FILE               equ        104
	RT_FILEDATA             equ        256

.Data?
	hInstance		HINSTANCE ?
	hMIDIres		DD	?		; handle of midi resource
	hMIDISize		DD	?		; size of midi resource and resulting file
	hMIDIdevID		DD	?		; handle of the mci midi device

	mciOpenParms		MCI_OPEN_PARMS <>	; mci open parameters struct
	mciPlayParms		MCI_PLAY_PARMS <>	; play struct

.Data
	szSequencer		DB	'sequencer', 0	; part of the mci command needs this string
	errMsg			DB	"Your PC doesn't seem to support  ", 13, 10,
					"MCI MIDI playback, sorry.", 0
	path			DB	256 Dup(0)	; path of temporary file
	PlayCount		DB	5		; set to 0 to loop MIDI forever
	DlgName			db	"MyDialog",0

.code

; get handle of MIDI resource
GetMIDIHandle Proc

	Invoke FindResource, NULL, MIDI_FILE, RT_FILEDATA	; locate midi resource offset
	Mov hMIDIres, eax											; store temp address in hMIDIres var
	Invoke SizeofResource, NULL, hMIDIres			; how big is the midi resource?
	Mov hMIDISize, eax											; save size address
	Invoke LoadResource, NULL, hMIDIres			; load resource, valid handle in EAX
	Invoke LockResource, eax									; lock resource
	Mov hMIDIres, eax											; save good handle
	mov eax,FALSE
	ret 

GetMIDIHandle EndP

MakeMIDIfile Proc ; output midi file from internal raw data resource
	Local fhandle1:DWord
	Local numchar:DWord

	invoke GetCurrentDirectory,248,Addr path
	Invoke lstrcat, Addr path, S("\~tmp.bin")			
	Invoke MessageBox, 0, Addr path, S("Tempfile for Playing"), MB_OK	 
	Invoke CreateFile, Addr path, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
	Mov fhandle1, Eax						 
	Invoke WriteFile, fhandle1, hMIDIres, hMIDISize, Addr numchar, 0 ; output midi file from resource
	Invoke CloseHandle, fhandle1					; close our file
	mov eax,FALSE
	ret 

MakeMIDIfile EndP

GetMIDIdevID Proc ; get midi device ID and test for capability
	
	Mov mciOpenParms.lpstrDeviceType, Offset szSequencer 	; MCI_DEVTYPE_SEQUENCER does not work!
	Mov mciOpenParms.lpstrElementName, Offset path 		; this is the path and filename to open

	Invoke mciSendCommand, hMIDIdevID, MCI_OPEN, MCI_OPEN_ELEMENT + MCI_OPEN_TYPE, Offset mciOpenParms
	m2m hMIDIdevID,mciOpenParms.wDeviceID		; to hMIDIdevID variable

	.If Eax != 0					; mci call status in eax, if MIDI
		Invoke MessageBox, 0, addr errMsg, S("Errot"), MB_OK	 ; not supported, popup error
		Call EndMIDIfile											; delete our temporary file
		Invoke ExitProcess, 0										; and exit
	.EndIf
	mov eax,FALSE
	ret 

GetMIDIdevID EndP

PlayMIDI Proc ; play midi stream
	
	Invoke mciSendCommand, hMIDIdevID, MCI_PLAY, MCI_NOTIFY, Offset mciPlayParms
	.If Eax != 0													; if not supported,
		Invoke MessageBox, 0, S("Can't do that"), S("Error"), MB_OK		
		Call EndMIDIfile											; delete our temporary file
		Invoke ExitProcess, 0										; and exit
	.EndIf
	mov eax,FALSE
	ret
	
PlayMIDI EndP

StopMIDI Proc ; stop midi playback
	
	Invoke mciSendCommand, hMIDIdevID, MCI_CLOSE, MCI_OPEN_ELEMENT + MCI_OPEN_TYPE, Offset mciPlayParms
	mov eax,FALSE
	ret
	
StopMIDI EndP

EndMIDIfile Proc ; delete the MIDI file we created
	
	Invoke DeleteFile, Addr path
	mov eax,FALSE
	ret
	
EndMIDIfile EndP

DlgProc Proc Private hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM

	.If uMsg == WM_INITDIALOG			
							
		m2m mciPlayParms.dwCallback,hWnd	
							
		Invoke GetMIDIHandle 			
		Invoke MakeMIDIfile			
		Invoke GetMIDIdevID			

	.ElseIf uMsg == WM_COMMAND
		mov eax,wParam
		mov edx,wParam
		shr edx,16
		.if edx==BN_CLICKED
			.if ax==1001	
				Invoke PlayMIDI				
			.elseif ax==1002
				mov PlayCount,1
				Invoke StopMIDI				
				Invoke GetMIDIdevID
			.endif
		.endif
	.ElseIf uMsg == MM_MCINOTIFY			
		.If wParam == MCI_NOTIFY_SUCCESSFUL 	
			.If PlayCount != 0									
				Invoke mciSendCommand, hMIDIdevID, MCI_SEEK, MCI_SEEK_TO_START, Offset mciPlayParms
				Invoke mciSendCommand, hMIDIdevID, MCI_PLAY, MCI_NOTIFY, Offset mciPlayParms
				Dec PlayCount
			.Else					
				Invoke EndMIDIfile		
				Invoke ExitProcess, 0	
			.EndIf
		.EndIf
	.ElseIf uMsg == WM_CLOSE			
		Invoke StopMIDI				
		Invoke EndMIDIfile			
		invoke EndDialog,hWnd,0
		mov eax,TRUE
		ret 
	.EndIf
	mov eax,FALSE
	ret 
DlgProc EndP

start:
	invoke GetModuleHandle, NULL
	mov    hInstance,eax
	invoke DialogBoxParam, hInstance, ADDR DlgName,NULL, addr DlgProc, NULL
	invoke ExitProcess,eax


end start


#include "\masm32\include\resource.h"

#define xxx_ICON 101
#define WINDOW1_PICTURE 102
#define IDR_ACCEL_TABLE 103
#define MIDI_FILE 104
#define RT_FILEDATA 256

MyDialog DIALOG DISCARDABLE 200, 200, 200, 66
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Demo on Playing a MIDI File in Resource"
FONT 9, "MS Sans Serif"
BEGIN
DEFPUSHBUTTON "&Play",1001,35,20,50,18
PUSHBUTTON "&Stop",1002,108,20,50,18
END

xxx_ICON ICON DISCARDABLE "xxx.ico"
MIDI_FILE RT_FILEDATA DISCARDABLE "music.mid"
上传的附件:
2006-12-10 00:46
0
雪    币: 721
活跃值: (350)
能力值: ( LV9,RANK:1250 )
在线值:
发帖
回帖
粉丝
4
感谢dwing和sixL的帮忙。
2006-12-12 20:39
0
游客
登录 | 注册 方可回帖
返回
//