- 论坛徽章:
- 1
|
本帖最后由 hanzhenlll 于 2011-12-19 12:29 编辑
目前正准备,在android ndk环境实现音频播放,,,,这是我在网上搜寻到的一段 C的 opensles的代码。。。验证过 可行...-
- #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;
- }
- 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
- }
复制代码 我目前遇到的问题是:
1.该代码采用的是sleep 方式,很二的等待结束....我寻找获取结束标志的方法全都失败了....
不知道用的朋友,有什么好办法 可以获得这个状态... 让他播完就结束.....
该程序的内部我不知道是怎么实现的,播放模块好像是用分离线程... 相关文档我也没找到,E文太烂 愁死我了.....
2.该代码采用输入路径的方式,播放音频...这种方式对于本地文件来说是可行的, 而我的音频文件是 4KB的网络传送来的PCM文件 ,我不知道如何将这个BUFFER传入给播放单元.......
如果各位谁有播放PCM格式的NDK C代码 欢迎贴上来啊,多谢多谢...
我知道论坛里,高手多,在这里谢谢各位了...... |
|