Project Address
Https://github.com/979451341/OpenSLAudio
OpenSL ES is a public API for developing audio based on the NDK, the C language, which enables standardized, high-performance, low-response-time audio capabilities.
This time using OpenSL es to make a music player that can play M4A, MP3 files, and be able to pause and adjust the volume
There are a few steps to playing music
1. Create a sound engine
First create an object interface for the sound engine
result = Slcreateengine (&engineobject, 0, NULL, 0, NULL, NULL);
And then implement it
result = (*engineobject)->realize (Engineobject, Sl_boolean_false);
Grabbing the sound engine from the object of the sound engine
result = (*engineobject)->getinterface (Engineobject, Sl_iid_engine, &engineengine);
Creating the output mixer
result = (*engineengine)->createoutputmix (Engineengine, &outputmixobject, 1, IDs, req);
Achieve output mix tone
result = (*outputmixobject)->realize (Outputmixobject, Sl_boolean_false);
2. Create a sound player
Creating and implementing a player
// realize the playerresult = (*bqPlayerObject)->Realize(bqPlayerObject, SL_BOOLEAN_FALSE);assert(SL_RESULT_SUCCESS == result);(void)result;// get the play interfaceresult = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay);assert(SL_RESULT_SUCCESS == result);(void)result;
3. Set Playback buffer
Data format Configuration
SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, 1, SL_SAMPLINGRATE_8, SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16, SL_SPEAKER_FRONT_CENTER, SL_BYTEORDER_LITTLEENDIAN};
The data locator is the location where the sound data is positioned to play, divided into 4 types: memory location, input/output device location, buffer queue position, and MIDI buffer queue position.
Data Locator Configuration
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
Get the cache queue interface and register
// get the buffer queue interfaceresult = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_BUFFERQUEUE, &bqPlayerBufferQueue);assert(SL_RESULT_SUCCESS == result);(void)result;// register callback on the buffer queueresult = (*bqPlayerBufferQueue)->RegisterCallback(bqPlayerBufferQueue, bqPlayerCallback, NULL);assert(SL_RESULT_SUCCESS == result);(void)result;
4. Get other interfaces to control playback
Get the sound Effects interface
// get the effect send interfaceresult = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_EFFECTSEND, &bqPlayerEffectSend);assert(SL_RESULT_SUCCESS == result);(void)result;
Get the Volume interface
// get the volume interfaceresult = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_VOLUME, &bqPlayerVolume);assert(SL_RESULT_SUCCESS == result);(void)result;// set the player‘s state to playingresult = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING);assert(SL_RESULT_SUCCESS == result);(void)result;
5. Provide playback data
Open music file
// convert Java string to UTF-8const char *utf8 = (*env)->GetStringUTFChars(env, filename, NULL);assert(NULL != utf8);// use asset manager to open asset by filenameAAssetManager* mgr = AAssetManager_fromJava(env, assetManager);assert(NULL != mgr);AAsset* asset = AAssetManager_open(mgr, utf8, AASSET_MODE_UNKNOWN);// release the Java string and UTF-8(*env)->ReleaseStringUTFChars(env, filename, utf8);// the asset might not be foundif (NULL == asset) { return JNI_FALSE;}// open asset as file descriptoroff_t start, length;int fd = AAsset_openFileDescriptor(asset, &start, &length);assert(0 <= fd);AAsset_close(asset);
Set Playback data
SLDataLocator_AndroidFD loc_fd = {SL_DATALOCATOR_ANDROIDFD, fd, start, length};SLDataFormat_MIME format_mime = {SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED};SLDataSource audioSrc = {&loc_fd, &format_mime};
6. Play Music
Playing music only need to change the playback status through the playback interface, pause is also, stop also, but the pause must be before the play cache to do before the line, or the pause is tantamount to stop
result = (*fdPlayerPlay)->SetPlayState(fdPlayerPlay, isPlaying ? SL_PLAYSTATE_PLAYING : SL_PLAYSTATE_PAUSED);
7. Mediation Volume
SLVOLUMEITF Getvolume ()
{
if (fdplayervolume! = NULL)
return fdplayervolume;
Else
return bqplayervolume;
}
void Java_com_ywl5320_openslaudio_mainactivity_setvolumeaudioplayer (jnienv env, Jclass Clazz,
Jint Millibel)
{
Slresult result;
SLVOLUMEITF VOLUMEITF = Getvolume ();
if (NULL! = VOLUMEITF) {
result = (volumeitf)->setvolumelevel (VOLUMEITF, Millibel);
ASSERT (sl_result_success = = RESULT);
(void) result;
}
}
8. Releasing Resources
Freeing up resources when you close apps
void Java_com_ywl5320_openslaudio_mainactivity_shutdown (jnienv* env, Jclass clazz)
{
Destroy buffer queue Audio player object, and invalidate all associated interfacesif (bqplayerobject! = NULL) {(*BQ Playerobject)->destroy (bqplayerobject); Bqplayerobject = NULL; Bqplayerplay = NULL; Bqplayerbufferqueue = NULL; Bqplayereffectsend = NULL; Bqplayermutesolo = NULL; Bqplayervolume = NULL;} Destroy file descriptor audio Player object, and invalidate all associated interfacesif (fdplayerobject! = NULL) {( *fdplayerobject)->destroy (fdplayerobject); Fdplayerobject = NULL; Fdplayerplay = NULL; Fdplayerseek = NULL; Fdplayermutesolo = NULL; Fdplayervolume = NULL;} Destroy output Mix object, and invalidate all associated interfacesif (outputmixobject! = NULL) {(*outputmixobject) ->destroy (Outputmixobject); Outputmixobject = NULL; Outputmixenvironmentalreverb = NULL;} Destroy Engine object, and invalidate all associated interfacesif (engineobject! = NULL) {(*engineobject)->destr Oy (Engineobject); Engineobject = NULL; Engineengine = NULL;}
}
Reference articles
http://blog.csdn.net/u013898698/article/details/72822595
http://blog.csdn.net/ywl5320/article/details/78503768
Android Audio Video Depth 13 opensl ES make music player, can pause and adjust volume (with source download)