android - 为什么 OpenSL ES 不会在每次启动应用程序时播放音频

标签 android c++ audio opensl

我正在使用 Android 上的 OpenSL ES 在我的游戏引擎中实现音频播放。到目前为止,我仅支持 Android 文件描述符数据定位器 (AndroidFD)。问题是,如果我使用 OpenSL ES 的包装器类,音频播放将不会始终有效。

包装器:

音频引擎.cpp

AudioEngine::AudioEngine()
    : _engine(NULL),
      _engineInterface(NULL),
      _outputMix(NULL)
{
    createEngine();
    createOutputMix();
}

AudioEngine::~AudioEngine()
{
    (*_outputMix)->Destroy(_outputMix);
    (*_engine)->Destroy(_engine);
}

Sound* AudioEngine::loadSound(const String& filename) const
{
    return NEW Sound(_engineInterface, _outputMix, filename);
}


// Private

void AudioEngine::createEngine()
{
    Uint32 result = slCreateEngine(&_engine, 0u, NULL, 0u, NULL, NULL);

    CHECK_OPENSLES_ERROR(result, "Platform::AudioEngine (OpenSLES)",
        "Failed to create the engine");

    result = (*_engine)->Realize(_engine, SL_BOOLEAN_FALSE);

    CHECK_OPENSLES_ERROR(result, "Platform::AudioEngine (OpenSLES)",
        "Failed to initialise the engine");

    result = (*_engine)->GetInterface(_engine, SL_IID_ENGINE, &_engineInterface);

    CHECK_OPENSLES_ERROR(result, "Platform::AudioEngine (OpenSLES)",
        "Failed to get the engine interface");
}

void AudioEngine::createOutputMix()
{
    Uint32 result = (*_engineInterface)->CreateOutputMix(_engineInterface, &_outputMix, 0u,
        NULL, NULL);

    CHECK_OPENSLES_ERROR(result, "Platform::AudioEngine (OpenSLES)",
        "Failed to create the output mix");

    result = (*_outputMix)->Realize(_outputMix, SL_BOOLEAN_FALSE);

    CHECK_OPENSLES_ERROR(result, "Platform::AudioEngine (OpenSLES)",
        "Failed to initialise the output mix");
}

声音.cpp

// Public

Sound::Sound(SLEngineItf engineInterface, SLObjectItf outputMix, const String& filename)
    : _engineInterface(engineInterface),
      _outputMix(outputMix),
      _player(NULL),
      _playerInterface(NULL)
{
    // Member (SLDataFormat_MIME)
    _dataSourceFormat.formatType = SL_DATAFORMAT_MIME;
    _dataSourceFormat.mimeType = NULL;
    _dataSourceFormat.containerType = SL_CONTAINERTYPE_UNSPECIFIED;

    // Member (SLDataLocator_AndroidFD)
    _dataSourceLocator.locatorType = SL_DATALOCATOR_ANDROIDFD;

    AAssetManager* assetManager = Application::assetManager();
    AAsset* asset = AAssetManager_open(assetManager, filename.data(), AASSET_MODE_UNKNOWN);

    _dataSourceLocator.fd = AAsset_openFileDescriptor(asset,
        reinterpret_cast<off_t*>(&_dataSourceLocator.offset),
        reinterpret_cast<off_t*>(&_dataSourceLocator.length));

    AAsset_close(asset);
    createPlayer();
}

Sound::~Sound()
{
    (*_player)->Destroy(_player);
}

void Sound::play() const
{
    const Uint32 result = (*_playerInterface)->SetPlayState(_playerInterface,
        SL_PLAYSTATE_PLAYING);

    CHECK_OPENSLES_ERROR(result, "Platform::Sound (OpenSLES)",
        "Failed to set the play state"); 
}


// Private

void Sound::createPlayer()
{
    SLDataSource dataSource =
    {
        &_dataSourceLocator,
        &_dataSourceFormat
    };

    SLDataLocator_OutputMix outputMixLocator =
    {
        SL_DATALOCATOR_OUTPUTMIX,
        _outputMix
    };

    SLDataSink dataSink =
    {
        &outputMixLocator,
        NULL
    };

    Uint32 result = (*_engineInterface)->CreateAudioPlayer(_engineInterface, &_player,
        &dataSource, &dataSink, 0u, NULL, NULL);

    CHECK_OPENSLES_ERROR(result, "Platform::Sound (OpenSLES)",
        "Failed to create a player");

    result = (*_player)->Realize(_player, SL_BOOLEAN_FALSE);

    CHECK_OPENSLES_ERROR(result, "Platform::Sound (OpenSLES)",
        "Failed to initialise the player");

    result = (*_player)->GetInterface(_player, SL_IID_PLAY, &_playerInterface);

    CHECK_OPENSLES_ERROR(result, "Platform::SoundInstance (OpenSLES)",
        "Failed to get the player interface");
}

游戏.cpp

Game::initialise()
{
    _audioEngine = NEW AudioEngine();
    _sound = _audioEngine->loadSound("music.ogg");
    _sound->play();
}

当应用程序启动时,声音不会一直播放。如果我在 Game::initialise() 中编写上面的所有 OpenSL ES 代码,声音将始终播放。我不明白为什么相同的代码在包装类中不起作用。

我在 Samsung Galaxy S III (GT-I9305)、Android 4.2.2、CyanogenMod 10.1.3-i9305 上运行该应用程序

最佳答案

在播放声音时,您可能必须将 dataSource、outputMixLocator 和 dataSink 保持在范围内,因为它们是作为指针传递的。使它们成为成员变量,使它们在对象的生命周期内保持 Activity 状态。

关于android - 为什么 OpenSL ES 不会在每次启动应用程序时播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21516126/

相关文章:

android admob 未显示 - 屏幕尺寸太小

java - 用户总是需要按两次谷歌登录按钮

android - Android应用程式在开启时当机

python - 如何将一串十六进制值转换为整数列表?

c# - 如何使用C#编写程序以基于数据库中存储的指定音频逐词发音?

android - 为什么不调用此Android异步任务中的doInBackground

c++ - 让析构函数根据是否发生异常采取不同的操作

c++ - 使用右值引用和简单工厂

c++ - 当内存对齐问题 (AVX) 不在最小上下文中重现时,如何调试它们?

linux - 如何使用 ALSA 将音频时钟从属到系统时钟?