Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 748
user2980457 Avatar asked Nov 17 '13 06:11

user2980457


1 Answers

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');
like image 133
user2980457 Avatar answered Oct 21 '22 10:10

user2980457