The method load(Context, int, int) in the type SoundPool is not applicable, The method getSystemService(String) is undefined

爷,独闯天下 提交于 2020-01-06 05:35:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!