I am using android media player class for playing notification sound in my android Application.
MediaPlayer player = MediaPlayer.create(getApplicationContext(), R.raw.notify);
player.setLooping(false);
player.start();
I need to play different notification sounds in different Activities, so every time i need to play the sound i need to create media player instance and then i need to say start.
But instead of doing this, How can i maintain single instance of the media player throughout the application and use it in all the activities to play the sounds.
Can someone please suggest me the better way of implementing it.
From my point of view i will create one singleton class and i will add all the MediaPlayer related function in this class.
Thanks.
You should consider the Singleton pattern. Make a class MyPlayer that has a static method
getMediaPlayer()that returns the same instance of MediaPlayer each time called.