我需要在Delphi7应用程序中嵌入一个普通的MP3播放器。
我将仅扫描目录并以随机顺序播放所有文件。

我发现了两种可能的解决方案:一种使用Delphi MediaPlayer,另一种使用PlaySound Windows API。

没有任何工作。

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

playsound(pchar(mp3[r].name), 0, SND_ASYNC or SND_FILENAME);


我找不到办法(礼貌地)要求Windows在歌曲停止播放时通知我。

使用Delphi MediaPlayer,Internet充满了彼此复制/粘贴的建议,例如:

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,但是在开始播放歌曲时也会得到NotifyValue == nvSuccessfull,所以我不能依靠它。
此外,根据我发现的所有示例,我从来没有收到过“ mode”属性状态的变化,该变化应该变为mpStopped。

这里有一个类似的问题

How can I repeat a song?

但是它不起作用,因为如上所述,我两次收到nvSuccessfull,而无法区分开始和停止。

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

感谢您,对本文的篇幅感到抱歉,但是在寻求帮助之前,我确实尝试了许多解决方案。

最佳答案

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

首先设置MP并选择一个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。更改歌曲并开始播放可能不是用户期望的。

关于delphi - 关于歌曲的Delphi MediaPlayer通知已停止,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46640579/

10-13 06:51