title: OpenSL ES change audio source without recreating audio player
body: <p>I have a layout that has about 60 buttons and each one, when pressed, plays a different audio file. I have all my audio files as mp3s in my assets folder and to play them I'm basically using the same code as is used in the Google NDK samples "native-audio" project:
  <a href="https://github.com/googlesamples/android-ndk" rel="nofollow">https://github.com/googlesamples/android-ndk</a></p>
  
  <p>I have 10 identical native functions (just with uniquely named variables) that work like this..</p>
  
  <p>function to play sound:</p>
  
  <pre><code>jboolean Java_com_example_nativeaudio_Fretboard_player7play(JNIEnv* env, jclass clazz, jobject assetManager, jstring filename)
  {
      SLresult result;
  
      // convert Java string to UTF-8
      const char *utf8 = (*env)-&gt;GetStringUTFChars(env, filename, NULL);
      assert(NULL != utf8);
      // use asset manager to open asset by filename
      AAssetManager* 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)-&gt;ReleaseStringUTFChars(env, filename, utf8);
      // the asset might not be found
      if (NULL == asset) {
          return JNI_FALSE;
      }
      // open asset as file descriptor
      off_t start, length;
      int fd = AAsset_openFileDescriptor(asset, &amp;start, &amp;length);
      assert(0 &lt;= fd);
      AAsset_close(asset);
  
      // configure audio source
      SLDataLocator_AndroidFD loc_fd = {SL_DATALOCATOR_ANDROIDFD, fd, start, length};
      SLDataFormat_MIME format_mime = {SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED};
      SLDataSource audioSrc = {&amp;loc_fd, &amp;format_mime};
      // configure audio sink
      SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject};
      SLDataSink audioSnk = {&amp;loc_outmix, NULL};
      // create audio player
      const SLInterfaceID ids[3] = {SL_IID_SEEK, SL_IID_MUTESOLO, SL_IID_VOLUME};
      const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
      result = (*engineEngine)-&gt;CreateAudioPlayer(engineEngine, &amp;p7PlayerObject, &amp;audioSrc, &amp;audioSnk,
                                                  3, ids, req);
      assert(SL_RESULT_SUCCESS == result);
      (void)result;
      // realize the player
      result = (*p7PlayerObject)-&gt;Realize(p7PlayerObject, SL_BOOLEAN_FALSE);
      assert(SL_RESULT_SUCCESS == result);
      (void)result;
      // get the play interface
      result = (*p7PlayerObject)-&gt;GetInterface(p7PlayerObject, SL_IID_PLAY, &amp;p7PlayerPlay);
      assert(SL_RESULT_SUCCESS == result);
      (void)result;
  
      if (NULL != p7PlayerPlay) {
          // play
          result = (*p7PlayerPlay)-&gt;SetPlayState(p7PlayerPlay, SL_PLAYSTATE_PLAYING);
          assert(SL_RESULT_SUCCESS == result);
          (void)result;
      }
  
      return JNI_TRUE;
  }
  </code></pre>
  
  <p>function to stop that sound:</p>
  
  <pre><code>void Java_com_example_nativeaudio_Fretboard_player7stop(JNIEnv* env, jclass clazz)
  {
      SLresult result;
  
      // make sure the asset audio player was created
      if (NULL != p7PlayerPlay) {
          // set the player's state
          result = (*p7PlayerPlay)-&gt;SetPlayState(p7PlayerPlay, SL_PLAYSTATE_STOPPED);
          assert(SL_RESULT_SUCCESS == result);
          (void)result;
          // destroy file descriptor audio player object, and invalidate all associated interfaces
          (*p7PlayerObject)-&gt;Destroy(p7PlayerObject);
          p7PlayerObject = NULL;
          p7PlayerPlay = NULL;
      }
  }
  </code></pre>
  
  <p>this is easy to deal with, but I want to minimize latency and avoid having to do <code>(*engineEngine)-&gt;CreateAudioPlayer()</code> every time I want to play a different file. Is there any way to just change the audioSrc used by the audio player without having to destroy and recreate it from scratch every time?</p>
  
  <p>As a bonus, where can I read more about this stuff? Seems pretty difficult to find any information on OpenSL ES anywhere.</p>