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

一笑奈何 提交于 2019-12-13 09:09:41

问题


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?


回答1:


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 assets\internal

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 assets\internal. 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



回答2:


It seems there's a newer version of the 'GameAudioManager' unit (now dubbed "AudioManager") that is compatible with Android on Delphi Tokyo. I reviewed the code and it seems to just remove the "onload" callback and 'loaded' check-loop.

Here is the updated code: https://github.com/Embarcadero/DelphiArcadeGames/blob/master/Full/AlienInvasion/AudioManager.pas



来源:https://stackoverflow.com/questions/49734013/is-there-an-alternative-to-tmediaplayer-for-multi-platform-rapid-sound-effects

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