Search code examples
androidaudiomaui

plugin.maui.audio: Duration is 100 times too long on Android


I am using Plugin.Maui.Audio to play short sounds in an educational program to give feedback, like 'excellent' or 'not quite'. The attribute Duration, which is a double and should provide the duration in seconds is OK on Windows but 100 times too long on Android. So the code below works for Windows, but it does not for Android:

audioSoundPlayer.Play();
int milliSecondes = (int)audioSoundPlayer.Duration * 1000;
Thread.Sleep(milliSecondes);

I want to wait until the sound has ended because after playing a sound I give out additional information using TTS.

I know that double types sometimes cause issues depending on the Culture Settings because of the decimal separator. But I do not do any conversions. The debugger displays the values below: Duration=3.4 (Windows) Duration=340 (Android)


Solution

  • I missed that there is an AsyncAudioPlayer which does the job perfectly.

    AsyncAudioPlayer audioPlayer = null;
    audioPlayer = AudioManager.Current.CreateAsyncPlayer(await FileSystem.OpenAppPackageFileAsync("wav/salamisound_bierflasche_mit_plopp_oeffnen.wav"));
    await audioPlayer.PlayAsync(cancellationToken);