audio - Soundpool or MediaPlayer ? - Android -
i confused of 2 classes.
i have problem have 1000 of .wav files, depends on user load different sounds.
as as, user can play many sounds in row, 4 sounds sequentially.
so should use? soundpool
better wav files not loads , keep files loaded.
any recommendation situation?
i have done on way using mediaplayer
, @blipinsk, after read answer stackoverflow suggested him in comment above.
my files bit larger soundpool can tolerate, as, want play many files sequentially. had implement myself using threads in soundpool. on contrary, ready in mediaplayer using oncompletionlistener. that, used mediaplayer.
actually tried soundpool threads, works since not support large media files, used media player.
i wrote class wrap mediaplayer run playlist, can add playlist , media player run them 1 after another. here class:
import android.media.mediaplayer; import android.os.environment; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.linkedblockingqueue; /** * created mbh on 01.08.2015. */ public class thsafelistmediaplayer { private static final string tag_debug = "mbh"; private string path_of_sound_files; // use because put sound clips in 1 folder // , pass name of folder only. private linkedblockingqueue<string> playlist; private mediaplayer mediaplayer; // media player play sounds, in background private executorservice executorservice; // making sure there 1 thread @ time // adding queue private boolean ispaused = false; private int pausedposition = -1; /** * constructor take care of initializing important variables */ public thsafelistmediaplayer() { // initializing variables executorservice = executors.newsinglethreadexecutor(); playlist = new linkedblockingqueue<>(); mediaplayer = new mediaplayer(); path_of_sound_files = environment.getexternalstoragedirectory().getpath() + "/mbh/sounds/"; } /** * add file playlist * * @param filename: file name */ public void addfile(string filename) { // may add executorservice here safer threads adding here // here use offer, because thread safe playlist.offer(filename); } /** * add file , play last add file , continue play list * * @param filename: file name, playing soundtrack start file */ public void addfileandplay(final string filename) { // multithreaded // executorservice.submit(new runnable() { // @override // public void run() { // playlist.offer(filename); // if (!mediaplayer.isplaying()) // play(playlist.poll()); // } // }); // single threaded playlist.offer(filename); if (!mediaplayer.isplaying()) play(playlist.poll()); } /** * start playing play list if there files in playlist * * @return: true if playing done, otherwise, false; */ public boolean play() { if (mediaplayer != null) { if (!mediaplayer.isplaying()) { if (ispaused) { mediaplayer.seekto(pausedposition); mediaplayer.start(); pausedposition = -1; ispaused = false; return true; } else if (!playlist.isempty()) { play(playlist.poll()); return true; } } } return false; } /** * pause current played track, if there track playing */ public void pause() { if(ispaused) return; if (mediaplayer != null) { if (mediaplayer.isplaying()) { mediaplayer.pause(); pausedposition = mediaplayer.getcurrentposition(); ispaused = true; } } } /** * play given file, when finishes, or fails, play next list * * @param filename: file name start playing */ private void play(string filename) { if (mediaplayer != null) { if (!mediaplayer.isplaying()) { try { mediaplayer.reset(); mediaplayer.setdatasource(filename); mediaplayer.prepare(); mediaplayer.setoncompletionlistener(new mediaplayer.oncompletionlistener() { @override public void oncompletion(mediaplayer mp) { playnextsoundtrack(); } }); mediaplayer.start(); } catch (exception e) { // todo: remove error checking before publishin // if current file not found, play next track if there playnextsoundtrack(); } } } } /** * function called recursively play next track */ private void playnextsoundtrack() { if (playlist.size() != 0) { play(playlist.poll()); } } }
i struggled while it. hope others.
note: used linkedblockingqueue keep playlist tracks in it, because implemented thread safe.
if want use class in threads, suggest use executorservice
if u use in multithreaded app.
Comments
Post a Comment