Android 的 AudioManager.getMode() 似乎不可靠。同样的情况产生不同的结果

发布于 2025-01-05 00:52:58 字数 1314 浏览 0 评论 0原文

我目前正在开发一个音频播放器,它应该在失去音频焦点时暂停/停止,例如当有电话进来时。

虽然我通过使用 OnAudioFocusChangeListener 使此功能正常工作,但我想增强此功能以在不同情况下表现不同。

由于应用程序实现了超时功能,以防用户在收听时睡着,我只想在确定用户仍然清醒时恢复播放。

我想我几乎可以忽略诸如传入电子邮件之类的通知声音,因为这些通常只是简短的剪辑,并且我的音频继续播放不会像在打电话期间那样对用户造成滋扰。 因此,我想将暂停/停止限制在有电话打入或用户拨打电话的情况。

我已经看到 AudioManager 有一些方便的状态来发出信号,但是在 OnAudioFocusChangeListener 中调用 AudioManager.getMode() 时,我没有得到一致的输出。 有时,来电确实会生成 MODE_RINGTONE,但通常足以使该功能变得不可靠,我会得到 MODE_NORMAL。

实际上,只要我在重新获得焦点时获得正确的模式,我就可以在失去焦点时不知道模式,但这似乎并不可靠。

我唯一可以测试此行为的实际设备是 Galaxy S2,运行 Android 2.3.6(由于我使用 MediaMetadataRetriever,该应用程序的 API 级别为 10,因此至少需要 2.3.3)。 我已经在各种模拟系统中测试了音频焦点的丢失和重新获得,总的来说,它似乎在那里工作得更好,但我每次也没有真正得到相同的结果。

也许问题是我误解了 AudioManager.getMode() 应该返回的内容,也许有人可以为我澄清这一点:

  1. 电话铃声 -> AudioManager.getMode() 在用户拨打或接听电话时应返回 MODE_RINGTONE
  2. -> AudioManager.getMode() 应该在焦点丢失时返回 MODE_IN_CALL
  3. 不接听电话 -> AudioManager.getMode() 应该在焦点增益用户挂断时返回 MODE_RINGTONE
  4. -> AudioManager.getMode() 应该在焦点增益上返回 MODE_IN_CALL

这是它应该工作的方式吗?至少我的测试表明这是应该的方式,但由于某种原因并非总是如此。

我尝试解决此问题的另一种方法是使用 PhoneStateListener,但我遇到的问题是,这需要我的应用程序请求读取手机状态的权限。由于我目前通过 SourceForge 共享我的应用程序,稍后可能通过 Android Market 共享,因此我想避免使用此类权限。

所以,最后我想问一下我理解 AudioManager.getMode() 的方式是否正确,是否有任何关于其可靠性的信息,对我来说,我从中得到的结果有点无用。

I am currently working on an audio player that is supposed to pause/stop when losing audio focus, for example when a call comes in.

While I got this working fine by using an OnAudioFocusChangeListener I want to enhance this function to behave differently under different circumstances.

As the application implements a timeout function in case the user has fallen asleep while listening I only want to resume playback when I can be positive that the user is still awake.

I figured I could pretty much ignore notification sounds like those of an incoming email, as these usually are only short clips and my audio continuing to play will not be a nuisance to the user, as it would be during a phone call.
Thus I want to limit pausing/stopping to situation where either a phone call comes in or the user makes a call.

I have seen that AudioManager has some convenient states to signal this, but upon calling AudioManager.getMode() in my OnAudioFocusChangeListener I do not get consistent output.
Sometimes an incoming call does generate MODE_RINGTONE, but often enough to render the function unreliable I get MODE_NORMAL.

I could actually live with not knowing the mode when losing focus as long as I get the correct mode when re-gaining focus, but also this doesn't seem to work reliably.

The only actual device I could test this behavior on is the Galaxy S2, running Android 2.3.6 (the application is API level 10 due to me using MediaMetadataRetriever, thus requires at least 2.3.3).
I have tested the loss and re-gaining of audio focus in various emulated systems, and overall it seems to be working better there, but I also don't really get the same results each time.

Maybe the problem is that I misunderstand what AudioManager.getMode() should return, maybe somebody could clarify this for me:

  1. phone rings -> AudioManager.getMode() should return MODE_RINGTONE on focus loss
  2. user makes or picks up a call -> AudioManager.getMode() should return MODE_IN_CALL on focus loss
  3. doesn't pick up the call -> AudioManager.getMode() should return MODE_RINGTONE on focus gain
  4. user hangs up -> AudioManager.getMode() should return MODE_IN_CALL on focus gain

Is that the way it is supposed to work? At least my tests suggest that this is the way it is supposed to be, but for some reason isn't all the time.

Another way I am trying to solve this problem is by using a PhoneStateListener, but the problem I have with this is that this requires my application to request the permission to read the phone state. As I am sharing my application, currently through SourceForge, later possibly through the Android Market, I would like to avoid using this kind of permissions.

So, coming to an end I would like to ask if the way I understand AudioManager.getMode() is correct and if there is any information on its reliability, as for me the results I get from it are kinda useless.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文