I am trying to activate a sound with an switch widget, so I can turn it off as well. Though I also want it to shut down automatically of after a few seconds. everything works though the app crashes on s.performClick();
anyone know how to fix the problem I have?
down here is my full code of the fragment.
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();
}
}