我正在尝试在我的android项目的几个类中播放我的soundpool方法,但是它们不是活动类,所以我需要使用上下文。每当我编写代码并启动应用程序时,它每次都会崩溃。我很好地正确使用上下文。任何帮助将不胜感激。我会在这里发布代码。
public class Sound1{
static SoundPool soundPool;
static int soundId;
static AudioManager audioManager;
static int[] sm;
// private final Context mContext;
Context context;
public Sound1(Context context){
this.context = context;
// mContext = context.getApplicationContext();
int maxStreams = 4;
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
soundPool = new SoundPool.Builder()
.setMaxStreams(maxStreams)
.build();
} else {
soundPool = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0);
}
sm = new int[3];
// fill your sounds
sm[0] = soundPool.load(context, R.raw.sound1, 1);
sm[1] = soundPool.load(context, R.raw.sound1, 1);
sm[2] = soundPool.load(context, R.raw.sound1, 1);
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
}
public void playSound(int sound) {
soundPool.play(sm[sound], 1, 1, 1, 0, 1f);
}
}
Sound1 sound1 = new Sound1(this);
public Player(GameScreen gameScreen, GameEventHandler mGameEventHandler) {
// Store the centre of the screen
//screenCentre.x = gameScreen.getGame().getScreenWidth() / 2;
//screenCentre.y = gameScreen.getGame().getScreenHeight() / 2;
this.mGameEventHandler = mGameEventHandler;
//mDeck = new Deck(mGameScreen);
Move1BoundingBox = new BoundingBox(
mGameScreen.getGame().getScreenWidth()/2, mGameScreen.getGame().getScreenHeight()/2 + 20,
210, 90);
Move2BoundingBox = new BoundingBox(
mGameScreen.getGame().getScreenWidth()/2, mGameScreen.getGame().getScreenHeight()/2 + 110,
210, 90);
ExpandedBoundingBox = new BoundingBox(mGameScreen.getGame().getScreenWidth()/2,
mGameScreen.getGame().getScreenHeight()/2, 210, 150);
}
public void update(ElapsedTime elapsedTime) {
// Consider any touch events occurring since the update;
Input input = mGameScreen.getGame().getInput();
sound1.playSound(1);
if (input.getTouchEvents().size() != 0) {
if (input.existsTouch(0)) {
// Get the primary touch event
isPlayerTouch = true;
STACK TRACE
04-21 14:08:08.609 18358-18358 / uk.ac.qub.eeecs.gage D / ViewRootImpl: ViewPostImeInputStage ACTION_DOWN 04-21 14:08:08.649 18358-18398 / uk.ac.qub.eeecs.gage V / MediaPlayer-JNI:isPlaying:0 04-21 14:08:08.699 18358-18398 / uk.ac.qub.eeecs.gage V / MediaPlayer-JNI: isPlaying:0 04-21 14:08:08.749 18358-18398 / uk.ac.qub.eeecs.gage V / MediaPlayer-JNI:isPlaying:0 04-21 14:08:08.969 18358-18398 / uk.ac.qub.eeecs.gage E / AndroidRuntime:FATAL EXCEPTION: 螺纹2376 过程:uk.ac.qub.eeecs.gage,PID:18358 java.lang.RuntimeException:无法在线程内创建处理程序 没有调用Looper.prepare() 在android.os.Handler。(Handler.java:200) 在android.os.Handler。(Handler.java:114) 在android.app.Activity。(Activity.java:889) 在uk.ac.qub.eeecs.game.cardDemo.Player。(Player.java:0) 在 uk.ac.qub.eeecs.game.cardDemo.CardDemoGameScreen。(CardDemoGameScreen.java:127) 在uk.ac.qub.eeecs.game.MenuScreen.update(MenuScreen.java:103) at uk.ac.qub.eeecs.gage.Game.doUpdate(Game.java:330) at uk.ac.qub.eeecs.gage.Game.access $ 200(Game.java:26) at uk.ac.qub.eeecs.gage.Game $ GameLoop.run(Game.java:527) 在java.lang.Thread.run(Thread.java:818)