0

How can I play sound from a class that DOES NOT extend activity? I've been searching for a while and in every tutorial or answers from stackoverflow I've seen that sounds are always implemented in an activity class.

But in this case I have a class thas has the logic of my game, in which I have a gameUpdate() function; and in that function I want to make a specific sound play if something happens (for example a collision). How can I possibly access the activity that is currently running, from this class? Is there any way to do that?

Thanks.

  • Take a look at SoundPool from this thread: http://stackoverflow.com/questions/9656853/the-correct-way-to-play-short-sounds-android – mammothbane Jun 03 '14 at 21:35
  • How can you comment with 1 reputation? :p Anyway, that does not answer my question. in that example sound is played in the activity class, which I don't want – user3704777 Jun 03 '14 at 21:49

4 Answers4

0

getActivity() or if is inside a fragment getFragment().getActivity()

Or alternativelly you can make add a Context to your class and get the activity reference from the constructor of the class.

Ex:

public class MyClass {
    Context mContext();
    public MyClass(Context context){
        this.context = context;
    }

}

and in your Activity class when you call MyClass:

MyClass myClass = new MyClass(this);

Inside your custom lass you can reference activity methods using its context.

Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58
Marcus Gabilheri
  • 1,259
  • 1
  • 14
  • 26
  • none of these functions getActivity() or getFragment().getActivity() were recognised in my class, nor in the view class the error is "function is undefined for the type *Classname* " – user3704777 Jun 03 '14 at 21:47
  • If you are not inside a fragment or an activity you need a Context. Add a Context to your constructor and pass it from the activity or fragment. You can also pass it through a button as explained in the answer from @Abdul Fair – Marcus Gabilheri Jun 03 '14 at 21:48
0

If you need to get the current Activity instance or context you need to pass it to your other classes so that you can use it. For example:

class ABC extends Activity
{
    public void onCreate(Bundle b)
    {
        XYZ xyz=new XYZ(this); // Sending the current Activity instance
    }
}   

class XYZ
{
    ABC activity;
    public XYZ(ABC activity)
    {
        this.activity = activity; //Now you can use it in this class
    }
}
Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58
  • thanks, i thought about that approach thing is, I have some classes chained (the activity creates a view, the view creates this class i was talking about), so I wanted to avoid "ping-ponging" my activity in all constructors. Thanks anyway :) – user3704777 Jun 03 '14 at 21:39
0

So you actually just need a Context, not specifically an Activity (which is a Context). I would recommend that the class that should play sounds has a constructor which requires a Context. Keep a reference, not directly to the Context that you receive, but to the Application context using getApplicationContext() to get a Context that is safe to retain without the risk of memory leaks.

public class MySoundPlayingClass {
    private final Context mContext;

    public MySoundPlayingClass(Context ctx) {
        // Since ctx could be an Activity, and this class
        // could exist outside of the lifecycle of the Activity,
        // grab the Application context to get a safe reference.
        mContext = ctx.getApplicationContext();
    }
}
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
0

Have a Util class and do something similar to below one. You can pass the context (it can be Activity instance) and the resource id to play it

Usage:

Util.play(context, R.raw.soundFile);

Sample Util class:

public class Util {
   public static void play(final Context context, int resource) {
    MediaPlayer mp = MediaPlayer.create(context, resource);
    if (null != mp) {
        mp.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
        mp.start();
    }
  }
}
fmucar
  • 14,361
  • 2
  • 45
  • 50