How to implement SoundPool SetOnLoadCompleteListener

klemikazeklemikaze USMember ✭✭
edited April 2015 in Xamarin.Android

Hi. I have tried but failed in finding some example implementation of this method. As I understood, there is class of OnLoadCompleteListener on android, so while implementing, you just create new instance and override one method, but Xamarins SoundPool defines only interface IOnLoadCompleteListener. I tried creating class and inheriting the interface, but when I put breakpoint into OnLoadComplete method, it never gets hit...
public class AudioPlayer : IAudioPlayer
{
private SoundPool pool;

        public IAudioPlayer Initialize(string[] files, int tempo)
            {
                pool = new SoundPool(3, Stream.Music, 1);
                pool.SetOnLoadCompleteListener(new OnLoadCompleteListener());
            pool.LoadComplete += pool_LoadComplete;
                foreach (var file in files)
                {
                        var fd = MainActivity.AssetsInstance.OpenFd(file);
                        pool.Load(fd, 1);
                }       
                return this;
        }

        void pool_LoadComplete(object sender, SoundPool.LoadCompleteEventArgs e)
        {
    //breakpoint here - never gets hit
                    throw new NotImplementedException();
        }

        protected class OnLoadCompleteListener : SoundPool.IOnLoadCompleteListener
        {
                public void OnLoadComplete(SoundPool soundPool, int sampleId, int status)
                {
            //breakpoint here - never gets hit
                    throw new NotImplementedException();
                }

                public IntPtr Handle
                {
                    get { throw new NotImplementedException(); }
                }

                public void Dispose()
                {
                    throw new NotImplementedException();
                }
        }
    }

}

Answers

  • CasseyLottmanCasseyLottman USMember

    For anyone finding this later - the solution is to make sure the class that you want to implement IOnLoadCompleteListener extends Java.Lang.Object. If you do that, you won't have to implement Dispose() and IntPtr Handle yourself.

    class MyOnLoadCompleteListener : Java.Lang.Object, SoundPool.IOnLoadCompleteListener 
     {
             public void OnLoadComplete(SoundPool soundPool, int sampleId, int status)
             {
                 throw new NotImplementedException();
             }
     }
    
Sign In or Register to comment.