Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

delphi - Is there an alternative to TMediaPlayer for multi platform rapid sound effects?

I am looking for a way to play multiple rapid sound effects (WAV format) in a multi-platform game (Android and Windows) with low overhead.

I tried using the TMediaPlayer component, but creating multiple instances adds a lot of overhead to startup and shutdown times.

I researched and found this code which offers a low-overhead approach. Sadly, the code is incompatible with Delphi 10.2 (Tokyo) under Android (windows works fine). Under Android, the onLoadComplete is never triggered, causing the GLoaded flag to remain false and the app to freeze. http://www.fmxexpress.com/free-game-audio-manager-wrapper-class-in-delphi-xe6-firemonkey-for-android-ios-windows-and-osx/

Does anyone know of an alternative solution or can understand why the GameAudioManager callback function fails to trigger?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

EDIT: My initial test was done with Berlin while OP is using Tokyo. The results below hold true for Berlin. Please see further down for Tokyo.

I was curious as to why the code in the FMXExpress link you provided did not work since I use very similar code in my multi-platform audio class. So I decided to download the code and give it a try.

The demo app looks for 3 specific files ding1.wav, ding2.wav and ding3.wav; on Windows it looks for the files in the documents folder and on Android it expects the files in assetsinternal

For Windows I had to copy some audio files I had to the documents folder and rename them accordingly. The app worked fine as you reported. (Windows 10 Version 1709).

For Android I first took a look at the project's deployment settings. The 3x audio files are listed but the local path is blank. I just unchecked the existing entries and re-added them from my documents folder and set the remote path for each to assetsinternal. Compiled the app directly on a device I had connected and it worked fine. Android SDK 24.3.3 32-bit, LG-V522 G-Pad III running Android 7.0

EDIT - DESCRIPTION OF PROBLEM WITH TOKYO:

The OP is correct; the linked sample code does not work when compiled with Tokyo 10.2.3

Simple debugging showed that the callback TOnSpoolLoadCallBack.onLoadComplete is never called which is required to set the global GLoaded to True. The effect is that the application loops indefinitely in the following code:

TGameAudioManager.AddSound

while not GLoaded do
begin
  Sleep(10);
  Application.ProcessMessages;
end;

To make the code work, (this is a hack) add a counter that limits the number of time the loop can run. Something like this...

procedure TGameAudioManager.AddSound(...)
const
  MaxWaitLoop = 10;
var
  ...
  loopCount : integer;
Begin
  ...
  loopCount := 0;
  while not GLoaded AND (loopCount < MaxWaitLoop) do
  begin
    inc(loopCount);
    Sleep(10);
    Application.ProcessMessages;
  end;
  ...

With some logic to prevent indefinite looping, the application now works and audio is heard on the Android device.

Notes:

  • You now need to figure out why the callback does not work in Tokyo. After a quick check and comparison with Berlin it wasn't obvious to me.
  • You should not use this code in production. Application.ProcessMessages almost always points to a bad implementation. Instead, the callback should bubble up to your main application where you can, for example, enable UI elements

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...