1

I've got a Service, which implements MediaPlayer.OnPreparedListener.

Until now I called player.setOnPreparedListener(this) inside a function and it worked well. Now I want to call setOnPreparedListener from Runnable (using a Handler), but I get error :

The method setOnPreparedListener(MediaPlayer.OnPreparedListener) in the type MediaPlayer is not applicable for the arguments (new Runnable(){})

So instead of this I would need to use something that would point to current class. The question is, which class?

elyar abad
  • 771
  • 1
  • 8
  • 27
c0dehunter
  • 6,412
  • 16
  • 77
  • 139

2 Answers2

9

I assume that when you called player.setOnPreparedListener(this), your activity implemented MediaPlayer.OnPreparedListener. And your Runnable isn't (of course). There are two options:

  1. If this runnable is implemented inside the activity, use the fully qualified this: YourActivity.this
  2. If not, you can implement this listener in just the point of the call:

    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
        // do stuff here
        }
    }
    
MByD
  • 135,866
  • 28
  • 264
  • 277
3

Use

player.setOnPreparedListener(MyService.this);

In your Handler, this obviously refers to the Handler. By using MyService.this you force the this keyword a scope higher, referencing the Context of the Activity.

nhaarman
  • 98,571
  • 55
  • 246
  • 278