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.
getApplicationContext()
getBaseContext()
this
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.