1

我想将几个 MP3 加载到声音池中,但我只会在 Activity 3 中使用这些 MP3,是否可以在 Activity 1 中加载 MP3?

或者这是否仅限于加载它使用它的 Activity?

我在文档中找不到答案。

4

1 回答 1

2

您可以随时加载它们并在任何地方使用它们。重用该SoundPool对象的最好办法是扩展Application该类并在其中声明一个私有变量,即您的SoundPool. 就像是:

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;
    }
}

现在,您可以在代码的任何位置运行:

MyApp.getInstance().getSoundPool();

您将可以访问您的全局 SoundPool 对象。

PS:如果您扩展应用程序类,请不要忘记更新清单。

于 2011-08-15T04:52:09.277 回答