gpt4 book ai didi

java - MediaPlayer - 创建多个实例?

转载 作者:行者123 更新时间:2023-12-01 11:09:50 25 4
gpt4 key购买 nike

我有一些球漂浮在屏幕上。当它们碰撞时,我会使用 MediaPlayer 播放声音。

  public static MediaPlayer create(Resources resources ) {
try {

AssetFileDescriptor afd = resources.openRawResourceFd(R.raw.collide);
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.prepare();
return mp;
} catch (IOException ex) {
Log.d("", "create failed:", ex);
// fall through
} catch (IllegalArgumentException ex) {
Log.d("", "create failed:", ex);
// fall through
} catch (SecurityException ex) {
Log.d("", "create failed:", ex);
// fall through
}
return null;
}




private void moveBalls(){ //
for (int i = 0; i < balls.size(); i++) {
Ball ball1 = balls.get(i);
for (int a = i + 1; a < balls.size(); a++) {
Ball ball2 = balls.get(a);
if(ball1.isColliding(ball2)){
ball1.Collide(ball1,ball2);
catchMP = create(resources);
catchMP.start();
}
}
}
for(Ball ball : balls){
ball.step();
}
}

我已经尝试过 .stop、.release 方法,检查它是否为 null - 一切。

所以。当有两个或四个球时一切都很好,比赛正常。当出现 6-100 个球时,它会停止播放,一段时间后会再次开始(这些声音消失了)。他们不知何故冻僵了。

有什么办法可以解决这个问题吗?

最佳答案

好吧,我再次需要在发帖之前多考虑一下。将 catchMP = create(resources) 移至构造函数。不知道为什么我在这里宣布它。完美运行:)

谢谢!

关于java - MediaPlayer - 创建多个实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32499049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com