我试图释放我的soundPool时遇到错误

时间:2015-03-24 12:35:53

标签: android release soundpool

我试图释放我的声音池,但我不太清楚我在做什么。我是否正确发布了声音池对象?

我在这段代码上遇到错误,“”标记内的位:     int sampleId =“(int)v.getTag();” - 为什么是这样?我该如何纠正?

我是否也正确发布了声音池?

public class FragmentOne extends Fragment implements  View.OnClickListener ,  SoundPool.OnLoadCompleteListener {

SoundPool Clubb1;
Integer currentBtnId;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState
) {
    View rootView = inflater.inflate(R.layout.fragment_one_layout, container, false);
    Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);

    Button buttonA = (Button) rootView.findViewById(R.id.buttonA);
    Button buttonB = (Button) rootView.findViewById(R.id.buttonB);
    Button buttonC = (Button) rootView.findViewById(R.id.buttonC);
    Button buttonD = (Button) rootView.findViewById(R.id.buttonD);
    Button buttonE = (Button) rootView.findViewById(R.id.buttonE);
    Button buttonF = (Button) rootView.findViewById(R.id.buttonF);
    Button buttonG = (Button) rootView.findViewById(R.id.buttonG);
    Button buttonH = (Button) rootView.findViewById(R.id.buttonH);
    Button buttonI = (Button) rootView.findViewById(R.id.buttonI);
    Button buttonJ = (Button) rootView.findViewById(R.id.buttonJ);

    buttonA.setTag(R.raw.clubb1);
    buttonB.setTag(R.raw.clubb2);
    buttonC.setTag(R.raw.clubb3);
    buttonD.setTag(R.raw.clubb4);
    buttonE.setTag(R.raw.clubb5);
    buttonF.setTag(R.raw.clubb6);
    buttonG.setTag(R.raw.clubb7);
    buttonH.setTag(R.raw.clubb8);
    buttonI.setTag(R.raw.clubb9);
    buttonJ.setTag(R.raw.clubb10);

    buttonA.setOnClickListener(this);
    buttonB.setOnClickListener(this);
    buttonC.setOnClickListener(this);
    buttonD.setOnClickListener(this);
    buttonE.setOnClickListener(this);
    buttonF.setOnClickListener(this);
    buttonG.setOnClickListener(this);
    buttonH.setOnClickListener(this);
    buttonI.setOnClickListener(this);
    buttonJ.setOnClickListener(this);

    Clubb1.setOnLoadCompleteListener(this);

    return rootView;
}

@Override
public void onClick(View v) {

    if(currentBtnId != null){
        Clubb1.release();
    }

    currentBtnId = v.getId();
    int sampleId = (int) v.getTag();
    Clubb1.load(getActivity(), sampleId, 1);
}

@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
    switch (currentBtnId){
        case R.id.buttonA:
            soundPool.play(sampleId, 1, 1, 1, 0, 1);
            break;
        case R.id.buttonB:
            soundPool.play(sampleId, 1, 1, 1, 0, 1);
            break;
        case R.id.buttonC:
            soundPool.play(sampleId, 1, 1, 1, 0, 1);
            break;
        case R.id.buttonD:
            soundPool.play(sampleId, 1, 1, 1, 0, 1);
            break;
        case R.id.buttonE:
            soundPool.play(sampleId, 1, 1, 1, 0, 1);
            break;
        case R.id.buttonF:
            soundPool.play(sampleId, 1, 1, 1, 0, 1);
            break;
        case R.id.buttonG:
            soundPool.play(sampleId, 1, 1, 1, 0, 1);
            break;
        case R.id.buttonH:
            soundPool.play(sampleId, 1, 1, 1, 0, 1);
            break;
        case R.id.buttonI:
            soundPool.play(sampleId, 1, 1, 1, 0, 1);
            break;
        case R.id.buttonJ:
            soundPool.play(sampleId, 1, 1, 1, 0, 1);
            break;

    }
}
}

1 个答案:

答案 0 :(得分:0)

标签是一个对象。从xml放置一个Integer或从java

设置标签
buttonA.setTag(1);
buttonB.setTag(2);
buttonC.setTag(3);
buttonD.setTag(4);
buttonE.setTag(5);
buttonF.setTag(6);
buttonG.setTag(7);
buttonH.setTag(8);
buttonI.setTag(9);
buttonJ.setTag(10);

int id =(Integer)v.getTag();

int sampleId = this.getResources()。getIdentifier(" clubb" + id," raw",this.getPackageName());

Clubb1.load(getActivity(),sampleId,1);