How to play system sound in Delphi FireMonkey XE4?

一世执手 提交于 2019-12-11 01:49:25

问题


I want to play system sound in both Windows and MAC OS X. To play sound in Windows used something like this:

PlaySound('C:\Windows\Media\Windows Default.wav', 0, SND_FILENAME + SND_ASYNC);

I am sure I have to use TMediaPlayer.


回答1:


You are correct, you can use the TMediaPlayer component, documentation clearly states :

TMediaPlayer playbacks audio files and the audio component of a video file.

To specify the media file to be played by the current TMediaPlayer, set the FileName property. The file name must include the path of the file, so it can be localized on the memory, and the extension. Call the Play and Stop methods to start playing a media file, or to stop or pause a running media file. The current position is specified through the CurrentTime property.

TMediaPlayer also exposes media file properties such as Duration, VideoSize, Volume or State.

Regarding the supported file formats (TMediaCodecManager).

The documentation states the following:

Use TMediaCodecManager to access, manage, and register codecs to be used when playing media files.

The supported media files formats are the native formats for each platform:

For Windows:
Audio formats: .wma, .mp3, .wav
Video formats: .avi,.wmv

For Mac OS/iOS:
Audio formats: .mp3, .caf
Video formats: .mov, .m4v, .mp4

Using TMediaCodecManager, it is possible to register custom media codecs to extend the audio/video feature support.

Quick example :

procedure TForm1.Button1Click(Sender: TObject);
begin
 MediaPlayer1.FileName := 'D:\test.mp3';
 MediaPlayer1.Play;
end;

You should be able to play mp3 files in Firemonkey across platforms but that is out of scope of this question.



来源:https://stackoverflow.com/questions/18576772/how-to-play-system-sound-in-delphi-firemonkey-xe4

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