How do I stop the currently playing ringtone?

别来无恙 提交于 2019-12-20 10:25:17

问题


I'm writing an app that sometime fires reminders that are supposed to play ringtones. I can start the ringtone OK, but can't stop it. I have a dismiss method in my fragment that should stop the currently playing ringtone and close the activity, but it doesn't work right. Here's what it does:

    RingtoneManager ringMan = new RingtoneManager(getActivity());
    ringMan.stopPreviousRingtone();
    getActivity().finish();

The method does get executed and the activity closes, but the ringtone just keeps on playing. What am I doing wrong?


回答1:


Reason why stopPreviousRingtone() is not stopping the ringtone

I found that it is only possible to stop a ringtone using RingtoneManager.stopPreviousRingtone() only if the Ringtone was created by the same RingtoneManager instance. An easy way to confirm this is to create an activity with a RingtoneManager and three buttons:

  1. Clicking the first button will use the RingtoneManager to start a ringtone.
  2. Clicking the second button will call stopPreviousRingtone() on the RingtoneManager.
  3. Clicking the third button will call stopPreviousRingtone() on the completely different RingtoneManager instance.

After doing this, you should find that the ringtone will only stop if you click the second button.

Stopping a Ringtone if you have access to the original Ringtone instance

If you have access to the original Ringtone instance, you can just call the stop() method:

Ringtone ringtone = RingtoneManager.getRingtone(getActivity());
ringtone.play();
...
ringtone.stop();

Stopping Ringtones when keeping a reference to the original Ringtone is not possible

What you could try is to create a service which will load and play the ringtone when it was started and will stop the ringtone when it was destroyed. Here is an example:

public class RingtonePlayingService extends Service
{
    private Ringtone ringtone;

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Uri ringtoneUri = Uri.parse(intent.getExtras().getString("ringtone-uri"));

        this.ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
        ringtone.play();

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy()
    {
        ringtone.stop();
    }
}

Then, you can start the ringtone in the background just by starting the service:

Intent startIntent = new Intent(context, RingtonePlayingService.class);
startIntent.putExtra("ringtone-uri", ringtoneUri);
context.startService(startIntent);

And, to stop the ringtone, just stop the service:

Intent stopIntent = new Intent(context, RingtonePlayingService.class);
context.stopService(stopIntent);



回答2:


You have to create a class extending from Service class, the demo code is given below

Please Note this is the code form my College Project, it is for starting up default Alarm using Ringtone from BroadcastReciever extending Activity and Stopping the Ringtone from different normal Android Activity.

Service Class code:

import android.app.Service;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.IBinder;
public class AlarmRing extends Service {
static Ringtone r;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    //activating alarm sound
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    r = RingtoneManager.getRingtone(getBaseContext(), notification);
    //playing sound alarm
    r.play();

    return START_NOT_STICKY;
}
@Override
public void onDestroy()
{
    r.stop();
}
}

Code for Activity calling the Service:

Intent i = new Intent(context, AlarmRing.class);
context.startService(i);

Activity ending or closing the Service:

Intent i = new Intent(this, AlarmRing.class);
stopService(i);`

NOTE: please note for context reference.




回答3:


Create RingtoneManager object using Application Context like this

RingtoneManager ringMan = new RingtoneManager(getApplicationContext());
    ringMan.stopPreviousRingtone();

and RingTone object also create like this only.

mRingTome = RingtoneManager.getRingtone(getApplicationContext(), notification);
            mRingTome.play();

It is working for me.



来源:https://stackoverflow.com/questions/14089380/how-do-i-stop-the-currently-playing-ringtone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!