Using MediaPlayer slows down SoundPool sound effect

20 Views Asked by At

I'm using MediaPlayer for background song and SoundPool for onButtonClick sound effects. I've noticed that while MediaPlayer playing background song SoundPool starts to give slow response for method SoundPool.Play. So I click on button and I hear a slight latency between button release and sound effect. If MediaPlayer stops there is no more latency between button release and sound effect.

What can be the issue?

    public partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();
            _soundIdClick = _clickSoundPool.Load(Android.App.Application.Context.Assets!.OpenFd("click.ogg"), 1);
            _musicMedaPlayer.SetDataSource(Android.App.Application.Context.Assets!.OpenFd("song.ogg"));
            _musicMedaPlayer.Prepare();
            Appearing+=OnAppearing;
        }

        private void OnAppearing(object sender, EventArgs e)
        {
            _musicMedaPlayer.Start();
        }

        private readonly MediaPlayer _musicMedaPlayer = new MediaPlayer();
        private readonly SoundPool _clickSoundPool = new SoundPool(5, Stream.Music, 0);
        private readonly int _soundIdClick;
    
        private void ImageButton_OnReleased(object sender, EventArgs e)
        {
            _clickSoundPool.Play(_soundIdClick, 1, 1, 0, 0, 1);            
        }
    }
0

There are 0 best solutions below