问题描述
在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(本)); // *应*回报播放铃声称号
}
返回失败了!; //总是在这里结束了
}
如果你看一下<一href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=media/java/android/media/Ringtone.java;h=171332431119d55aeed2e118a0640eb6b191732d;hb=HEAD"相对=nofollow> 铃声的来源
你可以看到 IsPlaying模块()
方法只在乎关于该特定实例铃声
。
当你调用 getRingtone()
从 RingtoneManager()
创建一个新的铃声
对象(<一href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=media/java/android/media/RingtoneManager.java;h=8481410cc8f3ca69f32154a14cbd66bd1d5cd1dd;hb=HEAD"相对=nofollow>来源)。因此,这将不使用的时候有人呼叫(如铃声
对象用来播放声音同样铃声
对象做到这一点),所以 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.
这篇关于Android的检测哪个铃声确实正在播放(Ringtone.isPlaying问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!