请大家进来领分!(关于TMediaPlayer控件和RES文件) uses windows;PlaySound( LPCSTR pszSound, HMODULE hmod, DWORD fdwSound ); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1,个人认为最简单的方法是:procedure Tdhmenuform.MediaPlayer1Notify(Sender: TObject);beginif mediaplayer1.Position = mediaplayer1.Length thenbeginmediaplayer1.Rewind ;mediaplayer1.Play;end;2,参考一下(这是wav,基本一样):1)编写.RC文件 ..RC文件是资源的源文件,编译器也就编译这个文件,生成.RES的资源文件 首先在我们的项目子目录中建立一个纯文本文件,起名叫Sound.rc,文件中 有一行,内容为: SOUND1 WAV SOUND.WAV 其中SOUND.WAV为一个Windows下普通的声音文件 2)编译它 在DOS的提示符下打 BRCC SOUND.RC 硬盘哗啦啦转一会儿后,就编译完了 3)制作程序 这也是最复杂,最灵活的一步,首先启动Windows, 再启动Delphi, 并且将项目 中的文件保存到我们的项目中的子目录中。 在Unit1.pas中找这么一行 {$R *.DFM} 把我们的资源文件就声明在后面 {$R SOUND.RES} 然后,在Form1中声明两个全局变量 PtrSound : PChar; hRes : THandle; {handle to the loaded resource if 0 indicates nothing playing} 再在Form1的Create事件中写下如下代码 procedure TForm1.FormCreate(Sender: TObject); var hResInfo : THandle; begin hResInfo := FindResource(HInstance, 'SOUND1', 'WAVE'); hRes := LoadResource(HInstance, hResInfo); if hRes > 32 then {its a good load} begin {lock the resource} ptrSound:=LockResource(hRes); end; end; 然后在Form1中放一个按钮Button1,写如下代码: procedure TForm1.Button1Click(Sender: TObject); begin sndplaysound(ptrSound,snd_async or snd_Memory); end; 3,爱莫能助了 给点分我呀!星语心愿(15K)http://www.midicn.com/cgi-pic/bbs/usr/2_164.mid 为何不用delphi做个程序,嵌入一个网页,其背景音乐用bgsound语句搞定呢? 1. 看看这两个贴子: http://www.csdn.net/expert/Topic/395/395059.shtm http://www.csdn.net/expert/Topic/364/364382.shtm2. 用软件 Resource Builder 1.1.0 加入 MIDI 数据, 在汉化新世纪下载3. http://homemidi.myetang.com/midiyy/nuxing/zbz/zbz.htm 小子是想给女朋友做吧? :)方法:在播放的时候设置一个变量为true,当一次播放完毕后(if mediaplayer.position=mediaplayer.length then)将这个变量值设置为false,并且用timer对这个变量值检测,如果检测结果为false则重新播放音乐。在delphi中timer控件能做很多工作比如做出oicq界面的效果,希望能深入研究。--旋峰(Arcobet)计算机软件工作室 我觉得用timer之后,如果系统繁忙,会出现不准确的时候,其实if mediaplayer.position=mediaplayer.length then 再加上一个循环标志就可以搞定 我有几种解决办法:1. 还是用 TMediaPlayer ,装入 res 中的资源后,流出一个临时文件,比如 TResourceStream.SaveToFile('c:\zbz.mid') 然后用 TMediaPlayer 播放,放完后删除。2. 用 MCISendCommand 手工操作(实际上 TMediaPlayer 也是对 MCISendCommand 的封装),先打开 MIDI 设备,然后播放,不过回调事件要自己处理。当然也是建立一个临时文件然后播放。MCISendCommand 没有对内存数据的处理参数。3. 用 midiStreamOpen, midiStreamOut 等可以对内存流进行播放,不过使用起来比较复杂,你可以先看 http://www.borg.com/~jglatt/tech/stream.htm 不懂再讲。如果为了达到目的,比较简便的方法是 1. 而 midiStreamOpen 等则可以完整的达到要求(不需建立临时文件),但是编程相对复杂。以下是一个 C 的例子int main(int argc, char **argv){HMIDISTRM handle;MIDIHDR midiHdr;unsigned long err, tempo;MIDIPROPTIMEDIV prop;MMTIME time; /* Open default MIDI Out stream device */err = 0;if (!(err = midiStreamOpen(&handle, &err, 1, 0, 0, CALLBACK_NULL))){time.wType = TIME_MS; //define the time division type /* Set the stream device's Time Division to milliseconds*/prop.cbStruct = sizeof(MIDIPROPTIMEDIV);prop.dwTimeDiv = 0xE728; //millisecondes : voir midi file spec (key word : E7 28)midiStreamProperty(handle, (LPBYTE)&prop, MIDIPROP_SET|MIDIPROP_TIMEDIV);/* Store pointer to our stream (ie, buffer) of messages in MIDIHDR */midiHdr.lpData = (LPBYTE)&Phrase[0];/* Store its size in the MIDIHDR */midiHdr.dwBufferLength = midiHdr.dwBytesRecorded = sizeof(Phrase);/* Flags must be set to 0 */midiHdr.dwFlags = 0;/* Prepare the buffer and MIDIHDR */midiOutPrepareHeader((HMIDIOUT)handle, &midiHdr, sizeof(MIDIHDR));midiStreamOut(handle, &midiHdr, sizeof(MIDIHDR));midiStreamRestart(handle); // actually play MIDIeventsmidiOutUnprepareHeader((HMIDIOUT)handle, &midiHdr, sizeof(MIDIHDR));midiStreamClose(handle); 方法 3 需要对 MIDI 格式有一定的了解。 to torble(阿裕) :你好,我决定用第一种方法,但有几点问题!1、如何装入RES资源?(请给出步骤)2、如何把RES中的MID数据建立一个临时文件,如建到C:盘根目录下3、请问如何把文件删除?在DELPHI中有关文件删除以及COPY的函数是什么?谢谢了!:) 1. Recourse Builder 的用法很简单,要把一个 MIDI 文件加进去,步骤如下:菜单 资源 -> 添加新类型(这时可以键入一个新的资源类型标识,比如 MIDI),然后光标选中MIDI ,击右键有个添加的菜单,然后把 MIDI 文件添加进去就行了。取个资源名:MUSIC2. 3.unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, MPlayer;type TForm1 = class(TForm) MediaPlayer1: TMediaPlayer; procedure FormDestroy(Sender: TObject); procedure FormCreate(Sender: TObject); private ARStream: TResourceStream; TempFile: string; public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}{$R MID.RES} // 你建立的资源文件procedure TForm1.FormDestroy(Sender: TObject);begin DeleteFile(TempFile); // 删除临时文件 ARStream.Free;end;procedure TForm1.FormCreate(Sender: TObject);begin ARStream := TResourceStream.Create(HInstance, 'MUSIC', 'MIDI'); // 找资源 TempFile := 'C:\TEMP.MID'; ARStream.SaveToFile(TempFile); MediaPlayer1.FileName := TempFile; MediaPlayer1.Open;end;//////////////////////////////////////////////////////////////////////// 复制文件函数procedure CopyFile(const FileName, DestName: string; OverwriteReadOnly, ShellDialog: Boolean; ProgressControl: TControl);var CopyBuffer: Pointer; Source, Dest: Integer; Destination: TFileName; FSize, BytesCopied, TotalCopied: Longint; Attr: Integer;const ChunkSize: Longint = 8192;begin{$IFDEF WIN32} if NewStyleControls and ShellDialog then begin CopyMoveFileShell(FileName, DestName, not OverwriteReadOnly, False, False); Exit; end;{$ENDIF} Destination := DestName; if HasAttr(Destination, faDirectory) then Destination := NormalDir(Destination) + ExtractFileName(FileName); GetMem(CopyBuffer, ChunkSize); try TotalCopied := 0; FSize := GetFileSize(FileName); Source := FileOpen(FileName, fmShareDenyWrite); if Source < 0 then raise EFOpenError.CreateFmt(ResStr(SFOpenError), [FileName]); try if ProgressControl <> nil then begin SetProgressMax(ProgressControl, FSize); SetProgressMin(ProgressControl, 0); SetProgressValue(ProgressControl, 0); end; ForceDirectories(ExtractFilePath(Destination)); if OverwriteReadOnly then begin Attr := FileGetAttr(Destination); if (Attr >= 0) and ((Attr and faReadOnly) <> 0) then FileSetAttr(Destination, Attr and not faReadOnly); end; Dest := FileCreate(Destination); if Dest < 0 then raise EFCreateError.CreateFmt(ResStr(SFCreateError), [Destination]); try repeat BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize); if BytesCopied = -1 then raise EReadError.Create(ResStr(SReadError)); TotalCopied := TotalCopied + BytesCopied; if BytesCopied > 0 then begin if FileWrite(Dest, CopyBuffer^, BytesCopied) = -1 then raise EWriteError.Create(ResStr(SWriteError)); end; if ProgressControl <> nil then SetProgressValue(ProgressControl, TotalCopied); until BytesCopied < ChunkSize; FileSetDate(Dest, FileGetDate(Source)); finally FileClose(Dest); end; finally FileClose(Source); end; finally FreeMem(CopyBuffer, ChunkSize); if ProgressControl <> nil then SetProgressValue(ProgressControl, 0); end;end; 调用存储过程问题 有意出售(FileControlPlus文本编辑器)源代码,大家帮我看看! 怎么样把一个FORM放到自己做的组件里面,高手请指教 在异常处理中用EConverError,需uses哪个单元? 先谢啦! 来看看!绝对送F 怎样才能将程序从内存中清空! delphi 类型转换 我想计算读文件速度!! (easy) 在Delphi里如何知道有没有产生内存泄露? 心情不好,请大家进来领分!(关于TMediaPlayer控件和RES文件) 在线等待!!!Printer.TextOut输出时的位置问题!
procedure Tdhmenuform.MediaPlayer1Notify(Sender: TObject);
begin
if mediaplayer1.Position = mediaplayer1.Length then
begin
mediaplayer1.Rewind ;
mediaplayer1.Play;
end;2,参考一下(这是wav,基本一样):
1)编写.RC文件
..RC文件是资源的源文件,编译器也就编译这个文件,生成.RES的资源文件
首先在我们的项目子目录中建立一个纯文本文件,起名叫Sound.rc,文件中
有一行,内容为:
SOUND1 WAV SOUND.WAV
其中SOUND.WAV为一个Windows下普通的声音文件
2)编译它
在DOS的提示符下打 BRCC SOUND.RC 硬盘哗啦啦转一会儿后,就编译完了
3)制作程序
这也是最复杂,最灵活的一步,首先启动Windows, 再启动Delphi, 并且将项目
中的文件保存到我们的项目中的子目录中。 在Unit1.pas中找这么一行
{$R *.DFM}
把我们的资源文件就声明在后面
{$R SOUND.RES}
然后,在Form1中声明两个全局变量
PtrSound : PChar;
hRes : THandle; {handle to the loaded resource
if 0 indicates nothing playing}
再在Form1的Create事件中写下如下代码
procedure TForm1.FormCreate(Sender: TObject);
var hResInfo : THandle;
begin
hResInfo := FindResource(HInstance, 'SOUND1', 'WAVE');
hRes := LoadResource(HInstance, hResInfo);
if hRes > 32 then {its a good load}
begin {lock the resource}
ptrSound:=LockResource(hRes);
end;
end;
然后在Form1中放一个按钮Button1,写如下代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
sndplaysound(ptrSound,snd_async or snd_Memory);
end; 3,爱莫能助了
星语心愿(15K)
http://www.midicn.com/cgi-pic/bbs/usr/2_164.mid
http://www.csdn.net/expert/Topic/395/395059.shtm
http://www.csdn.net/expert/Topic/364/364382.shtm2. 用软件 Resource Builder 1.1.0 加入 MIDI 数据, 在汉化新世纪下载3. http://homemidi.myetang.com/midiyy/nuxing/zbz/zbz.htm
方法:在播放的时候设置一个变量为true,当一次播放完毕后(if mediaplayer.position=mediaplayer.length then)将这个变量值设置为false,并且用timer对这个变量值检测,如果检测结果为false则重新播放音乐。在delphi中timer控件能做很多工作比如做出oicq界面的效果,希望能深入研究。
--旋峰(Arcobet)计算机软件工作室
if mediaplayer.position=mediaplayer.length then 再加上一个循环标志就可以搞定
{
HMIDISTRM handle;
MIDIHDR midiHdr;
unsigned long err, tempo;
MIDIPROPTIMEDIV prop;
MMTIME time; /* Open default MIDI Out stream device */
err = 0;
if (!(err = midiStreamOpen(&handle, &err, 1, 0, 0, CALLBACK_NULL)))
{
time.wType = TIME_MS; //define the time division type
/* Set the stream device's Time Division to milliseconds*/
prop.cbStruct = sizeof(MIDIPROPTIMEDIV);
prop.dwTimeDiv = 0xE728; //millisecondes : voir midi file spec (key word : E7 28)
midiStreamProperty(handle, (LPBYTE)&prop, MIDIPROP_SET|MIDIPROP_TIMEDIV);/* Store pointer to our stream (ie, buffer) of messages in MIDIHDR */
midiHdr.lpData = (LPBYTE)&Phrase[0];/* Store its size in the MIDIHDR */
midiHdr.dwBufferLength = midiHdr.dwBytesRecorded = sizeof(Phrase);/* Flags must be set to 0 */
midiHdr.dwFlags = 0;/* Prepare the buffer and MIDIHDR */
midiOutPrepareHeader((HMIDIOUT)handle, &midiHdr, sizeof(MIDIHDR));midiStreamOut(handle, &midiHdr, sizeof(MIDIHDR));
midiStreamRestart(handle); // actually play MIDIeventsmidiOutUnprepareHeader((HMIDIOUT)handle, &midiHdr, sizeof(MIDIHDR));
midiStreamClose(handle); 方法 3 需要对 MIDI 格式有一定的了解。
你好,我决定用第一种方法,但有几点问题!
1、如何装入RES资源?(请给出步骤)
2、如何把RES中的MID数据建立一个临时文件,如建到C:盘根目录下
3、请问如何把文件删除?在DELPHI中有关文件删除以及COPY的函数是什么?
谢谢了!:)
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, MPlayer;type
TForm1 = class(TForm)
MediaPlayer1: TMediaPlayer;
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
ARStream: TResourceStream;
TempFile: string;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
{$R MID.RES} // 你建立的资源文件procedure TForm1.FormDestroy(Sender: TObject);
begin
DeleteFile(TempFile); // 删除临时文件
ARStream.Free;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
ARStream := TResourceStream.Create(HInstance, 'MUSIC', 'MIDI'); // 找资源
TempFile := 'C:\TEMP.MID';
ARStream.SaveToFile(TempFile);
MediaPlayer1.FileName := TempFile;
MediaPlayer1.Open;
end;//////////////////////////////////////////////////////////////////////
// 复制文件函数procedure CopyFile(const FileName, DestName: string;
OverwriteReadOnly, ShellDialog: Boolean; ProgressControl: TControl);
var
CopyBuffer: Pointer;
Source, Dest: Integer;
Destination: TFileName;
FSize, BytesCopied, TotalCopied: Longint;
Attr: Integer;
const
ChunkSize: Longint = 8192;
begin
{$IFDEF WIN32}
if NewStyleControls and ShellDialog then begin
CopyMoveFileShell(FileName, DestName, not OverwriteReadOnly,
False, False);
Exit;
end;
{$ENDIF}
Destination := DestName;
if HasAttr(Destination, faDirectory) then
Destination := NormalDir(Destination) + ExtractFileName(FileName);
GetMem(CopyBuffer, ChunkSize);
try
TotalCopied := 0;
FSize := GetFileSize(FileName);
Source := FileOpen(FileName, fmShareDenyWrite);
if Source < 0 then
raise EFOpenError.CreateFmt(ResStr(SFOpenError), [FileName]);
try
if ProgressControl <> nil then begin
SetProgressMax(ProgressControl, FSize);
SetProgressMin(ProgressControl, 0);
SetProgressValue(ProgressControl, 0);
end;
ForceDirectories(ExtractFilePath(Destination));
if OverwriteReadOnly then begin
Attr := FileGetAttr(Destination);
if (Attr >= 0) and ((Attr and faReadOnly) <> 0) then
FileSetAttr(Destination, Attr and not faReadOnly);
end;
Dest := FileCreate(Destination);
if Dest < 0 then
raise EFCreateError.CreateFmt(ResStr(SFCreateError), [Destination]);
try
repeat
BytesCopied := FileRead(Source, CopyBuffer^, ChunkSize);
if BytesCopied = -1 then
raise EReadError.Create(ResStr(SReadError));
TotalCopied := TotalCopied + BytesCopied;
if BytesCopied > 0 then begin
if FileWrite(Dest, CopyBuffer^, BytesCopied) = -1 then
raise EWriteError.Create(ResStr(SWriteError));
end;
if ProgressControl <> nil then
SetProgressValue(ProgressControl, TotalCopied);
until BytesCopied < ChunkSize;
FileSetDate(Dest, FileGetDate(Source));
finally
FileClose(Dest);
end;
finally
FileClose(Source);
end;
finally
FreeMem(CopyBuffer, ChunkSize);
if ProgressControl <> nil then
SetProgressValue(ProgressControl, 0);
end;
end;