我正在开发一个简单的可视化应用程序,它监听音频输出,生成 FFT(由可视化 API 处理),做一些数学运算,然后无线发送信息以供显示。我无法让 Visualizer API 在 Oneplus3 上的 Android 8.0 上运行,但它在模拟的 Android 6.0 和 8.0 手机上都可以正常工作。
以下是我如何请求我认为我需要的权限,首先在清单中:
<manifest>
<application>
...rest of application...
</application>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
</manifest>
接下来我在运行时请求一些权限:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
if (ContextCompat.checkSelfPermission(getApplicationContext(), permission.MODIFY_AUDIO_SETTINGS) == PackageManager.PERMISSION_DENIED)
Log.d("App", "No MODIFY_AUDIO_SETTINGS" );
else
Log.d("App", "Yes MODIFY_AUDIO_SETTINGS" );
if (ContextCompat.checkSelfPermission(getApplicationContext(), permission.RECORD_AUDIO) == PackageManager.PERMISSION_DENIED)
Log.d("App", "No RECORD_AUDIO" );
else
Log.d("App", "Yes RECORD_AUDIO" );
Log.d("App","Requesting permissions" );
ActivityCompat.requestPermissions( this, new String[]
{
permission.MODIFY_AUDIO_SETTINGS,
permission.RECORD_AUDIO
},1 );
Log.d("App","Requested perms");
}
我相信这应该打印当前的权限状态,然后请求我需要的所有权限。在全新安装时,它会提示输入 RECORD_AUDIO( MODIFY_AUDIO_SETTINGS 不是危险权限,并且似乎是自动授予的)。异步地,这被称为:
public void onRequestPermissionsResult (int requestCode,
String[] permissions,
int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean success = true;
for( int i = 0; i < permissions.length; ++i ) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED ) {
Log.d("App",permissions[i]+" granted");
} else {
Log.d("App",permissions[i]+" denied");
Toast.makeText(getApplicationContext(), "Needed " + permissions[i], Toast.LENGTH_SHORT).show();
success = false;
}
}
if( success ) {
Log.d("App","Setting up visualizer");
}
}
在 Android 8.0 上,日志显示我们拥有这两个权限,并且如果我们仍然请求它们,它们就会被授予。但是当我们稍晚一点执行时(启动可视化器当前与按下按钮相关联),
D/App: Yes MODIFY_AUDIO_SETTINGS
D/App: Yes RECORD_AUDIO
D/App: Requesting permissions
D/App: Requested perms
D/AppTracker: App Event: start
D/OpenGLRenderer: HWUI GL Pipeline
D/AppTracker: App Event: stop
I/Adreno: QUALCOMM build : 08cdca0, I5f270ba9bc
Build Date : 09/18/17
OpenGL ES Shader Compiler Version: EV031.20.00.04
Local Branch :
Remote Branch : refs/tags/AU_LINUX_ANDROID_LA.UM.6.5.R1.08.00.00.312.025
Remote Branch : NONE
Reconstruct Branch : NOTHING
I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8996.so from the current namespace instead.
I/Adreno: PFP: 0x005ff087, ME: 0x005ff063
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 2
D/App: android.permission.MODIFY_AUDIO_SETTINGS granted
D/App: android.permission.RECORD_AUDIO granted
D/App: Setting up visualizer
D/AppTracker: App Event: start
I/vndksupport: sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8996.so from the current namespace instead.
E/AudioEffect: set(): AudioFlinger could not create effect, status: -1
E/visualizers-JNI: Visualizer initCheck failed -3
E/Visualizer-JAVA: Error code -3 when initializing Visualizer.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.rsaxvc.blequalizer, PID: 15981
java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
at android.media.audiofx.Visualizer.<init>(Visualizer.java:218)
at net.rsaxvc.blequalizer.MainActivity.setupVisualizerFxAndUI(MainActivity.java:133)
at net.rsaxvc.blequalizer.MainActivity.access$000(MainActivity.java:22)
at net.rsaxvc.blequalizer.MainActivity$1.onNavigationItemSelected(MainActivity.java:63)
at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:182)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
at android.support.design.internal.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:95)
at android.view.View.performClick(View.java:6289)
at android.view.View$PerformClick.run(View.java:24800)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6809)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
D/AppTracker: App Event: crash
Application terminated.
打电话时
mVisualizer = new Visualizer(0);
我们看到:“E/AudioEffect: set(): AudioFlinger could not create effect, status: -1”,从media/libmedia/AudioEffect.cpp,看起来audioFlinger->createEffect失败,返回-1,等于PERMISSION_DENIED,由于 services/audioflinger/AudioFlinger.cpp 中的以下代码:
// check audio settings permission for global effects
if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
lStatus = PERMISSION_DENIED;
goto Exit;
}
这在 libs/audioflinger/AudioFlinger.cpp 中:
static bool settingsAllowed() {
#ifndef HAVE_ANDROID_OS
return true;
#endif
#if AUDIOFLINGER_SECURITY_ENABLED
if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS"));
if (!ok) LOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS");
return ok;
#else
if (!checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS")))
LOGW("WARNING: Need to add android.permission.MODIFY_AUDIO_SETTINGS to manifest");
return true;
#endif
}
我的猜测是本机代码没有获得与 Java 代码相同的权限。或者,本机代码作为与应用程序本身不同的进程运行。
这似乎与仅在 Android 6.0中的 Android Visualizer 错误不同,其中应用程序只是不要求运行时权限。