Plugin.Maui.Audio icon indicating copy to clipboard operation
Plugin.Maui.Audio copied to clipboard

Playback latency

Open githubjanson opened this issue 1 year ago • 3 comments
trafficstars

In my mobile app, I need a short sound to play when I tap a button. And I need as good precision as possible, that is, as short latency between the tap and the sound as possible.

But the latency is approximately 150-200 ms when running the app on a (modern) iPhone, which is not good enough for my application. There are similar apps out there (non-MAUI), where this lag is significantly less, so it is not hardware related.

  • I am using a really small wav (7 kB).
  • I am preparing the audio player once, loading the stream when the app loads.
  • The only action when tapping the button is invoking Play() or PlayAsync().
  • I am running the app on device (no emulator).
  • Tried running in Release mode without debugging, no (noticable) difference.

I assume that this lag is not related to the Maui.Audio plugin, but perhaps someone has a clue or workaround how to get around this issue? Do I need to switch to another platform?

Thanks.

githubjanson avatar Jan 03 '24 09:01 githubjanson

It is interesting that you are seeing some latency. Out of interest how are you measuring this latency?

A couple of things that I would try:

  1. Check that your sound starts right at the beginning of the file and there isn't any silence mimicking latency.
  2. If you are using a Command/Clicked event swap to using the Pressed event on the Button to see if you can get a quicker response.

bijington avatar Jan 04 '24 09:01 bijington

@bijington, thanks for your tips! Changing to the Pressed event didn't help but made me realize that the latency is in the Button control and not related to the sound playback at all. When I press the button (and hold) there is a delay before the button "reacts". For the default MAUI button this is shown by the text turning grey. And at that exact moment, my sound plays.

So what I need is a button with shorter response time.

Regarding the latency, I have not measured it but rather estimated it by comparison. I am developing a rythmic exercise app, and I guess a latency around 50 ms would be acceptable, 25 ms would be great.

githubjanson avatar Jan 04 '24 13:01 githubjanson

@githubjanson out of curiosity did you find a solution for this issue? If not, are you using compiled bindings or have you filed an issue in the MAUI repo?

borrmann avatar Apr 25 '24 18:04 borrmann

Seems unlikely a problem with Plugin.Maui.Audio. Other MAUI elements don't seem to have a latency but I noticed a delay on button clicks as well. I have tried calling play from event triggers of pressed and clicked. Then from inside a custom button OnPressed.

I am also curious to know @githubjanson if you found a solution.

its-jefe avatar Jun 03 '24 16:06 its-jefe

Hi @borrmann, @its-jefe,

I skipped the Button control and am using the SKCanvasView/Touch event instead. This gives me the best experience, i.e., a minimal latency beteen the tap and the sound.

githubjanson avatar Jun 06 '24 12:06 githubjanson

Looks there is a solution. For a next time it would be super helpful if you could include a reproduction project so we can have a look together with you and easily see if this reproduces on our end as well as well as maybe fix it. For now closing this. If there is still something that needs to be investigated here, please let us know!

jfversluis avatar Aug 07 '24 14:08 jfversluis