开发者

Loading MP3s into the Sound Pool in Android

开发者 https://www.devze.com 2023-03-28 06:41 出处:网络
I want to load a couple of MP3s into the sound pool, but I will only be using these MP3s on Activity 3, is it possible to load the MP3s in Activity 1开发者_开发百科?

I want to load a couple of MP3s into the sound pool, but I will only be using these MP3s on Activity 3, is it possible to load the MP3s in Activity 1开发者_开发百科?

Or is this limited to the Activity that loads it uses it?

I can't find the answer in the docs.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消