如何停止当前播放的铃声? [英] How do I stop the currently playing ringtone?

查看:292
本文介绍了如何停止当前播放的铃声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用程序,有时触发提醒,都应该发挥的铃声。我可以开始铃声行,但不能阻止它。 我有一个方法辞退我的片段,该片段应停止当前播放的铃声,并关闭了活动,但它不工作的权利。 下面是它做什么:

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?

推荐答案

我发现这是唯一可能使用停止铃声 RingtoneManager.stop previousRingtone()仅在铃声按相同 RingtoneManager 实例创建。一个简单的方法来确认这是创建具有活性的 RingtoneManager 键,三个按键:

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. 点击第一个按钮将使用 RingtoneManager 来启动铃声。
  2. 单击第二个按钮将调用停止previousRingtone() RingtoneManager
  3. 点击第三个按钮将调用停止previousRingtone()在完全不同的 RingtoneManager 实例。
  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.

如果您有访问原始的铃声例如,你可以调用停止()方法:

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();
    }
}

然后,您可以<一href="http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29">start铃声在后台刚通过启动服务:

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

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

和,停止铃声,只是<一href="http://developer.android.com/reference/android/content/Context.html#stopService%28android.content.Intent%29">stop服务:

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

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

这篇关于如何停止当前播放的铃声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆