我需要在Delphi7应用程序中嵌入一个简单的MP3播放器。 我将简单地扫描一个目录并随机播放所有文件。
我找到了两个可能的解决方案:一个使用Delphi MediaPlayer,另一个使用PlaySound Windows API。
都不起作用。
问题似乎在于缺少“停止”通知。像这样使用PlaySound:
playsound(pchar(mp3[r].name), 0, SND_ASYNC or SND_FILENAME);
我找不到一种(礼貌的)方法来请求Windows在歌曲停止播放时通知我。使用Delphi MediaPlayer,互联网上充斥着各种建议,例如这里:
http://www.swissdelphicenter.ch/en/showcode.php?id=689
http://delphi.cjcsoft.net/viewthread.php?tid=44448
procedure TForm1.FormCreate(Sender: TObject);
begin
MediaPlayer1.Notify := True;
MediaPlayer1.OnNotify := NotifyProc;
end;
procedure TForm1.NotifyProc(Sender: TObject);
begin
with Sender as TMediaPlayer do
begin
case Mode of
mpStopped: {do something here};
end;
//must set to true to enable next-time notification
Notify := True;
end;
end;
{
NOTE that the Notify property resets back to False when a
notify event is triggered, so inorder for you to recieve
further notify events, you have to set it back to True as in the code.
for the MODES available, see the helpfile for MediaPlayer.Mode;
}
我的问题是,在一首歌曲结束时确实会得到NotifyValue == nvSuccessfull,但也会在开始播放歌曲时得到,因此我不能依赖它。 此外,无论如何我从未收到“mode”属性状态的更改,这应该根据我找到的所有示例变为mpStopped。
这里有一个类似的问题 如何重复播放一首歌? 但是它不起作用,因为如上所述,我会收到两次nvSuccessfull,而无法区分开始和结束。
最后但并非最不重要的,这个应用程序应该从XP到Win10都能工作,这就是我在WinXP上使用Delphi7进行开发的原因。
谢谢,对于这篇文章的长度,我很抱歉,但在寻求帮助之前,我真的尝试了很多解决方案。
mciSendString
。您可以通过AllocateHWnd
创建一个不可见的窗口,并 监听MM_MCINOTIFY
消息。 - VictoriaMCI_WAIT
标志(而不是MCI_NOTIFY
)是另一种方法。或者有一个线程并调用PlaySound
而不使用SND_ASYNC
标志。另一个相对简单的选择可能是DirectShow API。 - Victoria