I am trying to write a small android app for my son. The idea is to make ringtone start to play on button click and it should be stoped by clicking on the other button. Something like you click a button to start a calling and another button to answer to it. What I manage to do is to make the first button to ring to work by the following code:
CallLukas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
});
However, the second button to stop the ringtone does not work as it suppose to. I use the following code:
Incoming.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mp.isPlaying()) {
mp.stop();
mp.release();
}
}
});
It stops the ringtone but in the same time makes the app to crash. What would be the right way to stop the previously activated ringtone? Thnak you...