[英]Android Studio Soundboard buttons won't play sound after switching activity i can press them only like 7 times before they stop
In Android studios i made Soundboard in which i have 4 activities when i'm on first activity i can press sounds all day and they play but right after i click next for second activity when i try to play them they won't play sound. 在Android Studio中,我制作了Soundboard,在进行第一次活动时,我会进行4个活动,我可以整天按声音播放,但是当我尝试播放它们时,在单击第二个活动时它们就不会播放声音。 I can click like 7 times and they stop. 我可以点击7次,然后它们停止。
I look everywhere but nothing works i even changed whole code again still same thing happens. 我到处看,但是什么也没用,我什至再次更改了整个代码,仍然发生了同样的事情。
Button button1; 按钮button1; Button theuniverserequired, iusedthestones, etc more sounds.... 按下所需的宇宙,石块等更多声音。
iaminevitable = (Button) findViewById(R.id.iaminevitable);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.iaminevitable);
iaminevitable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mp.start();
}
});
iusedthestones = (Button) findViewById(R.id.iusedthestones);
final MediaPlayer mp2 = MediaPlayer.create(this,
R.raw.iusedthestones);
iusedthestones.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mp2.start();
}
}); etc....
button1 = findViewById(R.id.button_1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent int1 = new Intent(MainActivity.this, Activity2.class);
startActivity(int1);
}
});
there is nothing in error messages for why is this happening 错误消息中没有任何信息说明为什么会发生这种情况
First off: Declare your MPs outside your onCreate to prevent it from garbage collected when activity is paused 首先,请在onCreate之外声明MP,以防止其在活动暂停时被垃圾回收
private MediaPlayer mp,mp2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mp = new MediaPlayer.create(...
mp2 = new MediaPlayer.create(...
Further more: Drop off resources used by MPs when activity stops to avoid memory leaks which may force your app to crash on continuous usage. 更多信息:活动停止时,请释放MP所使用的资源,以避免内存泄漏,这可能会迫使您的应用在连续使用时崩溃。
@Override
protected void onStop() {
super.onStop();
if (mp != null) {
mp.reset();
mp.release();
mp = null;
}
if (mp2 != null) {
mp2.reset();
mp2.release();
mp2 = null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.