我正在开发一个简单的可视化工具应用程序,它可以监听音频输出,生成FFT(由可视化器API处理),做一些数学运算,并无线发送信息以供显示。我在使用Oneplus3上的Android 8.0上运行Visualizer API时遇到了麻烦,但它在模拟的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无法创建效果,状态:-1”,来自media / libmedia / AudioEffect.cpp,看起来像audioFlinger-&gt; 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 Visualizer error only in Android 6.0不同,其中应用程序不是要求运行时权限。
答案 0 :(得分:0)
通过开发此应用程序的一部分,我的手机更新到Android 8.0。 AFAICT,这打破了一些东西。最后,卸载并重新安装应用程序修复它。