Soundpool vs Media Player in Android | Techbirds

Posted on: May 20, 2014 /

Categories: Android, Tutorials / Author Name: Swapna Singh

There are two methods for playing sounds in android Soundpool and Media Player.

Soundpool :

Soundpool is designed for short clips which can be kept in memory decompressed for quick access, this is best suited for sound effects in apps or games. Using this method with soundboards is a bad idea as you will be loading lots of “medium” sized sounds into the memory and you may exceed your limit (16 Mb) and get an OutOfMemoryException. SoundPool load music files using a separate thread, the operation of the main thread does not block the UI.

How to use :

public SoundPool (int maxStream, int streamType, int srcQuality) 
maxStream – maximum number of simultaneous playback of the stream
StreamType – the type of stream, generally STREAM_MUSIC (specifically listed in the AudioManager class) 
srcQuality – sample rate conversion quality, no effect, use 0 as the default value 

Media Player :

Media Player is designed for longer sound files or streams, this is best suited for music files or larger files. The files will be loaded from disk each time create is called, this will save on memory space but introduce a small delay (not really noticeable).

How to use :

MediaPlayer mp = MediaPlayer.create(ClassName.this, R.raw.sound); mp.start(); mp.pause(); // Stop mp.start();   // Start from place paused mp.stop();    // Stop sound mp.start();   // Start from beginning mp.reset(); mp = MediaPlayer.create(ClassName.this, R.raw.sound2);

mp.release();  // To free memory

2,089 total views, 1 views today

Share this Onfacebook-7406919twitter-2947257linkedin-5966284google-4821909