How can I repeat a song?

陌路散爱 提交于 2019-12-13 17:03:09

问题


I have a TMediaPlayer called MediaPlayer1 I then open a file(a song) I play it. now my problem is that I need the song to repeat until the program stops.

The idea is that the form activates and then repeats the specified song until the form is closed.

MediaPlayer1.Filename := 'filename';

Then it opens it

MediaPlayer1.Open;

Then it plays it

MediaPlayer1.Play;

So now the song is playing but when it ends I want it to play again(repeat) and then again until the form is closed.

I tried what David Heffernan said but it does not work, I think I did something wrong can someone pleas correct me.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, MPlayer, StdCtrls;

type
  TForm1 = class(TForm)
    MediaPlayer1: TMediaPlayer;
    Label1: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure MediaPlayer1Notify(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormActivate(Sender: TObject);
begin
 mediaplayer1.FileName:='E:\it project\mario.mid';
 mediaplayer1.Open;
 mediaplayer1.AutoRewind:=true;
 mediaplayer1.Play;
 mediaplayer1.Notify:=true;
end;

procedure TForm1.MediaPlayer1Notify(Sender: TObject);
begin
  if MediaPlayer1.NotifyValue=nvSuccessful then begin
    MediaPlayer1.Play;  
    MediaPlayer1.Notify := True;
  end;
end;
end.

回答1:


You have to organise the auto repeat for yourself.

Create an OnNotify event for the media player. This fires when the song completes.

procedure TForm1.MediaPlayer1Notify(Sender: TObject);
begin
  if MediaPlayer1.NotifyValue=nvSuccessful then begin
    //restart the song
    MediaPlayer1.Play;  
    MediaPlayer1.Notify := True;//ensures we are notified when song completes
  end;
end;

The code that starts the song needs to look like this:

MediaPlayer1.AutoRewind := True;//for obvious reasons
MediaPlayer1.Play;
MediaPlayer1.Notify := True;//ensures we are notified when song completes



回答2:


Just use TMediaPlayer: http://docwiki.embarcadero.com/CodeExamples/en/MediaPlayer_%28Delphi%29



来源:https://stackoverflow.com/questions/12656296/how-can-i-repeat-a-song

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!