android10

  • Increase font size
  • Default font size
  • Decrease font size

Playing Video and Audio on Android - Part 2

E-mail Print
( 4 Votes )
Share

Introduction

SDK Version: M3

This is the second part of the article, you can find the first part here.

Playing Video and Audio

In this part, I'm going to focus on MediaPlayer. I'm going to show you, how to stop it properly, and how to create a media player more easily.
The default behavior is the same, with every activity, after onPause and onStop, if the activity is not destroyed, it will continue to run in the background, until it is killed so the system can free up the memory it is using. So until the activity is destroyed, the mediaplayer will continue to occupy space in the phones memory.

1
2
3
4
5
6
7
8
9
10
11
12
private MediaPlayer mp;
 
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mp=new MediaPlayer();
audioPlayer(context,uri);
}
 
public void audioPlayer(Context context,Uri uri){
mp= MediaPlayer.create(context, uri);
mp.start();
}

From the official docs:
"public static MediaPlayer create (Context context, Uri uri)
Convenience method to create a MediaPlayer for a given Uri. On success, prepare() will already have been called and must not be called again.
When done with the MediaPlayer, you should call release(), to free the resources. If not released, too many MediaPlayer instances will result in an exception."

I have yet to get an exception from mediaplayer, but to make sure it won't happen, don't forget to realese(), at least in onDestroy()!

1
2
3
4
5
6
7
@Override
protected void onPause() {
super.onPause();
if(mp.isPlaying()){ //you need to check if it is playing first, or you might get a null pointer exception!
mp.stop();
}
}

Depending on your needs, you can put the mp.stop() in the onPause part, so if the activity goes to the background for whatever reason, it will only pause the playback, and it can continue it in onResume(). Just put mp.play() in onResume().

1
2
3
4
5
@Override
public void onDestroy(){
super.onDestroy();
mp.release();
}


If you don't release the media player in the onDestroy part, it will continue to be in the memory, after the activity is stopped. The OS will handle this issue for you if it runs out of memory, but it's nicer, if You do it, so the OS will run out of memory later (much later in case of huge files).

As an alternative, you could set up an onCompletionListener, so when the media playback is done, it will release the media player.

1
2
3
4
5
6
mp.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});


Comments (6)
  • stalker
    avatar

    I like this article. I've read part 1 also. But I think all the part contains very less information. Can you provide little more information in one part.

    Thanks.

  • penano
    avatar

    You're right stalker!!!..I think the same...I will tell Miriam (the author) to iimprove it...

  • stalker
    avatar

    Alright.

  • suresh.vempower  - video...

    Hi Miriam ....
    Could you please help me with video player from raw folder

  • laxman  - android animations source code

    could u plz help me how can i develop a android animation source code , is there any possibility to download animation .zip file

Only registered users can write comments!
 

ANDROID10 --- TIP!!!

android10 tipYou can edit your position easily: just click on your picture when you're logged in, then on the top you will find a menu, go there, edit your profile, go to your position tab and drag the icon in the map. That's all. Just easy!!!
contact android10!!!