智能手机安卓版c语言发出声音程序代码?
时间: 2023-09-02 17:14:12 浏览: 59
以下是一个简单的安卓C语言程序,可以发出声音:
```c
#include <jni.h>
#include <android/log.h>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#define BUFFER_SIZE 44100
SLObjectItf engineObject = NULL;
SLEngineItf engineEngine;
SLObjectItf outputMixObject = NULL;
SLEnvironmentalReverbItf outputMixEnvironmentalReverb = NULL;
SLEnvironmentalReverbSettings reverbSettings = SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR;
SLObjectItf playerObject = NULL;
SLPlayItf playerPlay;
SLAndroidSimpleBufferQueueItf playerBufferQueue;
void playAudioBuffer(SLAndroidSimpleBufferQueueItf bufferQueue, void *context) {
static short audioBuffer[BUFFER_SIZE];
for (int i = 0; i < BUFFER_SIZE; i++) {
audioBuffer[i] = 10000 * ((float)rand() / RAND_MAX) - 5000;
}
(*bufferQueue)->Enqueue(bufferQueue, audioBuffer, BUFFER_SIZE*sizeof(short));
}
void Java_com_example_myapp_MainActivity_startAudio(JNIEnv* env, jobject thiz) {
SLresult result;
// create engine
result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
// create output mix
result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 0, NULL, NULL);
result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
result = (*outputMixObject)->GetInterface(outputMixObject, SL_IID_ENVIRONMENTALREVERB, &outputMixEnvironmentalReverb);
result = (*outputMixEnvironmentalReverb)->SetEnvironmentalReverbProperties(outputMixEnvironmentalReverb, &reverbSettings);
// create audio player
SLDataLocator_AndroidSimpleBufferQueue bufferQueueLocator = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2 };
SLDataFormat_PCM pcmFormat = { SL_DATAFORMAT_PCM, 1, SL_SAMPLINGRATE_44_1, SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16, SL_SPEAKER_FRONT_CENTER, SL_BYTEORDER_LITTLEENDIAN };
SLDataSource audioPlayerSource = { &bufferQueueLocator, &pcmFormat };
SLDataLocator_OutputMix audioPlayerLocator = { SL_DATALOCATOR_OUTPUTMIX, outputMixObject };
SLDataSink audioPlayerSink = { &audioPlayerLocator, NULL };
const SLInterfaceID audioPlayerInterfaces[1] = { SL_IID_BUFFERQUEUE };
const SLboolean audioPlayerInterfaceRequired[1] = { SL_BOOLEAN_TRUE };
result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audioPlayerSource, &audioPlayerSink, 1, audioPlayerInterfaces, audioPlayerInterfaceRequired);
result = (*playerObject)->Realize(playerObject, SL_BOOLEAN_FALSE);
result = (*playerObject)->GetInterface(playerObject, SL_IID_PLAY, &playerPlay);
result = (*playerObject)->GetInterface(playerObject, SL_IID_ANDROIDBUFFERQUEUESOURCE, &playerBufferQueue);
result = (*playerBufferQueue)->RegisterCallback(playerBufferQueue, playAudioBuffer, NULL);
result = (*playerPlay)->SetPlayState(playerPlay, SL_PLAYSTATE_PLAYING);
(*playAudioBuffer)(playerBufferQueue, NULL);
}
void Java_com_example_myapp_MainActivity_stopAudio(JNIEnv* env, jobject thiz) {
if (playerObject != NULL) {
(*playerObject)->Destroy(playerObject);
playerObject = NULL;
playerPlay = NULL;
playerBufferQueue = NULL;
}
if (outputMixObject != NULL) {
(*outputMixObject)->Destroy(outputMixObject);
outputMixObject = NULL;
outputMixEnvironmentalReverb = NULL;
}
if (engineObject != NULL) {
(*engineObject)->Destroy(engineObject);
engineObject = NULL;
engineEngine = NULL;
}
}
```
注:此程序使用 OpenSL ES 接口,需要在安卓 NDK 中使用 NDK 模块编译,不支持在标准C语言中编译。