I am a newbie to Android and currently learning the Android SDK.
I am writing a very simple application that will just play various sounds upon interacting with widgets. I am using MediaPlayer to playback the sounds.
My question is should i use singleton pattern for MediaPlayer? I want just one object of MediaPlayer to be shared, but the confusion comes from the following link
https://developer.android.com/reference/android/media/MediaPlayer.html
which says that:
It is also recommended that once a MediaPlayer object is no longer being used, call release() immediately so that resources used by the internal player engine associated with the MediaPlayer object can be released immediately. Resource may include singleton resources such as hardware acceleration components and failure to call release() may cause subsequent instances of MediaPlayer objects to fallback to software implementations or fail altogether.
I don't want to run MediaPlayer as service for now.
So what should i do? Should I use the Singleton Pattern for MediaPlayer?
Great Thanks for any help.
It is not advisable , as you can see from the extract, to keep an object of MediaPlayer as a singleton. Mainly because valuable resources such as hardware could be kept locked as long as you have a media player object in any of the states other than the END state. Also when you call release(), the object cannot be reused. You would need a new instance. I think the android team wants the developers to create objects when needed and release it when done.