Reputation: 910
I'am a newbie in java and android and i want to play sound in my app on different button clicks, for this purpose I made a separate class for playing sound but could not make it work.I want to call this class and play sound when user clicks on the button. Can anyone help how to make a separate class for media player and call it from a click of a button.
code of the class where i declare the media player
public class CorrectSound extends Activity implements OnPreparedListener{
public static void playSound(Context context){
MediaPlayer mp = MediaPlayer.create(context, R.raw.tiktik);
mp.setOnPreparedListener(null);
}
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
}
this is the button which is in separate activity i want to call this class here.
public void optionAClick (View V){
Intent i = new Intent(Login.this,profile.class);
startActivity(i);
}
Upvotes: 1
Views: 583
Reputation: 3364
Make a static function in a class and just call it from any class you want.
Example
public class MyApplication extends Application {
......
......
public static PlaySound(Context context){
MediaPlayer mp = MediaPlayer.create(context, R.raw.notif);
mp.start();
}
}
From some other class
Just use this:
MyApplication.PlaySound(this);
Upvotes: 0
Reputation: 1748
Ok, delete all in the playSound function, and put this:
public static void playSound(Context context){
MediaPlayer mp = MediaPlayer.create(context, R.raw.tiktik);
mp.start();
}
Now, whenever you call your function, it will make a sound.
Upvotes: 3