You will have to forward your Context
to the class in the constructor.
Add a class member for your Context
:
Context mContext;
Then, add a constructor that takes a Context
:
public test (Context c){
mContext = c;
}
Instantiate your class using this constructor:
Test test = new Test(this); //Assuming you call this in an Activity
Lastly, where you want to play your sound in your class, use mContext
as the Context
:
MediaPlayer mp = MediaPlayer.create(mContext, R.raw.button_click);
If you want to instantiate your class in a FrameLayout, use this code:
Test test = new Test(getContext()); //Assuming you call this in a subclass of FrameLayout