How to get music to play in Delphi 7?

心不动则不痛 提交于 2019-12-22 08:36:28

问题


I need to get music to play in the background in the start of the program in the OnFormActivate event for my program. I have the song I want to use but I dont know what command Delphi needs to use in order to start playing that song.

Thanks for you help guys :)


回答1:


Use the TMediaPlayer component, it's on the System tab of the component palette.

procedure TForm1.FormActivate(Sender: TObject);
begin
  MediaPlayer1.FileName := '<fill in>.mp3';
  MediaPlayer1.Open;
  MediaPlayer1.Play;
end;

Set the Visible property to False.


Edit in response to OP's comment:

To repeat the song, you can use the TTimer component, also found on the System tab. To repeat the song with a one second delay:

procedure TForm1.FormActivate(Sender: TObject);
begin
  MediaPlayer1.FileName := '<fill in>.mp3';
  MediaPlayer1.Open;
  MediaPlayer1.TimeFormat := tfMilliseconds;
  Timer1.Interval := MediaPlayer1.Length + 1000;
  MediaPlayer1.Play;
  Timer1.Enabled := True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  MediaPlayer1.Play;
end;

Set the timer's Enabled property to False.




回答2:


You can use TMediaPlayerComponent.
Here you can find a tutorial on how to use it.



来源:https://stackoverflow.com/questions/7765955/how-to-get-music-to-play-in-delphi-7

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