Search code examples
androidrunnableandroid-mediaplayer

Android: MediaPlayer.setOnPreparedListener()


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?


Solution

  • 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.