在Android上,我遇到了一个问题,试图找出实际播放的铃声(我不是试图检测默认铃声,但实际播放的铃声可能会因用户为特定联系人设置的特定铃声而有所不同)。
在循环(成功) RingtoneManager中所有可用的铃声时,我使用了Ringtone.isPlaying()函数。但是,它们都没有向Ringtone.isPlaying()返回true!有谁知道我做错了什么吗?以下是在播放环的同时运行的代码示例:
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
}发布于 2010-01-19 18:13:44
如果查看source of Ringtone,就会发现isPlaying()方法只关心Ringtone的那个特定实例。
当您从RingtoneManager()调用getRingtone()时,它会创建一个新的Ringtone对象(source)。因此,这将不是用于在有人调用时播放声音的同一个Ringtone对象(如果使用Ringtone对象来执行此操作),因此在您的示例中,isPlaying()将始终返回false。
只有当您在特定的isPlaying()对象上调用了play()时,Ringtone才会返回true。
因为每个应用程序都会创建自己的MediaPlayer对象,所以我认为您无法监控其他应用程序当前正在播放的声音。
https://stackoverflow.com/questions/2092470
复制相似问题