Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AudioTrack, SoundPool or MediaPlayer Which Should I use?

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.

like image 442
Fede Avatar asked Nov 23 '12 10:11

Fede


People also ask

What is the use of MediaPlayer class?

MediaPlayer Class in Android is used to play media files. Those are Audio and Video files. It can also be used to play audio or video streams over the network.

What is SoundPool?

A SoundPool is a collection of sound samples that can be loaded into memory from a resource inside the APK or from a file in the file system. The SoundPool library uses the MediaCodec service to decode the audio into raw 16-bit PCM.

How do I play music through MediaPlayer?

Start/Pause the playback: After loading the media file, you can start playing the media file by using the start() method. Similarly, you can pause the playing media file by using the pause() method. Stop the playback: You can stop playback by using the reset() method.

What is the difference between Soundpool and mediaplayer?

Today, we’ve talked about the difference between SoundPool and MediaPlayer. These are some key points you should take away from this article: Use SoundPool for short sound clips, and use MediaPlayer for any long sound clips. Know the difference between reset and release on MediaPlayer. New Lifecycle events were added.

How do I add local sound files to Soundpool?

Simply put, use SoundPool for short sounds, and use MediaPlayer for any long background music. A new menu called “Sound Manager” has been added. Just like the Image Manager, this is where you can add local sound files. Both MediaPlayer and SoundPool search here for available sounds.

What is a Soundpool and how to use it?

Here is a detailed explanation of two components: A SoundPool is a collection of samples that can be loaded into memory from a resource inside the APK or from a file in the file system, which means it is designed for short clips and it is best suited for sound effects.

What is the play block in Soundpool?

Note that the play block is not a command block, or a block that performs an action. By setting the stream variable to the SoundPool’s play block, it both plays the sound and assigns the streamID. If you try to play a long sound file using SoundPool, you might notice that the sound gets cut off.


1 Answers

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.

like image 58
Elvis Popovic Avatar answered Oct 04 '22 14:10

Elvis Popovic