我正在尝试使用开关小部件激活声音,因此我也可以将其关闭。虽然我也希望它在几秒钟后自动关闭。s.performClick();
尽管应用程序在任何人知道如何解决我遇到的问题时崩溃,但一切正常 ?
下面是我的完整代码片段。
public static class Therapy extends Fragment implements CompoundButton.OnCheckedChangeListener{
MediaPlayer player = null;
Switch s;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.therapy, container, false);
Switch s = (Switch)v.findViewById(R.id.switch1);
s.setOnCheckedChangeListener(this);
return v;
}
class MyTimerTask extends TimerTask {
public void run() {
s.performClick();
}
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
player = MediaPlayer.create(getActivity(), R.raw.bird1);
player.setLooping(true);
player.start();
MyTimerTask myTask = new MyTimerTask();
Timer myTimer = new Timer();
myTimer.schedule(myTask, 1000);
} else {
player.stop();
player.release();
}
}