noober
noober

Reputation: 4938

Android plays sounds after delay

I have to play sounds on GUI events, like clicking buttons etc. For this purpose, I call the following native code from WebView:

MediaPlayer _SoundPlayer = new MediaPlayer();
private void playSound(String sound)
{
    _SoundPlayer.reset();
    try
    {
        AssetFileDescriptor afd = getAssets().openFd("sound/" + sound + ".mp3");
        _SoundPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        _SoundPlayer.prepare();
        _SoundPlayer.start();
    }
    catch (Exception e) { }
}

The problem is there is a delay ~500ms between an event and its sound. Can I optimize playing sound somehow, maybe, creating a dedicated MediaPlayer instances for every kind of sound?

Regards,

Upvotes: 4

Views: 2460

Answers (2)

Tom
Tom

Reputation: 17854

I see that this already has an accepted answer, but I would add that there is no complete solution at this time: Android currently has very large audio latency. Devs are still waiting for a good solution.

This issue refers to the NDK, but the issue is general:

http://code.google.com/p/android/issues/detail?id=3434

Upvotes: 4

Rajesh
Rajesh

Reputation: 15774

Use SoundPool for low-latency media playback, instead of MediaPlayer.

Upvotes: 6

Related Questions