初学者在这里。我有一个带有 SoundPool 对象的类。我想让这个类实现 Parcelable 以便它的实例可以在活动之间传递(这样我只加载一次声音)。
但是我在将 SoundPool 对象写入包裹时遇到了问题。我可以知道这是否可能吗?(也许 SoundPool 没有实现 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);
}
*/
}
任何帮助表示赞赏。请指出这是否是正确的做事方式。谢谢。