Android 检测实际正在播放的铃声(Ringtone.isPlaying 问题)
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看
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 theisPlaying()
method only cares about that particular instance ofRingtone
.When you call
getRingtone()
fromRingtoneManager()
it creates a newRingtone
object (source). So this will not be the sameRingtone
object used to play a sound when someone calls (ifRingtone
objects are used to do that) soisPlaying()
will always returnfalse
in your case.isPlaying()
will only ever returntrue
if you have calledplay()
on that specificRingtone
object.As each application creates its own
MediaPlayer
objects I don't think you can monitor which sounds other applications are currently playing.