Loading MP3s into the Sound Pool in Android

这一生的挚爱 提交于 2019-12-02 05:17:07

You can load them anytime and use them everywhere. The best thing to re-use the SoundPool object would be to extend the Application class and declare a private variable in there that is your SoundPool. Something like:

class MyApp extends Application {
    private static MyApp singleton;

    private static SoundPool mSoundPool;

    public onCreate() {
         super.onCreate();
         singleton = this;
         mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); Just an example
    }

    public static MyApp getInstance() {
         return singleton;
    }

    public SoundPool getSoundPool() {
         return mSoundPool;
    }
}

Now, anywhere on you code you can run:

MyApp.getInstance().getSoundPool();

and you'll have access to your global SoundPool object.

PS: don't forget to update the Manifest if you extend the Application Class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!