如何在 android studio 中使用原生 OpenSL ES

How to use native OpenSL ES in android studio

我需要使用 Android Studio 在 NDK 中开发一个音频应用程序。我已经添加

the ndk path to local.properties -

    ndk.dir=/opt/android-ndk-r10
    sdk.dir=/opt/adt-bundle-linux-x86_64-20140702/sdk

In build.gradle I added an entry for OpenSLES -
apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.hellojni"
        minSdkVersion 8
        targetSdkVersion 21

        ndk {
            moduleName "HelloJNI"
            ldLibs "OpenSLES"       // Link with these libraries!
            stl "stlport_shared"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:20.0.0'
    compile 'com.android.support:appcompat-v7:20.0.0'
}

接下来我尝试为 opensl 添加 #includes -

#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>

但是 IDE 没有识别 headers 说它找不到包含文件。我还尝试将 native-audio 项目导入 android studio,但也没有编译或 运行。我知道官方仍然不支持将 NDK 与 Android Studio 一起使用。但是我看过一些视频,展示了如何将 ndk 与 android studio 集成。 有没有办法做到这一点。 提前致谢

OpenSL 库可用于具有 API 9+ 的 android 平台,因此您可能需要更改所需的最低 sdk。

不确定 NDK 如何选择要编译的平台,但您可能还需要使用自定义 Application.mk 文件自己编译,如下所示:

APP_ABI := armeabi
APP_PLATFORM := android-9
TARGET_PLATFORM := android-9

此外,您应该始终使用最高可用的 SDK 进行定位和编译。

以下是构建文件的示例:

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.hellojni"
        minSdkVersion 9
        targetSdkVersion 22

        ndk {
            moduleName "HelloJNI"
            ldLibs "OpenSLES"       // Link with these libraries!
            stl "stlport_shared"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
}

注意:通过上面的配置,为我找到了库(使用内置的ndk配置)。