注意:我是一個noob,所以不真正知道這些錯誤是什麼意思。SoundPool類型中的方法load(Context,int,int)不適用,方法getSystemService(String)未定義
這是我的類代碼:
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;
}
}
而且我得到兩個錯誤,第一個錯誤是在這裏:
soundPoolMap.put(A1, mSoundPool.load(this, R.raw.a, 1));
和錯誤說The method load(Context, int, int) in the type SoundPool is not applicable for the arguments (MySingleton, int, int)
和第二個錯誤是在這裏:
AudioManager mgr = (AudioManager)MySingleton.getSystemService(Context.AUDIO_SERVICE);
and the error says The method getSystemService(String) is undefined for the type MySingleton
不,只需在背景中加載音樂...然後從每個活動中調用相同的音樂 – Ryan