您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
8. 完成了IAudioPlay和SLAudioPlay的音频播放环境初始化~1
发布时间:2021-06-11 21:37:06编辑:雪饮阅读()
利用IAudioPlay和SLAudioPlay完成的音频播放环境初始化需要先在cpp/CMakeLists.txt中引入這兩個家夥:
# 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("xplay")
# 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.
#添加頭文件路徑(括號中的include是相對於本文件路徑)
include_directories(../../../include)
#設置ffmpeg庫所在路徑的變量,這裏的FF是自定義的一個名字
set(FF ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI})
#avcodec這個是自定義的一個名字
add_library(avcodec SHARED IMPORTED)
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${FF}/libavcodec.so)
add_library(avformat SHARED IMPORTED)
set_target_properties(avformat PROPERTIES IMPORTED_LOCATION ${FF}/libavformat.so)
add_library(avutil SHARED IMPORTED)
set_target_properties(avutil PROPERTIES IMPORTED_LOCATION ${FF}/libavutil.so)
add_library(swscale SHARED IMPORTED)
set_target_properties(swscale PROPERTIES IMPORTED_LOCATION ${FF}/libswscale.so)
add_library(swresample SHARED IMPORTED)
set_target_properties(swresample PROPERTIES IMPORTED_LOCATION ${FF}/libswresample.so)
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).
#src/main/cpp/native-lib.cpp
IDemux
FFDemux
XData
XLog
native-lib.cpp
XThread
IObserver
FFDecode
IDecode
XParameter
IVideoView
GLVideoView
XTexture
XEGL
XShader
FFResample
IResample
SLAudioPlay
IAudioPlay
)
# 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
GLESv2 EGL
OpenSLES
android
avcodec avformat avutil swscale swresample
# Links the target library to the log library
# included in the NDK.
${log-lib} )
那麽在cpp/FFResample.cpp中音頻重采樣需要再次優化下:
然後對於入口cpp/native-lib.cpp中就可以調用這個IAudioPlay和SLAudioPlay的音频播放環境了。:
不,別急這裏還沒有在cpp/IAudioPlay.cpp中進行相關實現:
雖然是個空實現,為後面做鋪墊唄。 最後關於混音器和緩衝隊列這些,咱們可以參考之前面嚮過程時期的那個代碼然後調整于cpp/SLAudioPlay.cpp:
果然還是老師的v1080.mp4可以播放,但是沒有聲音,在這個雷電4中
#define XPLAY_XPARAMETER_H
struct AVCodecParameters;
class XParameter
{
public:
AVCodecParameters *para = 0;
int channels = 2;
int sample_rate = 44100;
};
#endif //XPLAY_XPARAMETER_H
{
#include <libswresample/swresample.h>
}
#include "XLog.h"
#include <libavcodec/avcodec.h>
#include "FFResample.h"
bool FFResample::Open(XParameter in,XParameter out)
{
//音频重采样上下文初始化
actx = swr_alloc();
actx = swr_alloc_set_opts(actx,
av_get_default_channel_layout(out.channels),
AV_SAMPLE_FMT_S16,out.sample_rate,
av_get_default_channel_layout(in.para->channels),
(AVSampleFormat)in.para->format,in.para->sample_rate,
0,0 );
int re = swr_init(actx);
if(re != 0)
{
XLOGE("swr_init failed!");
return false;
}
else
{
XLOGI("swr_init success!");
}
outChannels = in.para->channels;
outFormat = AV_SAMPLE_FMT_S16;
return true;
}
XData FFResample::Resample(XData indata)
{
if(indata.size<=0 || !indata.data) return XData();
if(!actx)
return XData();
//XLOGE("indata size is %d",indata.size);
AVFrame *frame = (AVFrame *)indata.data;
//输出空间的分配
XData out;
int outsize = outChannels * frame->nb_samples * av_get_bytes_per_sample((AVSampleFormat)outFormat);
if(outsize <=0)return XData();
out.Alloc(outsize);
uint8_t *outArr[2] = {0};
outArr[0] = out.data;
int len = swr_convert(actx,outArr,frame->nb_samples,(const uint8_t **)frame->data,frame->nb_samples);
if(len<=0)
{
out.Drop();
return XData();
}
//XLOGE("swr_convert success = %d",len);
return out;
}
#include <string>
#include <android/native_window_jni.h>
#include "FFDemux.h"
#include "XLog.h"
#include "FFDecode.h"
#include "XEGL.h"
#include "XShader.h"
#include "IVideoView.h"
#include "GLVideoView.h"
#include "FFResample.h"
#include "IAudioPlay.h"
#include "SLAudioPlay.h"
class TestObs:public IObserver
{
public:
void Update(XData d)
{
//XLOGI("TestObs Update data size is %d",d.size);
}
};
IVideoView *view = NULL;
extern "C"
JNIEXPORT jstring
JNICALL
Java_com_example_xplay_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
//XLOGI("S begin!");
//XSleep(3000);
//XLOGI("S end!");
//return env->NewStringUTF(hello.c_str());
///////////////////////////////////
///测试用代码
TestObs *tobs = new TestObs();
IDemux *de = new FFDemux();
//de->AddObs(tobs);
de->Open("/sdcard/1080.mp4");
IDecode *vdecode = new FFDecode();
vdecode->Open(de->GetVPara());
IDecode *adecode = new FFDecode();
adecode->Open(de->GetAPara());
de->AddObs(vdecode);
de->AddObs(adecode);
view = new GLVideoView();
vdecode->AddObs(view);
IResample *resample = new FFResample();
XParameter outPara = de->GetAPara();
resample->Open(de->GetAPara(),outPara);
adecode->AddObs(resample);
IAudioPlay *audioPlay = new SLAudioPlay();
audioPlay->StartPlay(outPara);
resample->AddObs(audioPlay);
//vdecode->Open();
de->Start();
vdecode->Start();
adecode->Start();
//XSleep(3000);
//de->Stop();
/*for(;;)
{
XData d = de->Read();
XLOGI("Read data size is %d",d.size);
}*/
return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_xplay_XPlay_InitView(JNIEnv *env, jobject instance, jobject surface) {
// TODO
ANativeWindow *win = ANativeWindow_fromSurface(env,surface);
view->SetRender(win);
//XEGL::Get()->Init(win);
//XShader shader;
//shader.Init();
}
void IAudioPlay::Update(XData data)
{
//压入缓冲队列
}
#define XPLAY_SLAUDIOPLAY_H
#include "IAudioPlay.h"
class SLAudioPlay: public IAudioPlay
{
public:
virtual bool StartPlay(XParameter out);
void PlayCall(void *bufq);
};
#endif //XPLAY_SLAUDIOPLAY_H
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include "XLog.h"
static SLObjectItf engineSL = NULL;
static SLEngineItf eng = NULL;
static SLObjectItf mix = NULL;
static SLObjectItf player = NULL;
static SLPlayItf iplayer = NULL;
static SLAndroidSimpleBufferQueueItf pcmQue = NULL;
static SLEngineItf CreateSL()
{
SLresult re;
SLEngineItf en;
re = slCreateEngine(&engineSL,0,0,0,0,0);
if(re != SL_RESULT_SUCCESS) return NULL;
re = (*engineSL)->Realize(engineSL,SL_BOOLEAN_FALSE);
if(re != SL_RESULT_SUCCESS) return NULL;
re = (*engineSL)->GetInterface(engineSL,SL_IID_ENGINE,&en);
if(re != SL_RESULT_SUCCESS) return NULL;
return en;
}
void SLAudioPlay::PlayCall(void *bufq)
{
if(!bufq)return;
SLAndroidSimpleBufferQueueItf bf = (SLAndroidSimpleBufferQueueItf)bufq;
XLOGE("SLAudioPlay::PlayCall");
}
static void PcmCall(SLAndroidSimpleBufferQueueItf bf,void *contex)
{
SLAudioPlay *ap = (SLAudioPlay *)contex;
if(!ap)
{
XLOGE("PcmCall failed contex is null!");
return;
}
ap->PlayCall((void *)bf);
}
bool SLAudioPlay::StartPlay(XParameter out)
{
//1 创建引擎
eng = CreateSL();
if(eng)
{
XLOGI("CreateSL success! ");
}
else
{
XLOGE("CreateSL failed! ");
return false;
}
//2 创建混音器
SLresult re = 0;
re = (*eng)->CreateOutputMix(eng,&mix,0,0,0);
if(re !=SL_RESULT_SUCCESS )
{
XLOGE("SL_RESULT_SUCCESS failed!");
return false;
}
re = (*mix)->Realize(mix,SL_BOOLEAN_FALSE);
if(re !=SL_RESULT_SUCCESS )
{
XLOGE("(*mix)->Realize failed!");
return false;
}
SLDataLocator_OutputMix outmix = {SL_DATALOCATOR_OUTPUTMIX,mix};
SLDataSink audioSink= {&outmix,0};
//3 配置音频信息
//缓冲队列
SLDataLocator_AndroidSimpleBufferQueue que = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,10};
//音频格式
SLDataFormat_PCM pcm = {
SL_DATAFORMAT_PCM,
(SLuint32) out.channels,// 声道数
(SLuint32) out.sample_rate*1000,
SL_PCMSAMPLEFORMAT_FIXED_16,
SL_PCMSAMPLEFORMAT_FIXED_16,
SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT,
SL_BYTEORDER_LITTLEENDIAN //字节序,小端
};
SLDataSource ds = {&que,&pcm};
//4 创建播放器
const SLInterfaceID ids[] = {SL_IID_BUFFERQUEUE};
const SLboolean req[] = {SL_BOOLEAN_TRUE};
re = (*eng)->CreateAudioPlayer(eng,&player,&ds,&audioSink,sizeof(ids)/sizeof(SLInterfaceID),ids,req);
if(re !=SL_RESULT_SUCCESS )
{
XLOGE("CreateAudioPlayer failed!");
return false;
} else{
XLOGI("CreateAudioPlayer success!");
}
(*player)->Realize(player,SL_BOOLEAN_FALSE);
//获取player接口
re = (*player)->GetInterface(player,SL_IID_PLAY,&iplayer);
if(re !=SL_RESULT_SUCCESS )
{
XLOGE("GetInterface SL_IID_PLAY failed!");
return false;
}
re = (*player)->GetInterface(player,SL_IID_BUFFERQUEUE,&pcmQue);
if(re !=SL_RESULT_SUCCESS )
{
XLOGE("GetInterface SL_IID_BUFFERQUEUE failed!");
return false;
}
//设置回调函数,播放队列空调用
(*pcmQue)->RegisterCallback(pcmQue,PcmCall,this);
//设置为播放状态
(*iplayer)->SetPlayState(iplayer,SL_PLAYSTATE_PLAYING);
//启动队列回调
(*pcmQue)->Enqueue(pcmQue,"",1);
XLOGI("SLAudioPlay::StartPlay success!");
return true;
}
关键字词:IAudioPlay
相关文章
-
无相关信息