2

Running Android 5.1 on my Nexus 5 I'm experiencing laggy audio due to this problem. The problem is that my 44.1 KHz OGG files are resampled (up to 48 KHz?) on playback using the SoundPool class, causing the lag.

Now, I suppose different devices are optimized for different sample rate. How can I make sure that my audio performs well on all devices? Is it possible to detect the optimal sample rate and then choose the best file from multiple audio resources?

Community
  • 1
  • 1
l33t
  • 18,692
  • 16
  • 103
  • 180

1 Answers1

2

You need AudioManager's PROPERTY_OUTPUT_SAMPLE_RATE

public int getBestSampleRate()
{
    if (Build.VERSION.SDK_INT >= 17) {
        AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        String sampleRateString = am.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
        int sampleRate = sampleRateString == null ? 44100 : Integer.parseInt(sampleRateString);

        return sampleRate;
    } else {
        return 44100;
    }
}
Pavel Alexeev
  • 6,026
  • 4
  • 43
  • 51