いくつかの MP3 をサウンド プールにロードしたいのですが、これらの MP3 はアクティビティ 3 でのみ使用します。アクティビティ 1 で MP3 をロードすることは可能ですか?
それとも、これはそれを使用するアクティビティに限定されていますか?
ドキュメントで答えが見つかりません。
いつでもロードして、どこでも使用できます。オブジェクトを再利用する最善の方法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: アプリケーション クラスを拡張する場合は、マニフェストを更新することを忘れないでください。