gpt4 book ai didi

c++ - Android OpenSL ES 在初始化时崩溃

转载 作者:行者123 更新时间:2023-11-30 05:27:36 25 4
gpt4 key购买 nike

所以我坚持将 Polycode 移植到 Android。
我正在尝试通过 OpenSL ES 实现音频接口(interface)。我阅读了一些文章,尝试尽可能多地从 android ndk 示例中复制等等,但对我来说没有任何效果 - 可靠地 - ..

我使用的是最新的 Android Studio (2.1.1),因此是最新的 NDK (12RC1)。对于测试,我使用我的 Fairphone 2(API 级别 21)和模拟器(Nexus 5 API 级别 23)。

应用程序通常在调用 CreateAudioPlayer 时崩溃,有时在实现播放器对象时崩溃,有时在 SetPlayState 时崩溃。但也恰好完整的初始化工作正常。这是独立于设备的。

在我的 Fairphone 上,我在实现播放器对象时收到这些警告:

org.polycode.templateapp D/AudioTrack: TrackOffload: AudioTrack Offload disabled by property, returning false
org.polycode.templateapp W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client

我的初始化代码如下所示:

SLresult lRes;

const SLInterfaceID lEngineMixIIDs[]={SL_IID_ENGINE};
const SLboolean lEngineMixReqs[]={SL_BOOLEAN_TRUE};
const SLuint32 lOutputMixIIDCount=0;
const SLInterfaceID lOutputMixIIDs[]={};
const SLboolean lOutputMixReqs[]={};

lRes = slCreateEngine(&mEngineObj, 0, NULL, 1, lEngineMixIIDs, lEngineMixReqs);
lRes = (*mEngineObj)->Realize(mEngineObj,SL_BOOLEAN_FALSE);
lRes = (*mEngineObj)->GetInterface(mEngineObj, SL_IID_ENGINE, &mEngine);

lRes=(*mEngine)->CreateOutputMix(mEngine, &mOutputMixObj,lOutputMixIIDCount,lOutputMixIIDs, lOutputMixReqs);
lRes=(*mOutputMixObj)->Realize(mOutputMixObj, SL_BOOLEAN_FALSE);

SLDataLocator_AndroidSimpleBufferQueue lDataLocatorIn;
lDataLocatorIn.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
lDataLocatorIn.numBuffers = 1;

SLDataFormat_PCM lDataFormat;
lDataFormat.formatType = SL_DATAFORMAT_PCM;
lDataFormat.numChannels = 1; // Mono sound.
lDataFormat.samplesPerSec = SL_SAMPLINGRATE_44_1;
lDataFormat.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
lDataFormat.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
lDataFormat.channelMask = SL_SPEAKER_FRONT_CENTER;
lDataFormat.endianness = SL_BYTEORDER_LITTLEENDIAN;

SLDataSource lDataSource;
lDataSource.pLocator = &lDataLocatorIn;
lDataSource.pFormat = &lDataFormat;

SLDataLocator_OutputMix lDataLocatorOut;
lDataLocatorOut.locatorType = SL_DATALOCATOR_OUTPUTMIX;
lDataLocatorOut.outputMix = mOutputMixObj;

SLDataSink lDataSink;
lDataSink.pLocator = &lDataLocatorOut;
lDataSink.pFormat = NULL;

const SLuint32 lSoundPlayerIIDCount = 2;
const SLInterfaceID lSoundPlayerIIDs[3] = { SL_IID_PLAY, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_VOLUME };
const SLboolean lSoundPlayerReqs[3] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };

lRes = (*mEngine)->CreateAudioPlayer(mEngine, &mPlayerObj, &lDataSource, &lDataSink, lSoundPlayerIIDCount, lSoundPlayerIIDs, lSoundPlayerReqs);
lRes = (*mPlayerObj)->Realize(mPlayerObj, SL_BOOLEAN_FALSE);

lRes = (*mPlayerObj)->GetInterface(mPlayerObj, SL_IID_PLAY, &mPlayer);
lRes = (*mPlayerObj)->GetInterface(mPlayerObj, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &mPlayerQueue);

lRes = (*mPlayerQueue)->RegisterCallback(mPlayerQueue, OpenSLAudioInterface::queueCallback, this);

lRes = (*mPlayer)->SetPlayState(mPlayer, SL_PLAYSTATE_PLAYING);

我想知道的是:在开始初始化 OpenSL 之前,是否需要等待初始化?
目前我正在从 onCreate 中启动的线程初始化 OpenSL。 onCreate 仍在等待完成。

或者我是否错过了任何 Android 配置或使用 OpenSL 的权限?还是我的代码有问题?

完整代码见:https://github.com/fodinabor/Polycode/tree/OpenSL/src/core/PolyOpenSLAudioInterface.cpp
https://github.com/fodinabor/Polycode/tree/OpenSL/include/polycode/core/PolyOpenSLAudioInterface.h

Android Studio 项目位于:https://github.com/fodinabor/Polycode/tree/OpenSL/build/android (TemplateApp 是我用于测试的)

任何的帮助甚至提示都是受欢迎的!

最佳答案

好的,所以我尝试在创建 native 窗口后初始化 OpenSL - 这似乎有效。所以我猜这是一个线程问题。

我不认为它需要在创建 native 窗口之后,但它必须在“ANativeActivity_onCreate”完成之后。

关于c++ - Android OpenSL ES 在初始化时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37195594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com