Search code examples
androidandroid-mediaplayerandroid-context

Can not find MediaPlayer.create(Context context, int resid) method from WordAdapter class


Trying to use WordAdapter.this as a Context doesn't get accepted by the compiler in the following code:

playButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                MediaPlayer mp = MediaPlayer.create(WordAdapter.this, currentWord.getAudioResourceId());
                mp.start();
            }
        });

The class is WordAdapter, and it extends ArrayAdapter using a collection of Word objects.


Solution

  • MediaPlayer.create() needs a Context as first parameter. You can pass an context to it. Your adapter class is not child of Context class so you can not pass WordAdapter.this as an context.