Alca
Alca

Reputation: 81

Playing sound in firemonkey

I use Firemonkey to deploy an android apps, with audio files on that. I have about 30 animal button, and each of button will heard animals sound when user select it. Here is my code for the first button:

procedure TFMain.buttonLionClick(Sender: TObject);
begin
  MediaPlayer1.FileName := 'D:\lion.mp3';
  MediaPlayer1.Play;
end;

But it is failed. How can I deploying that sounds in to my apps?

Upvotes: 2

Views: 5955

Answers (3)

Vepir
Vepir

Reputation: 602

First of all, if you want to build your app for android you need to convert the .mp3 to .3gp to be able to play it. (You can use this site: http://video.online-convert.com/convert-to-3gp ;just upload your file and click convert.)

Secondly, you need to include your files in your resources; go to project>resources and images then select to look at all files and then select your .3gp files and add them.

Now you can make a procedure to play sounds and music from your resources: (using TMediaPlayer)

procedure TForm1.PlayAudio(ResourceID: string); //PLAY MUSIC FROM RESOURCES
var
  ResStream: TResourceStream;
  TmpFile: string;
begin

  ResStream := TResourceStream.Create(HInstance, ResourceID, RT_RCDATA);
  try

    TmpFile := TPath.Combine(TPath.GetDocumentsPath, 'tmp.3gp');

    //TPath.Combine(TPath.GetDocumentsPath, 'filename')  { Internal }
    //TPath.Combine(TPath.GetSharedDocumentsPath, 'filename')  { External }

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

    MediaPlayer1.FileName := TmpFile;
    MediaPlayer1.Play;

  finally
    ResStream.Free;
  end;

end;

Note: You need to do this like this because you can't play your files directly from your assets where your resources will be saved when deploying to android.

Now to play music just use for example: PlayAudio('Resource_1')

Note: Before playing a new audio, you need to stop the previous one using:

MediaPlayer1.Stop;
MediaPlayer1.Clear;

And if you want to loop your music then you can use TTimer to replay your sound after it ends.

Note: this works specifically on android. On Windows use .mp3, .wav or something like that and TPath.Combine(TPath.GetTempPath, 'tmp.wav') to get your path on windows.

Upvotes: 6

Dsm
Dsm

Reputation: 6013

Your fundamental problem is a lack of understanding of the differences between different file systems. A few (Windows, DOS) use the drive style notification (a letter followed by a ':') but many (e.g. Unix derivatives, OS X, iOS, etc) do not, seeing the drive name as just part of the more general file structure. In addition windows/dos use '\' as a separator and most others use '/'. Fire monkey provides many special locations and separators to help you build generic code.

Upvotes: 1

Jerry Dodge
Jerry Dodge

Reputation: 27296

An Android application uses files which are on the Android device. D:\test.mp3 tells me this file resides on a Windows computer, perhaps your development machine. That's not how it works, Android isn't aware of files on your development machine. You need to use a file which is on the Android device, and reference that path instead (using an appropriate path structure which Android will recognize).

It seems that you want to Deploy these files to the Android device. In that case, study:

http://docwiki.embarcadero.com/RADStudio/XE8/en/Deployment_Manager

Upvotes: 3

Related Questions