Fede
Fede

Reputation: 1716

AudioTrack, SoundPool or MediaPlayer, which should I use?

Should I use AudioTrack, SoundPool or MediaPlayer if I need to be able to:

So, which API should I use?

Also, I can't find too much doc on AudioTrack API.
Does anyone know where can I find examples?

Upvotes: 41

Views: 26319

Answers (5)

Elvis Popovic
Elvis Popovic

Reputation: 651

SoundPool is actually an audio mixer.
It can play short clips only, regardless of whether they are encoded as "ogg" or "mp3" or they are uncompressed. SoundPool always store them in memory uncompressed, and note that the limit is 1 MB. If your clip is too big in memory, SoundPool will fall silent and you'll get the following error: "AudioFlinger could not create track. status: -12"

MediaPlayer plays stream and decode it in real time.
It can play much longer clips but it needs processor power for it.

Conclusion:
SoundPool:
= is better fort short audio effects (clicks, explosions, sound loops).
= can play more clips simultaneously, and has volume and speed control. It can also play loops.
MediaPlayer
= is better for background music
= cannot play the same sound clip when it is already being played. It needs the clip to be completely finished before it could be played again.

Note:
You can't play music from SoundPool if the clip isn't fully loaded and decoded. So you must use OnLoadCompleteListener (Android 10 or above) to check for it. If you try to play a sound before it's decoded, SoundPool will be mute. MediaPlayer doesn't suffer from these problems.

Upvotes: 65

Bhargav Thanki
Bhargav Thanki

Reputation: 4954

One more thing to add that all other answers completely missed out. In MedialPlayer you can get media complete event while in SoundPool there is nothing like complete events. You have to apply the logic on your own.

Upvotes: 0

Lama
Lama

Reputation: 2966

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

Upvotes: 3

Kishore Pula
Kishore Pula

Reputation: 332

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/

Upvotes: 19

Harsha
Harsha

Reputation: 1746

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:)

Upvotes: 9

Related Questions