Android的检测哪个铃声确实正在播放(Ringtone.isPlaying问题)正在播放、确实、铃声、问题

2023-09-06 17:45:53 作者:送礼就送敌敌畏

在Android上,我有一个问题,试图找出其中的铃声实际上是打(我不是要检测的默认铃声,但一个真正打,因为它可能是不同的,由于用户设置一个特定的铃声对于特定的接触)。

我使用的是Ringtone.isPlaying()函数,我循环(成功)的所有可从RingtoneManager铃声。然而他们没有以往Ringtone.isPlaying返回true()!任何人有什么线索,我做错了什么?这里是绝对正运行,而环中的code样品正在播放:

  RingtoneManager RM =新RingtoneManager(本); //'这'是我的活动(实际上是我的情况下,服务)
如果(RM!= NULL)
{
    光标光标= rm.getCursor();
    cursor.moveToFirst();
     的for(int i = 0; i ++在)
     {
            铃声铃声= rm.getRingtone(ⅰ); //获取铃声在光标这个位置
            如果(铃声== NULL)
            打破;
        否则,如果(ringtone.isPlaying()==真)
                返回(ringtone.getTitle(本)); // *应*回报播放铃声称号
    }
    返回失败了!; //总是在这里结束了
}
 

解决方案

如果你看一下 铃声的来源 你可以看到 IsPlaying模块()方法只在乎关于该特定实例铃声

Android手机怎样自定义铃声

当你调用 getRingtone() RingtoneManager()创建一个新的铃声对象(来源)。因此,这将不使用的时候有人呼叫(如铃声对象用来播放声音同样铃声对象做到这一点),所以 IsPlaying模块()总是返回你的情况。

IsPlaying模块()将只返回如果你调用播放() 具体的铃声对象。

由于每个应用程序创建自己的的MediaPlayer 对象,我不认为你可以监视这听起来其他应用程序正在播放。

On Android, I'm having a problem trying to figure out which ringtone is actually playing (I'm not trying to detect the default ringtone, but the one actually playing as it may be different due to user setting a particular ringtone for a particular contact).

I'm using the Ringtone.isPlaying() function as I cycle through (successfully) all the available Ringtones from the RingtoneManager. However none of them ever returns true to Ringtone.isPlaying()! Anyone have a clue what I am doing wrong? Here is the code sample that is definitely being run whilst the ring is playing:

RingtoneManager rm = new RingtoneManager(this); // 'this' is my activity (actually a Service in my case)
if (rm != null)
{
    Cursor cursor = rm.getCursor();
    cursor.moveToFirst();
     for (int i = 0; ; i++)
     {
            Ringtone ringtone = rm.getRingtone(i);  // get the ring tone at this position in the Cursor
            if (ringtone == null)
            break;
        else if (ringtone.isPlaying() == true)
                return (ringtone.getTitle(this));   // *should* return title of the playing ringtone
    }
    return "FAILED AGAIN!"; // always ends up here
}

解决方案

If you look at the source of Ringtone you can see that the isPlaying() method only cares about that particular instance of Ringtone.

When you call getRingtone() from RingtoneManager() it creates a new Ringtone object (source). So this will not be the same Ringtone object used to play a sound when someone calls (if Ringtone objects are used to do that) so isPlaying() will always return false in your case.

isPlaying() will only ever return true if you have called play() on that specific Ringtone object.

As each application creates its own MediaPlayer objects I don't think you can monitor which sounds other applications are currently playing.