检索具有音频焦点更改的应用程序名称 [英] Retrieving the application name that has audio focus change

查看:88
本文介绍了检索具有音频焦点更改的应用程序名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到任何有关找出哪个应用程序获得音频焦点的信息.我可以从我的应用程序中正确确定焦点更改的类型,但不能从其他任何应用程序中确定.有什么方法可以确定哪些应用程序受到关注吗?

我想做什么?"

我设法录制了内部声音,无论是音乐还是声音.如果我当前正在录制音频而不管其来源是什么,那么我想确定重点放在哪个应用程序上,以确定下一步我的应用程序需要做什么.

当前,我正在使用 AudioManager.OnAudioFocusChangeListener ,以便我的应用程序在焦点改变后停止录制内部声音,但是我希望获得焦点的应用程序名称.

解决方案

简短回答:没有很好的解决方案……而Android可能就是这样.

说明:查看源代码,AudioManager没有用于检查谁拥有Audio Focus的API(甚至是隐藏的API).AudioManager将对AudioService的调用进行包装,以保留真实的音频状态.当AudioManager绑定到AudioService时,AudioService通过其存根公开的API也没有用于查询当前Audio Focus的API.因此,即使通过反射/系统级权限,您也将无法获得所需的信息.

如果您想知道如何跟踪焦点更改,可以查看 UsageStats 时间戳记.然后,一旦您有一些应用在丢失AudioFocus的大约500毫秒内被使用,您可以将其与具有音频许可"的应用进行交叉检查.您可以按照这篇文章获取任何已安装应用的权限.

这显然是一种启发式方法,可能需要进行一些调整.它还要求用户授予您的应用权限,以访问使用情况统计信息.里程可能会有所不同.

I can't seem to find anything related to finding out what application got audio focus. I can correctly determine from my application what type of focus change it was, but not from any other application. Is there any way to determine what application received focus?

"What am I wanting to do?"

I have managed to record internal sound whether it be music or voice. If I am currently recording audio no matter the source, I want to determine what application took the focus over to determine what my application need's to do next.

Currently I am using the AudioManager.OnAudioFocusChangeListener for my application to stop recording internal sounds once the focus changes, but I want the application's name that gained the focus.

解决方案

Short Answer: There's no good solution... and Android probably intended it this way.

Explanation: Looking at the source code, AudioManager has no API's(even hidden APIs) for checking who has Audio Focus. AudioManager wraps calls to AudioService which holds onto the real audio state. The API that AudioService exposes through it's Stub when AudioManager binds to it also does not have an API for querying current Audio Focus. Thus, even through reflection / system level permissions you won't be able get the information you want.

If you're curious how the focus changes are kept track of, you can look at MediaFocusControl whose instance is a member variable of AudioService here.

Untested Hacky Heuristic: You might be able to get some useful information by looking at UsageStats timestamps. Then once you have apps that were used within say ~500ms of you losing AudioFocus you can cross-check them against apps with Audio Permissions. You can follow this post to get permissions for any installed app.

This is clearly a heuristic and could require some tuning. It also requires the user to grant your app permissions to get access to the usage stats. Mileage may vary.

这篇关于检索具有音频焦点更改的应用程序名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆