调用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
原创粉丝点击