尝试释放我的 soundPool 时出现错误
Posted
技术标签:
【中文标题】尝试释放我的 soundPool 时出现错误【英文标题】:Im getting an error when trying to release my soundPool 【发布时间】:2015-03-24 12:35:53 【问题描述】:我正在尝试释放我的声音池,但我不太清楚我在做什么。我是否正确释放了我的声音池对象?
我在这段代码中遇到错误,“”标记内的位: 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】:标签是一个对象。从 xml 中放置一个整数或从 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);
【讨论】:
如何将这段代码添加到我的项目中?我会放 int sampleId = (Integer)v.getTag();进入我的 xml? 你只想要原始资源 w.r.t 按钮点击右键? 我只需要我的 10 个按钮来播放与每个按钮对应的 10 个不同的声音片段,但我在 .java 代码的标记位上遇到了该错误。我需要在哪里将您编写的代码放入我的代码中?我不太擅长编程,所以请帮助我并解释要做什么以及把它们放在哪里。以上是关于尝试释放我的 soundPool 时出现错误的主要内容,如果未能解决你的问题,请参考以下文章
org.eclipse.swt.SWTException:尝试打开 AndroidManifest.xml 时出现“小部件已释放”