我在 SO 上看到了多个问题,这些问题说不可能看到另一个应用程序何时想要使用麦克风(当其他应用程序在 Android 中使用麦克风时,无法访问麦克风 https://stackoverflow.com/questions/24170124/unable-to-access-microphone-when-another-app-is-using-it-in-android)。但是,我知道这是可能的,因为 Shazam 具有“自动”功能,可以实现我想要的确切功能:在其他应用程序不使用麦克风时连续收听音频。
目前,当我使用我的应用程序并使用 Snapchat 等应用程序时,我无法录制带有音频的视频,因为 Snapchat 不会接管麦克风。然而,正如我之前所说,在这种情况下,Shazam 的自动功能运行良好。
那么当其他应用程序想要使用麦克风时我该如何监听呢?我可以使用“黑客”的东西,只要它不需要root手机或类似的东西。
编辑:
Shazam 从未有过此功能,他们的应用程序在运行时无法将麦克风让给其他应用程序。
这是目前我拥有的最好的解决方案。无法监听其他请求麦克风的应用程序,因此我创建了一个UsageStats
每 3 秒运行一次的检查程序,以查看当前打开的应用程序是否能够请求音频。如果您有更好的东西或对此进行改进,请告诉我。
注意:您必须将权限添加到应用程序清单中:
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
android.os.Process.myUid(), getPackageName());
if (mode != AppOpsManager.MODE_ALLOWED) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
}
if (otherAppAudioTimer != null) {
otherAppAudioTimer.cancel();
}
otherAppAudioTimer = new Timer();
otherAppAudioTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
UsageStatsManager usm = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
long now = System.currentTimeMillis();
final List<UsageStats> queryUsageStats = usm.queryUsageStats(UsageStatsManager.INTERVAL_YEARLY, now - (1000 * 60 * 60), now);
if (queryUsageStats != null && queryUsageStats.size() > 0) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>();
for (UsageStats usageStats : queryUsageStats) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (!mySortedMap.isEmpty()) {
String currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
boolean hasRecordAudio = getPackageManager()
.checkPermission(Manifest.permission.RECORD_AUDIO, currentApp)
== PackageManager.PERMISSION_GRANTED;
if (getApplicationContext().getPackageName().equals(currentApp)) {
Log.e("hasAudio", "Current app is self");
return;
}
if (hasRecordAudio) {
//the current app can record audio
} else {
//the current app cannot record audio
}
}
}
}
}, 0, 3000);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)