首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Delphi MediaPlayer通知停止

Delphi MediaPlayer通知停止
EN

Stack Overflow用户
提问于 2017-10-09 07:08:45
回答 1查看 2.3K关注 0票数 3

我需要在一个MP3应用程序中嵌入一个简单的Delphi7播放器。我只需扫描一个目录,并按随机顺序播放所有文件。

我找到了两个可能的解决方案:一个使用Delphi,另一个使用MediaPlayer PlaySound。

没有人在工作。

问题似乎在于“停止”通知的缺失。像这样使用PlaySound:

代码语言:javascript
复制
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

代码语言:javascript
复制
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。

这里有一个类似的问题。

How can I repeat a song?

但是它不起作用,因为,正如所说,我两次收到nvSuccessfull,没有办法区分开始和停止。

最后但并非最不重要的是,这个应用程序应该可以从XP到Win10,这就是为什么我在WinXP上使用Delphi7进行开发。

谢谢,很抱歉,这篇文章的篇幅很长,但在寻求帮助之前,我确实尝试了很多解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-09 16:33:35

要检测何时加载用于播放的新文件,可以使用OnNotify事件以及TMediaPlayer的EndPosPosition属性(以下称为MP)。

首先设置MP并选择一个TimeFormat,例如

代码语言:javascript
复制
MediaPlayer1.Wait := False;
MediaPlayer1.Notify := True;
MediaPlayer1.TimeFormat := tfFrames;
MediaPlayer1.OnNotify := NotifyProc;

加载要播放的文件时,请设置EndPos属性

代码语言:javascript
复制
MediaPlayer1.FileName := OpenDialog1.Files[NextMedia];
MediaPlayer1.Open;
MediaPlayer1.EndPos := MediaPlayer1.Length;
MediaPlayer1.Play;

OnNotify()程序

代码语言:javascript
复制
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。更改歌曲并开始播放可能不是用户所期望的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46640579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档