您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
2. 创建OpenSLES音频播放测试项目并完成引擎初始化~1
发布时间:2021-06-04 15:07:27编辑:雪饮阅读()
上篇中實現了安卓视频播放窗口去掉标题栏、全屏、横屏等的處理並在雷電模擬器4中進行了測試。但是目前來説視頻還有最致命的問題就是沒有聲音,雖然聲音也解碼了。
#include <string>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include <android/log.h>
#define LOGD(FORMAT,...) __android_log_print(ANDROID_LOG_ERROR,"ywl5320",FORMAT,##__VA_ARGS__);
/*
* SLObjectItf:OpenSL ES中最重要的接口類
* */
static SLObjectItf engineSL = NULL;
/*
* SLEngineItf:OpenSL ES中具体的引擎(接口類)
* */
SLEngineItf CreateSL()
{
/*SLresult:返回結果
* 下面三個方法:
* slCreateEngine、Realize、GetInterface
* 返回結果都是SLresult類型,所以這裏統一定義一個返回結果類型
* */
SLresult re;
SLEngineItf en;
/*
* slCreateEngine方法用於创建一个引擎接口对象,這裏一般只關注第一個參數,其餘5個參數一般設置為0即可
* */
re = slCreateEngine(&engineSL,0,0,0,0,0);
if(re != SL_RESULT_SUCCESS) return NULL;
//创建好引擎接口对象后,需要用SLObjectItf的Realize方法来实现engineObject,Realize方法的第二個參數async表示是否異步,這裏使用同步
re = (*engineSL)->Realize(engineSL,SL_BOOLEAN_FALSE);
if(re != SL_RESULT_SUCCESS) return NULL;
//GetInterface方法来初始化SLEngnineItf对象实例,GetInterface的第二個參數是一個SL接口id,這裏一般用常量SL_IID_ENGINE
re = (*engineSL)->GetInterface(engineSL,SL_IID_ENGINE,&en);
if(re != SL_RESULT_SUCCESS) return NULL;
return en;
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_audio_1opensles_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
//1 创建引擎
SLEngineItf eng = CreateSL();
if(eng)
{
LOGD("CreateSL success! ");
}
else
{
LOGD("CreateSL failed! ");
}
return env->NewStringUTF(hello.c_str());
}
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("audio_opensles")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
OpenSLES
# Links the target library to the log library
# included in the NDK.
${log-lib} )
編譯並運行到雷電4上出現這一幕就算是引擎創建成功了。
关键字词:OpenSLES
相关文章
-
无相关信息