调用OpenSL ES NDK播放声音
来源:互联网 发布:mac word空白页删不掉 编辑:程序博客网 时间:2024/04/29 03:14
Android NDK 给出了native-audio的例子,这个例子结合java代码,讲解了如何使用OpenSL播放声音。我把此例子进行了精简,完全使用c,可以让我们更好的体会到OpenSL的用法,不多说,上代码
//////////Android.mkLOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := audio-testLOCAL_SRC_FILES := opensles_native.cLOCAL_LDLIBS += -lOpenSLES -llogLD_FLAGS += -sharedinclude $(BUILD_EXECUTABLE)//Appication.mkNDK_TOOLCHAIN_VERSION := 4.8LOCAL_CPP_EXTENSION += .cxxAPP_PLATFORM := android-9
////////////////opensles_native.c#include <stdio.h>#include <SLES/OpenSLES.h>#include <android/log.h>#include <assert.h>enum _bool { false = 0, true};typedef enum _bool bool;/* engine interface */static SLObjectItf engineObject = NULL;static SLEngineItf engineEngine;/* output mix interfaces */static SLObjectItf outputMixObject = NULL;static SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;/* aux effect on the output mix */static const SLEnvironmentalReverbSettings reverbSettings =SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR;/* uri player interfaces */static SLObjectItf uriPlayerObject = NULL;static SLPlayItf uriPlayerPlay;static SLSeekItf uriPlayerSeek;void createEngine(){ SLresult result; // create engine result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL); assert(SL_RESULT_SUCCESS == result); // realize the engine result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE); assert(SL_RESULT_SUCCESS == result); // get the engine interface result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine); assert(SL_RESULT_SUCCESS === result); // create output mix const SLInterfaceID ids[1] = {SL_IID_ENVIRONMENTALREVERB}; const SLboolean req[1] = {SL_BOOLEAN_FALSE}; result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 1, ids, req); assert(SL_RESULT_SUCCESS == result); // realize the output mix result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE); assert(SL_RESULT_SUCCESS == result); #if 0 // get the environmental reverb interface result = (*outputMixObject)->GetInterface(outputMixObject, SL_IID_ENVIRONMENTALREVERB, &outputMixEnvironmentalReverb); if (SL_RESULT_SUCCESS == result) { result = (*outputMixEnvironmentalReverb)->SetEnvironmentalReverbProperties(outputMixEnvironmentalReverb, &reverbSettings); } #endif // ignore unsuccessful result codes for env reverb}bool createUriAudioPlayer(char* uri){ SLresult result; // config audio source SLDataLocator_URI loc_uri = {SL_DATALOCATOR_URI, (SLchar *) uri}; SLDataFormat_MIME format_mime = {SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED}; SLDataSource audioSrc = {&loc_uri, &format_mime}; // config audio sink SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject}; SLDataSink audioSnk = {&loc_outmix, NULL}; // create audio player const SLInterfaceID ids[1] = {SL_IID_SEEK}; const SLboolean req[1] = {SL_BOOLEAN_TRUE}; result = (*engineEngine)->CreateAudioPlayer(engineEngine, &uriPlayerObject, &audioSrc, &audioSnk, 1, ids, req); assert(SL_RESULT_SUCCESS == result); // realize the player result = (*uriPlayerObject)->Realize(uriPlayerObject, SL_BOOLEAN_FALSE); if (SL_RESULT_SUCCESS != result) { (*uriPlayerObject)->Destroy(uriPlayerObject); uriPlayerObject = NULL; return false; } // get the play interface result = (*uriPlayerObject)->GetInterface(uriPlayerObject, SL_IID_PLAY, &uriPlayerPlay); assert(SL_RESULT_SUCCESS == result); // get the seek interface result = (*uriPlayerObject)->GetInterface(uriPlayerObject, SL_IID_SEEK, &uriPlayerSeek); assert(SL_RESULT_SUCCESS == result); // enable whole file looping result = (*uriPlayerSeek)->SetLoop(uriPlayerSeek, SL_BOOLEAN_TRUE, 0, SL_TIME_UNKNOWN); assert(SL_RESULT_SUCCESS == result); return true;}void setPlayingUriAudioPlayer(bool played){ SLresult result; if (uriPlayerPlay != NULL) { result = (*uriPlayerPlay)->SetPlayState(uriPlayerPlay, played ? SL_PLAYSTATE_PLAYING : SL_PLAYSTATE_PAUSED); assert(SL_RESULT_SUCCESS == result); }}int main(int argc, char** argv){ // Create the OpenSL engine createEngine(); // Create the audio player with everything ready. createUriAudioPlayer(argv[1]); printf("Playing..."); setPlayingUriAudioPlayer(true); // play the music sleep(20); printf("Pause..."); setPlayingUriAudioPlayer(false); // pause the player sleep(20); printf("Playing..."); setPlayingUriAudioPlayer(true); sleep(1000); // Just wait for the playing threads}运行代码:拷贝程序audio-test到设备/system/bin下面,然后执行<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; line-height: 19.2000007629395px; font-family: 'Courier New' !important;">./audio-test /sdcard/<span style="box-sizing: border-box; line-height: 1.5 !important;">test.mp3</span>就可以听到播放的声音了
</pre><pre name="code" class="cpp">
0 0
- 调用OpenSL ES NDK播放声音
- 调用OpenSL ES NDK播放声音
- OpenSL 播放声音
- OpenSL 播放声音
- 五、OpenSL ES音频播放
- Android 音频 OpenSL ES URI播放
- Android 音频 OpenSL ES PCM数据播放
- Android+FFmpeg+OpenSL ES音频解码播放
- Android+FFmpeg+OpenSL ES音频解码播放
- OpenSL ES 进行音频解码播放
- Android+FFmpeg+OpenSL ES音频解码播放
- OpenSL ES
- Android的声音编程--使用OpenSL ES Audio
- Android的声音编程--使用OpenSL ES Audio
- Android studio使用ndk native c调用OpenSLES播放声音
- 基于ffmpeg+opengl+opensl es的android视频播放器
- android使用opensl es进行简单的音频播放
- Android通过OpenSL ES播放音频套路详解
- maya的面选择显示模式,中心点或整个面的设置方法
- HDU 2546饭卡 (01背包问题)
- (4.1.30)android 数据传递详解(Serialization、Parcelable、Parcel、Intent、Bundle)
- informatica session中bulk和normal模式
- 在Ubuntu QML应用中使用WebSocket建立TCP/IP连接
- 调用OpenSL ES NDK播放声音
- 阿里巴巴2015秋招算法类机试题目(三阶魔方)
- Android架构简介
- Limit JTextField input to a maximum length
- spring定时任务
- 【无限滚动加载数据】—infinite-scroll插件的使用---------下拉加载数据、无线滚动
- ural 1033. Labyrinth dfs
- Mysql (7)查询 删除重复
- (4.1.30.1)Android中Intent传递对象的两种方法(Bundle.putParcelable/putSerializable)!