Raj
Raj

Reputation: 59

How To Play Sound on Android Firemonkey

I have the following code:

uses
  MMSystem;

procedure TForm3.AddSound;
var
  hFind, hRes: THandle;
  Song: PChar;
begin
  hFind := FindResource(HInstance, 'Resource_1', RT_RCDATA);
  if hFind <> 0 then begin
    hRes:=LoadResource(HInstance, hFind);
    if hRes <> 0 then begin
      Song:=LockResource(hRes);
      if Assigned(Song) then SndPlaySound(Song, snd_ASync or snd_Memory);
      UnlockResource(hRes);
    end;
    FreeResource(hFind);
  end;
end;

It works fine in Windows when calling this procedure in the OnClick event of a button, but the same code is not working for Android. I get the following error:

[DCC Fatal Error] Unit3.pas(8): F2613 Unit 'MMSystem' not found.

Upvotes: 0

Views: 3886

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596497

This code doesn't work on Android because MMSystem is not a cross-platform unit in the FireMonkey library. It is a Windows-only unit in the VCL library instead. Mobile projects must use FireMonkey only, not VCL.

To play sounds on Android, look at FireMonkey's TMediaPlayer component. However, it cannot play audio from resources, so you will have to deploy the audio file along side your app and play it from file instead.

See Embarcadero's Audio-Video in FireMonkey documentation for more details.

Upvotes: 1

Related Questions