LearningBasics
LearningBasics

Reputation: 680

Android:MediaPlayer when to call release() method

I was reading the document on Media Player and it says 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. So does that mean we shall call release after stop() function or shall we call it in the activity life cycle destroy method. Off course the functionality requires the play button can be pressed after stop button. Can help would be great especially with some piece of code.

Upvotes: 2

Views: 2338

Answers (1)

Yuraj
Yuraj

Reputation: 3195

MediaPlayer release() method, from Android Dev:

Releases resources associated with this MediaPlayer object. It is considered good practice to call this method when you're done using the MediaPlayer. In particular, whenever an Activity of an application is paused (its onPause() method is called), or stopped (its onStop() method is called), this method should be invoked to release the MediaPlayer object, unless the application has a special need to keep the object around. In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a MediaPlayer object is no longer needed may also lead to continuous battery consumption for mobile devices, and playback failure for other applications if no multiple instances of the same codec are supported on a device. Even if multiple instances of the same codec are supported, some performance degradation may be expected when unnecessary multiple instances are used at the same time.

Upvotes: 3

Related Questions