what exactly does the "context" mean? For soundPool.load int load(Context context, int resId, int priority)

StackOverflow https://stackoverflow.com/questions/18909912

  •  29-06-2022
  •  | 
  •  

Question

Recently I am trying to use SoundPool play some simple sound effect in my app

but unfortunately it doesn't work and app crash in AVD

what exactly does the "context" mean?

totally clueless

thanks in advance

Was it helpful?

Solution

Any context allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

Normally you can use this.YourClassName as context.

Eg: soundID = soundPool.load(this.MainActivity, R.raw.sound1, 1);

OTHER TIPS

Activity is a subclass of Context, so you can just pass this withing an Activity and it should work.

Intent is your ears regards to Android device. Your activity can talk to Android through Intent and listen on "any change" on your phone.

You have 4 options:

public int load (AssetFileDescriptor afd, int priority) 
public int load (String path, int priority) 
public int load (Context context, int resId, int priority) 
public int load (FileDescriptor fd, long offset, long length, int priority) 

No matter what method you use, need Context to "find out" resources on your Android.

If you going to use AssetFileDescriptor or FileDescriptor you need provide context to initiate them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top