Playing Random Songs From A List Using Mediaplayer In Androidstudio?
So, here is the thing. I was following the New Boston Tutorials for adding background music to an activity. But then i was thinking wouldn't it be nice if i can add a list of songs
Solution 1:
This is because media player is not ready to play your song yet. After setting data source for the Media Player instance, you should call player.prepareAsync();
now when the Media Player is prepared, the onPrepared
method will be executed.Inside this method, start the playback:
@OverridepublicvoidonPrepared(MediaPlayer mp) {
//start playback
mp.start();
}
And after completion of your task you should release all resources like :
player.stop();
player.release();
For avoiding multiple songs play simultaneously with same media player instance I would suggest to create singleton class which returns only one instance at a time. If you are using service for background song playing, I would suggest to follow this tutorial :- http://code.tutsplus.com/tutorials/create-a-music-player-on-android-song-playback--mobile-22778 Hope this will help you !!!
Post a Comment for "Playing Random Songs From A List Using Mediaplayer In Androidstudio?"