0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Tramway11
  • 419
  • 3
  • 9
  • 20

1 Answers1

4

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

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Thanks. The method of a music service is called from an Activity. So based on your answer 2) is better. I don't see where the error is because I run the app on the phone and can't debug here or in emulator. Maybe I can somehow catch the exception from this Service call? – Tramway11 Jul 10 '13 at 12:19
  • 1
    And what's the name of the Context variable in 2)? – Tramway11 Jul 10 '13 at 12:36
  • 1
    Just like an `Activity`...`ServiceName.this`. I don't understand why you can't debug on your phone – codeMagic Jul 10 '13 at 13:20
  • HTC (I've HTC Explorer) has no driver for this officially. There are some suspicious drivers from third parties, I've not installed them yet. I have `code`private PlayerService mp3Service;`\code`, but making mp3Service.this results in red mark error in IDE (cannot be resolved to a type). – Tramway11 Jul 10 '13 at 13:38
  • You should be able to just start the `Service` and let that handle playing the song – codeMagic Jul 10 '13 at 13:50
  • `mp3Service.playSong(getApplicationContext(),url);` works from the `play_button.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {`. But it doesn't work from the Activity onCreate itself... I need song to start onCreate. – Tramway11 Jul 10 '13 at 14:02
  • Did you try using `YourActivityName.this`? Without knowing why its crashing (what the error is) I can't say what the problem is. Make sure that `mp3Service` is initialized before calling the method – codeMagic Jul 10 '13 at 14:05
  • Yes, it crashes too. I check with isMyServiceRunning() method, it returns true. Now I'm trying somehow to get error message – Tramway11 Jul 10 '13 at 14:18
  • Try [this link](http://stackoverflow.com/questions/9544850/htc-explorer-usb-driver-information) to get the drivers – codeMagic Jul 10 '13 at 14:27
  • Using `e.getStackTrace()[0].toString()+" "+e.getStackTrace()[1].toString()` I get this Toast on the screen: `PlayerActivity.onCreate(PlayerActivity.java:143) android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)` HTC Sync I've seen, now downloaded. – Tramway11 Jul 10 '13 at 14:38
  • mp3Service.playSong(PlayerActivity.this,url); – Tramway11 Jul 10 '13 at 14:43
  • AFTER THIS: `android.app.ActivityThread.performLaunchActivity(Activity.Thread.java:1836) android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)android.app.ActivityThread.access$1500(ActivityThread.java:135)android.app.ActivityThread.$H.handleMessage(ActivityThread.java:1054)android.os.Handler.dispatchMessage(Handler.java:99)android.os.Looper.loop(Looper.java:150)` – Tramway11 Jul 10 '13 at 14:48
  • You need to look for `Fatal Exception` and the error it gives after that – codeMagic Jul 10 '13 at 14:55
  • I've installed HTC Sync, now will test on it, if will work, thanks. – Tramway11 Jul 10 '13 at 15:45
  • I moved the call of Service to onStart() method, LogCat gives the next: `07-10 19:50:51.563: E/AndroidRuntime(8024): FATAL EXCEPTION: main 07-10 19:50:51.563: E/AndroidRuntime(8024): android.app.SuperNotCalledException: Activity {myappname/myappname.PlayerActivity} did not call through to super.onStart() ` – Tramway11 Jul 10 '13 at 15:54
  • The most strange is that play launches by clicking button in PlayerActivity, but not while starting PlayerActivity... – Tramway11 Jul 10 '13 at 20:17
  • It seems to me, that call to the player start blocks UI thread, when calling from onCreate || onResume, then app stops. So more likely that this is not from context but UI hang. So I'm trying to launch new thread for player start call, but having this done music stops after 15-50 seconds often, not sure why. – Tramway11 Jul 10 '13 at 21:57