Android 检测实际正在播放的铃声(Ringtone.isPlaying 问题)

发布于 2024-08-18 04:32:15 字数 859 浏览 10 评论 0原文

在 Android 上,我在尝试找出实际正在播放的铃声时遇到问题(我不是试图检测默认铃声,而是实际播放的铃声,因为由于用户为特定铃声设置了特定铃声,因此实际播放的铃声可能会有所不同)接触)。

当我循环浏览(成功)RingtoneManager 中的所有可用铃声时,我正在使用 Ringtone.isPlaying() 函数。然而,它们都没有返回 true 到 Ringtone.isPlaying()!有人知道我做错了什么吗?以下是在铃声播放时肯定正在运行的代码示例:

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
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有深☉意 2024-08-25 04:32:15

如果您查看 Ringtone 的来源,您可以看到 isPlaying() 方法只关心该特定的铃声的实例。

当您从 RingtoneManager() 调用 getRingtone() 时,它会创建一个新的 Ringtone 对象(源< /a>)。因此,这不会是在有人呼叫时用于播放声音的同一个 Ringtone 对象(如果使用 Ringtone 对象来执行此操作),因此 isPlaying() 将始终返回 false

仅当您在特定 Ringtone 对象上调用 play() 时,isPlaying() 才会返回 true

由于每个应用程序都会创建自己的 MediaPlayer 对象,因此我认为您无法监视其他应用程序当前正在播放哪些声音。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文