xamarinandroid-mediaplayeraudioeffect

Adding Reverb effect to MediaPlayer in 'Mobile App (Xamarin.Forms)' project


I have created an MP3 player in a Xamarin project, and it plays great. I have added the ability to adjust the Pitch and Speed, but I cannot get the Reverb effect to take to my MediaPlayer.

I have found various examples in Kotlin and Java of the code required to create both PresetReverb and EnvironmentalReverb methods to achieve this.

While my C# code runs and produces no errors, the Reverb effect will not apply using either method. I am new to Xamarin and Android, so I am hoping to get some help.

Here is my code.

MediaPlayer _mediaPlayer = new MediaPlayer();

AssetFileDescriptor fd = global::Android.App.Application.Context.Assets.OpenFd(fileName);

#region REVERB

PresetReverb presetReverb = new PresetReverb(1, 0); // was set to _mediaPlayer.AudioSessionId
presetReverb.Preset = PresetReverb.PresetLargeroom;
presetReverb.SetEnabled(true);
_mediaPlayer.AttachAuxEffect(presetReverb.Id);
_mediaPlayer.SetAuxEffectSendLevel(1.0f);

#endregion

_mediaPlayer.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
_mediaPlayer.Prepare();
_mediaPlayer.Start();

Any ideas/advice would be greatly appreciated.


Solution

  • You could try Audio session 0 instead of the AudioSessionId

      PresetReverb presetReverb = new PresetReverb(1, 0);
                presetReverb.Preset = PresetReverb.PresetLargehall;
                presetReverb.SetEnabled(true);
                _mediaPlayer.AttachAuxEffect(presetReverb.Id);
                _mediaPlayer.SetAuxEffectSendLevel(1.0f);