1

我在播放音频的 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++ 端的音频引擎。它适用于某些手机,麦克风是可访问的。但在某些情况下,它没有。

4

2 回答 2

1

在 onResume 中创建引擎并在 onPause() 中销毁,因此流仅在焦点时保持独占模式。这允许其他应用程序回收独占流模式。因此,我建议您添加 onResume() 和 onPause()

@Override
protected void onResume() {
    super.onResume();
    PlaybackEngine.create(this);
    setupLatencyUpdater();
    // Return the spinner states to their default value
    mChannelCountSpinner.setSelection(CHANNEL_COUNT_DEFAULT_OPTION_INDEX);
    mPlaybackDeviceSpinner.setSelection(SPINNER_DEFAULT_OPTION_INDEX);
    mBufferSizeSpinner.setSelection(SPINNER_DEFAULT_OPTION_INDEX);
    mAudioApiSpinner.setSelection(SPINNER_DEFAULT_OPTION_INDEX);
}

并在 onPause()

@Override
protected void onPause() {
   if (mLatencyUpdater != null) mLatencyUpdater.cancel();
   PlaybackEngine.delete();
   super.onPause();
}

您也可以通过此github 链接了解更多详细信息

于 2020-06-29T15:28:49.617 回答
0

这是库中的错误,此处提到库问题链接

于 2020-06-23T04:57:01.977 回答