问题
Note: I'm kind of a noob so dont really know what these errors mean.
This is my class code:
package ryan.test;
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class MySingleton {
private MySingleton instance;
private static SoundPool mSoundPool;
private HashMap<Integer, Integer> soundPoolMap;
public static final int A1 = 1;
private MySingleton() {
mSoundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);// Just an example
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(A1, mSoundPool.load(this, R.raw.a, 1));
// soundPoolMap.put(A5, mSoundPool.load(MyApp.this, R.raw.a, 1));
}
public synchronized MySingleton getInstance() {
if(instance == null) {
instance = new MySingleton();
}
return instance;
}
public void playSound(int sound) {
AudioManager mgr = (AudioManager)MySingleton.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
mSoundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);
}
public SoundPool getSoundPool() {
return mSoundPool;
}
}
And I am getting two errors, the first error is here:
soundPoolMap.put(A1, mSoundPool.load(this, R.raw.a, 1));
and the error says The method load(Context, int, int) in the type SoundPool is not applicable for the arguments (MySingleton, int, int)
and the second error is here:
AudioManager mgr = (AudioManager)MySingleton.getSystemService(Context.AUDIO_SERVICE);
and the error says The method getSystemService(String) is undefined for the type MySingleton
回答1:
You need a Context to use those methods. getSystemService
is a method of the Context instance, myActivity.getSystemService()
. load()
also expects you to pass in the Context instance (myActivity) as the first argument. It's not recommended that you keep a reference to the context outside of the main activity, so you should consider moving this logic back into the activity. Why are you trying to do this in a singleton? Play music in the background? Use a service.
回答2:
You can't access system services in a non-activity class unless you pass in the applicationContext or activity. You need to do this code in your extends Activity
class.
You need to include the context in your constructor in order to access services provided by the sound provider, or pass in the sound provider management object in order to access these objects.
The code should be trivial, just declare a SoundPool object in your class and pass it into the constructor.
回答3:
The method load needs an instance of a Context
(i.e. an Activity
or a Service
). As your singleton does not have that instance, you need to first set an instance of Context
, and only after that, call the load method, using that instance.
Hence, you cannot do this in your constructor.
来源:https://stackoverflow.com/questions/7085584/the-method-loadcontext-int-int-in-the-type-soundpool-is-not-applicable-the