Delphi MediaPlayer歌曲停止通知
我需要在Delphi7应用程序中嵌入一个普通的MP3播放器。 我会简单地扫描一个目录并以随机顺序播放所有文件。Delphi MediaPlayer歌曲停止通知
我发现了两种可能的解决方案:一种使用Delphi MediaPlayer,一种使用PlaySound Windows API。
没有工作。
该问题似乎是在缺少“停止”通知。 使用PlaySound这样的:
playsound(pchar(mp3[r].name), 0, SND_ASYNC or SND_FILENAME);
我无法找到一个办法(礼貌地)让Windows通知我,当一首歌曲停止播放。
使用德尔福的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开发的原因。
非常感谢您对本文的长度感到抱歉,但在寻求帮助之前,我确实尝试了很多解决方案。
要检测时加载播放一个新的文件,你可以使用OnNotify
事件和TMediaPlayer(以下简称MP)
先设定MP的EndPos
和Position
属性,然后选择一个TimeFormat
,例如
MediaPlayer1.Wait := False;
MediaPlayer1.Notify := True;
MediaPlayer1.TimeFormat := tfFrames;
MediaPlayer1.OnNotify := NotifyProc;
当你加载播放文件,设置EndPos
属性
MediaPlayer1.FileName := OpenDialog1.Files[NextMedia];
MediaPlayer1.Open;
MediaPlayer1.EndPos := MediaPlayer1.Length;
MediaPlayer1.Play;
而且OnNotify()
程序
procedure TForm1.NotifyProc(Sender: TObject);
var
mp: TMediaPlayer;
begin
mp:= Sender as TMediaPlayer;
if not (mp.NotifyValue = TMPNotifyValues.nvSuccessful) then Exit;
if mp.Position >= mp.EndPos then
begin
// Select next file to play
NextMedia := (NextMedia + 1) mod OpenDialog1.Files.Count;
mp.FileName := OpenDialog1.Files[NextMedia];
mp.Open;
mp.EndPos := mp.Length;
mp.Position := 0;
mp.Play;
// Set Notify, important
mp.Notify := True;
end;
end;
最后您尝试使用MP.Mode = mpStopped
模式中的注释改变到一个新的歌曲。当按钮被操作时,模式被改变,当用户按下“停止”按钮时,该模式将变为mpStopped
。改变歌曲并开始播放可能不会是用户所期望的。
最简单的可能是'mciSendString'。你会为'MM_MCINOTIFY'消息创建一个不可见的窗口(通过'AllocateHWnd')和_listen_。 – Victoria
谢谢。一般来说,直接用Windows API进行编程并不是我的“面包和黄油”,但是阅读MSDN上的MM_MCINOTIFY,它看起来像是Delphi映射到“NotifyValue”属性上的东西,所以我想我需要一些更多帮助来区分之间:) – ZioBit
@维多利亚 - 听起来很有趣的解决方案 - 但如果你只是使用API而不依赖于VCL,你能不能只使用一个线程呢?我有同样的问题(不同的应用程序,但同样的问题)。 Windows的原始版本(以及我在这里讨论的XP)在通知模式下非常稳定。后来的人不是。所以我也很好奇一个好的解决方案。 – Dsm