As the memory consumption in Android is prioritized more, if the Application is using the Mediaplayer API, most of the memory resources are allocated. The developer needs to take care when the Mediaplayer instance is no longer needed or after completion of playing the media resource file. So in this article, it’s been discussed how the media player resources can be released in various scenarios step-by-step so that the memory consumption of the application is stable.
Steps to Release the Audio Resources
Step 1: Create an Empty Activity Android Studio project.
- Create an empty activity Android Studio project.
- Refer to the Android | How to Create/Start a New Project in Android Studio? on how to create an Empty activity Android studio project. Note that we are going to implement this project using the Java language.
Step 2: Preparing the sample Audio file to play.
- In this case, a sample MP3 file is taken in the raw folder.
- To create the “raw” folder right click on the res > New > Android Resource Directory and select the resource type as the raw.
- And after creating the folder add some sample audio file to play.
- Refer to the following video if unable to get the above steps.
Step 3: Working with the activity_main.xml
- In the activity_main.xml file, there is a simple PLAY button is implemented to start playing the audio file when clicked.
- Invoke the following code.
Step 4: Working with the MainActivity.java file
- Initiate the MediaPlayer instance with the raw resource to play the audio file.
- Invoke the following code to handle the play button to start playing the audio file.
- Comments are added inside the code for better understanding.
Output: Run on Emulator
Various scenarios where the Audio resources can be released or cleared
Note: In this case, there are two scenarios that have been shown for demonstration purpose.
There can be more other scenarios where the audio resources can be released.
Scenario 1: After completely playing the audio file
- This is one of the scenarios where the Audio resources can be released.
- In this case, the resources are released, after completely playing the audio file.
- Invoke the following code to implement the on completion listener for the mediaPlayer instance and release the Audio resources.
Scenario 2: When the user presses the back or home button.
- If the application is of the type of news reading or podcast, Whenever the user presses the back button or home button, the application will be in the Stop state.
- So at this condition, the onStop method needs to be overridden and the allocated Audio resources should be released or cleared.
- Invoke the following code to the MainActivity.java file.
The audio should be stopped when the home button is pressed. Like the following:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.