Play a sound alert/beep on Delphi XE5/Android platform

不羁的心 提交于 2019-12-09 03:36:19

问题


Is there any way to play a sound alert/beep on Delphi XE5/Android platform?

What I'm trying to achieve is how to play system alert/beep sound like Windows application using Beep function or at least find the path of system's audio files so I can run specific audio file based on an event.


回答1:


I ended up using resource files to play my custom audio file.

Steps:

  1. From Delphi IDE click on "Project".
  2. Then select "Resources and Images...".
  3. Choose your media file and set it as RCDATA.
  4. Remember your resource identifier.

Note: Make sure the media type is supported by TMediaPlayer otherwise it won't work.

Delphi Procedure:

procedure PlayAudio(ResourceID: string);
var
  ResStream: TResourceStream;
  TmpFile: string;
begin
  ResStream := TResourceStream.Create(HInstance, ResourceID, RT_RCDATA);
  try
    TmpFile := TPath.Combine(TPath.GetTempPath, 'tmp.mp3');

    ResStream.Position := 0;
    ResStream.SaveToFile(TmpFile);
    MediaPlayer1.FileName := TmpFile;

    MediaPlayer1.Play;

  finally
    ResStream.Free;
  end;
end;

Usage:

PlayAudio('Resource_1');


来源:https://stackoverflow.com/questions/20027769/play-a-sound-alert-beep-on-delphi-xe5-android-platform

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