如何将SoundPool对象读入和写入包裹?

时间:2012-11-16 10:45:49

标签: android android-activity parcelable soundpool

初学者。我有一个带有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);
}
*/
}

感谢任何帮助。如果这是完全做事的正确方法,请指出。感谢。

1 个答案:

答案 0 :(得分:0)

您肯定想要将SoundPool转换为Parcel并在活动之间发送。这完全没有效率。

只需将SoundPool对象存储在public static变量中,您就可以从所有活动中访问它。

有关详细信息,请参阅我对Intent and Parcelable object Android

的回答
相关问题