Tramway11
Tramway11

Reputation: 419

Determine what Context variable to use in MediaPlayer.create in Android

Knotty problem with MP3 stream MediaPlayer in Android. I've done yet some app with it, but now I refactor the code cause of some reasons.

There is a call of a Service in the activity of Player:

mp3Service.playSong(getBaseContext(),url);

The playSong method consists of this:

public void playSong(Context c, String url) {
     if (this.currenturl.equals(""))
     {

         this.mplayer = MediaPlayer.create(c, Uri.parse(url));
         this.currenturl=url;
         this.mplayer.start();
     }
     else
        {
        if (!this.currenturl.equals(url))
        {
                this.mplayer.stop();
               //this.mplayer=null; 
                 this.mplayer = MediaPlayer.create(c, Uri.parse(url));
                 this.mplayer.start();
                 this.currenturl=url;

        } else
        {
            if (this.on==false)
            this.mplayer.start();

        };

        };
     this.on=true;
}

The call to playSong method worked normally when it was on the ImageView click listener! The music started to play.

But, when calling simply from player activity onCreate - it stops the app. Not sure why, but it's totally hard to understand what context parameter to use here. I've read some similar articles and documentation, but there is much fog.

How to determine what should I use for the first Context parameter here? Does it depend on from where I call the .playSong(Context, Uri)? If yes, how? Context is very abstract for new in Android, the class documentation by itself don't shed light to it.

There are many choices which I tried but I need logical reason why to use this and how to determine why app stops.

  1. getApplicationContext()
  2. getBaseContext()
  3. this
  4. PlayerActivity.this

and others. But without understanding it's not right. Maybe the error is in the other place. But without the service call all was working.

Upvotes: 0

Views: 4302

Answers (1)

codeMagic
codeMagic

Reputation: 44571

I agree that Context and when to use which kind can be a hard idea to grasp. From what I have learned, it normally seems best to use your Activity's Context in most situations. These are my thoughts on it and somebody please correct me if I'm wrong with examples/facts.

How to determine what should I use for the first Context parameter here? Does it depend on from where I call the .playSong(Context, Uri)? If yes, how?

Yes and not necessarily. From what I have read, you want to use the Context that is closest to the Object that needs it...use the most minimal Context needed.

  1. If you call if from an Activity and the Object will be destroyed when that Activity is destroyed then use the Activity Context (here Player.this).
  2. If you are calling a service, which has its own Context then use the Service's Context.
  3. If it needs access to things like System services then use getApplicationContext()

You said you've already read articles about using Context and since I don't know which ones I won't post a bunch of links. However,

Here is a good SO answer about using getApplicationContext()

Maybe the error is in the other place.

If you post what error you are getting then maybe we can help better to find the error

Upvotes: 4

Related Questions