我在播放音频的 Android 上使用前台服务。我还在片段上使用来自Android Oboe库的NDK 的麦克风输入,与服务无关。但是,在我关闭应用程序后,即使服务被终止(服务通知消失),其他应用程序也无法访问麦克风。
这是我开始服务的方式:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//Start in Foreground
ContextCompat.startForegroundService(this, intent);
if (serviceConnection != null) {
//Then bind service
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
} else {
startService(intent);
if (serviceConnection != null) {
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
}
服务只是public class MainService extends Service
我添加了这个AndroidManifest
<service android:name=".services.MainService" android:stopWithTask="true"/>
所以当我关闭应用程序时它会停止。
但是,双簧管代码不在服务中。我怀疑 C++ 在后台保持一个线程打开。不知何故,这个线程即使在应用程序关闭后仍然存在。可能吗?
我添加了这个:
override fun onDestroy() {
Log.d(TAG, "-----fxfragment destroyed!")
NativeInterface.destroyAudioEngine()
super.onDestroy()
}
删除 C++ 端的音频引擎。它适用于某些手机,麦克风是可访问的。但在某些情况下,它没有。