0

I have created an Java application that runs in Android. Sounds are prepared and played only with a synchronous MediaPlayer class, involving a latency from 50 to 80 ms, that is too big for a real time product.

So, for improving performance of a sound player in Java for Android (by minimizing its latency), I am looking for an asynchronous audio player or media player.

Asynchronous because that avoids latency when loading (preparing) or playing a sound

Do you know an Android native library or something else that can be imported in a java application?

For instance, I have seen that URL but I don't know how to do for "plugging" it in a Java application?

https://developer.android.com/reference/android/media/AsyncPlayer.html

Thanks

Pascal DUTOIT
  • 121
  • 1
  • 13

1 Answers1

2

I wrote a pretty complere chapter about Android Audio in my book. Here is the flowchart I use to decide which API to use. The old AsyncPlayer you referenced is deprecatated and would not really solve your latency issue in my opinion. Media Player is the worse for start up latency. SoundPool is probably the best choice based on the info you have provided. AudioTrack gives you the most flexibility.

enter image description here

Hope this helps. Here is a code excerpt for playing sounds using the soundPool API:

private void playSoundPool(int soundID) {
    int MAX_STREAMS = 20;
    int REPEAT = 0;
    SoundPool soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, REPEAT);
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int soundId, int status) {
            int priority = 0;
            int repeat = 0;
            float rate = 1.f; // Frequency Rate can be from .5 to 2.0
            // Set volume
            AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            float streamVolumeCurrent =
            mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
            float streamVolumeMax =
            mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            float volume = streamVolumeCurrent / streamVolumeMax;
            // Play it
            soundPool.play(soundId, volume, volume, priority, repeat, rate);
        }
    });
    soundPool.load(this, soundID, 1);
}
Mark JW
  • 476
  • 3
  • 8
  • I have modified my code for using the SoundPool notions. But, now, what is the .jar location of android.media.AudioManager, import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.ParcelFileDescriptor; //import android.util.AndroidRuntimeException; import android.util.Log; ... ? I have to add it inthe Java Build Path. Thanks – Pascal DUTOIT Mar 20 '17 at 18:49
  • No external .jar file is required, You just need to have the import "android.media.SoundPool" inluded with your imports. That should be the only include required for the code I have provided. – Mark JW Mar 21 '17 at 00:35
  • Mark, my Java code for Android (apk) is directly generated with Gluon through JavaFXPorts. I should not see Activity, Context and AudioManager. Your example requires to import SoundPool but also the Context (with soundPool.load(this, soundID, 1)), AudioManager ( with new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, REPEAT);),... So, I don't understand what to do.I don't use Android Studio but "only" Eclipse with JavaFX. An idea for solving my problem? Thanks a lot – Pascal DUTOIT Mar 21 '17 at 03:58
  • Mark,in fact,after compiling Java,I have two important errors: "package android.media does not exist import android.media.AudioManager" and "package android.media does not exist import android.media.SoundPool;" I don't understand these errors because I have added, in my JavaBuild Path, this android-sdks platform: android.jar (android-25). – Pascal DUTOIT Mar 21 '17 at 04:06
  • Help - I don't understand because this package android.media is really included in android-25 . See: https://developer.android.com/reference/android/media/SoundPool.html - And, in Eclipse, I have included this android-sdk AI-25 in the project libraries (Java Build Path). – Pascal DUTOIT Mar 21 '17 at 04:30
  • Mark, I want to follow your advice with the SoundPool use but I have a problem when I compile this solution. Could you help me? See here - Thanks https://stackoverflow.com/questions/42924174/when-i-compile-javafx-application-for-android-with-the-soundpool-use-the-jav – Pascal DUTOIT Mar 22 '17 at 05:17
  • Seems like a problem in the Android project setup for Eclipse+NeoFX. I am not familiar with this. The SoundPool API is available in all Android SDK versions back to the very first, I think. Once you choose a min and target SDK in the Manifest, then all the importing of Android SDK and the SoundPool support needed should be set up automatic. Sorry I can't help specifically on the project setup issues. – Mark JW Mar 22 '17 at 15:28