AudioTrack, SoundPool or MediaPlayer Which Should I use?

22,596

Solution 1

Sound pool is actually audio mixer. It can play short clips only regardless of whether they are encoded as ogg or mp3 or they are uncompressed. Sound pool always store them in memory uncompressed, and you must know that limit is 1 MB. If your clip is too big in memory, sound pool will fall silent, and you'll find following error: "AudioFlinger could not create track. status: -12" Media player plays stream and decode it in real time. So it can play much longer clips but needs processor power for it.

So Media player is better for background music, while sound pool is better fort short audio effects (clicks, explosions, sound loops). In addition, sound pool can play more clips simultaneously, and has volume and speed control. Also it can play loops.

One note: you can't play music from sound pool if clip isn't fully loaded and decoded. So you must use OnLoadCompleteListener (Android 10 or above) to check it. If you try to play sound before it is decoded, sound pool will be mute.

Media player doesn't suffer from these problems.

Solution 2

Playing Audio​

If you want to play audio in your Android apps, there are three APIs to choose from​

MediaPlayer - Streams and decodes in real-time for local or remote files. Good for long clips and applications such as background music. More CPU and resource-intensive. Relatively long initialization time. MediaPlayer is a state machine!​

SoundPool - Good for short audio effects or clips. Stored uncompressed in memory, 1MB limit. Clips must be fully loaded before playing. Supports volume and speed control, looping, simultaneous sounds, priorities.​

AudioTrack - Lowest-level audio API on Android. It provides a channel you can configure. Push and pull byte data to the channel. Configure rate, samples, audio format, etc. You can decode audio in unsupported formats.​

In summary, MediaPlayer is the good general-purpose class to play a file, SoundPool is well suited for short audio effects, and AudioTrack lets you get into the low-level audio configurations.​

​ Reference - https://en.proft.me/2018/05/8/how-play-audio-file-android/

Solution 3

In addition to what the other answers suggested, one difference between MediaPlayer and SoundPool is that in MediaPlayer, you cannot play a sound clip when it is already playing (that feature could be helpful especially in games). MediaPlayer needs the clip to finish completely before it could be played again. I think that information is provided in the official website.

However, SoundPool does not have such an issue and also I noticed that SoundPool plays audio just when it is called (I sometimes noticed a slight delay when I used MediaPlayer ).

I think that is (part of) the reason why people say that SoundPool is better for short sounds such as in games. Hope this helps:)

Solution 4

MediaPlayer does not support setting the volume for multible channels.

I think SoundPool is the only thing that suits your needs here.

EDIT: Yup, you need a SoundPool, read this: https://blog.csdn.net/chen_cheng_fly/article/details/7357350

Share:
22,596
Fede
Author by

Fede

Updated on January 18, 2021

Comments

  • Fede
    Fede over 3 years

    I need to play multiple audio files, with different duration, like 5 to 30 seconds. And i want to set volume independently for right/left channel and also to apply effects, like Reverb or Distortion. So, Which API should i use?

    Also, i can't find too much doc on AudioTrack API. Do you know where can i find examples? Thx.

  • Fede
    Fede over 11 years
    Ok, Thank's i will try with soundpool.
  • Shoaib Anwar
    Shoaib Anwar almost 7 years
    MediaPlayer does support setting volume for right/left channels separately. This is a good example here:codota.com/android/methods/android.media.MediaPlayer/se‌​tVolume plus you can refer to the documentation here: developer.android.com/reference/android/media/…