Beginner here. I have a class with a SoundPool object in it. I would like to have the class implement Parcelable so that its instance can be passed between activities (so that I only load the sounds once).
But I am facing a problem in writing the SoundPool object into the parcel. May I know if it is possible at all? (Maybe SoundPool does not implement Parcelable?)
public class SoundSamples { // implements Parcelable{
SoundPool sound;
AudioManager am;
int soundId[] = new int[4];
Context ctxt;
public SoundSamples(Context context) {
ctxt = context;
}
public void makeSound() {
Context context = ctxt;
sound = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundId[0] = sound.load(context, R.raw.c4, 0);
soundId[1] = sound.load(context, R.raw.cs4, 0);
soundId[2] = sound.load(context, R.raw.d4, 0);
soundId[3] = sound.load(context, R.raw.ds4, 0);
}
public SoundPool getSound() {
return sound;
}
public int[] getSoundId() {
return soundId;
}
/*
private SoundSamples(Parcel in) {
// STUCK HERE
sound = in.readParcelable(null);
soundId = in.readArray(Integer);
}
public static final Parcelable.Creator<SoundSamples> CREATOR
= new Parcelable.Creator<SoundSamples>() {
public SoundSamples createFromParcel(Parcel in) {
return new SoundSamples(in);
}
public SoundSamples[] newArray(int size) {
return new SoundSamples[size];
}
};
public void writeToParcel(Parcel out, int flags) {
// AND STUCK HERE
out.writeIntArray(soundId);
out.writeParcelable((Parcelable)sound,0);
}
*/
}
Any help is appreciated. Do point out if this is the correct way to do things at all. Thanks.
You definitely do not want to convert a
SoundPoolto aParceland send it between activities. This is totally inefficient.Just store the
SoundPoolobject in apublic staticvariable and you can access it from all your activities.For more information, see my answer to Intent and Parcelable object Android