Accessing RingtoneManager

From an Activity, I am trying to use the RingtoneManager and play a ringtone, and it works fine. However the app crashes when I try to stop the ringtone.

Here is the Activity code:

public class Time_Date extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time__date);

        Uri alarmUri = 
        RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

        if (alarmUri == null) {
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }

        final Ringtone ringtone = RingtoneManager.getRingtone(this, alarmUri);
        ringtone.play();

        Intent receive = getIntent();

        int vibrator = receive.getIntExtra("vibrator",1);

        if (vibrator==1) {
            Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(400);
        }
    }
}

And, now when I define an onclick and add this method to stop the ringtone, it crashes, What’s the best fix to this ?

public void dismissButton(View view) {
    ringtone.stop();
}


Source: android

Leave a Reply