Search code examples
androidringtonemanager

How to stop previous ringtone?


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...


Solution

  • The solution for my issue was making the MediaPlayer as a service. I have created a new Java class to extend the service class. Here is the code for it:

    public class RingtoneService extends Service {
    private MediaPlayer mp;
    
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mp = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
        mp.setLooping(true);
        mp.start();
        return START_STICKY;
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        mp.stop();
    }
    }
    

    Then I have updated my MainActivity class with the fallowing code:

    CallLukas.setOnClickListener(this);
    Incoming.setOnClickListener(this);
    
     @Override
    public void onClick(View v) {
        if (v == CallLukas){
            startService(new Intent(this, RingtoneService.class));
    
        }else if (v == Incoming) {
            stopService(new Intent(this, RingtoneService.class));
    
        }
    }
    

    The code basically sets the OnClickListener on my buttons and starts the service if one is clicked and stops the service if the other is clicked.

    The final steps are to add the newly created service to the AndroidManifest file. It needs to be added inside the <application> tag.

    <application        
        <service android:name=".RingtoneService"></service>        
    </application>
    

    The completed instruction on how to implement this can be found here: https://www.youtube.com/watch?v=p2ffzsCqrs8&t=315s