作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,我的问题是,当我打开应用程序时,当我尝试单击某些歌曲时,它强制关闭。奇怪的是,它在Galaxy Tab上很好用,但不适用于Galaxy S,droid 2,vibrative等。这是我使用的代码:
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MyMain extends Activity {
/** Called when the activity is first created. */
MediaPlayer mpButtonClick;
MediaPlayer pButtonClick;
MediaPlayer oButtonClick;
MediaPlayer tButtonClick;
MediaPlayer sButtonClick;
MediaPlayer aButtonClick;
MediaPlayer dButtonClick;
MediaPlayer cButtonClick;
MediaPlayer lButtonClick;
MediaPlayer himButtonClick;
MediaPlayer herButtonClick;
MediaPlayer hellButtonClick;
MediaPlayer mocButtonClick;
MediaPlayer rasButtonClick;
MediaPlayer raseButtonClick;
MediaPlayer dalekButtonClick;
MediaPlayer sharButtonClick;
MediaPlayer godButtonClick;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//set up the button sounds
mpButtonClick = MediaPlayer.create(this, R.raw.money);
Button bmoney = (Button) findViewById(R.id.money);
bmoney.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick.start();
}
});
pButtonClick = MediaPlayer.create(this, R.raw.pants);
Button bpants = (Button) findViewById(R.id.pants);
bpants.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pButtonClick.start();
}
});
oButtonClick = MediaPlayer.create(this, R.raw.oath);
Button oath = (Button) findViewById(R.id.oath);
oath.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
oButtonClick.start();
}
});
tButtonClick = MediaPlayer.create(this, R.raw.tardis);
Button tardis = (Button) findViewById(R.id.tardis);
tardis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tButtonClick.start();
}
});
sButtonClick = MediaPlayer.create(this, R.raw.screwdriver);
Button screwdriver = (Button) findViewById(R.id.screwdriver);
screwdriver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sButtonClick.start();
}
});
aButtonClick = MediaPlayer.create(this, R.raw.alarm);
Button alarm = (Button) findViewById(R.id.alarm);
alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
aButtonClick.start();
}
});
dButtonClick = MediaPlayer.create(this, R.raw.kame);
Button kame = (Button) findViewById(R.id.kame);
kame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dButtonClick.start();
}
});
cButtonClick = MediaPlayer.create(this, R.raw.card);
Button card = (Button) findViewById(R.id.card);
card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cButtonClick.start();
}
});
lButtonClick = MediaPlayer.create(this, R.raw.laugh);
Button laugh = (Button) findViewById(R.id.laugh);
laugh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
lButtonClick.start();
}
});
himButtonClick = MediaPlayer.create(this, R.raw.him);
Button him = (Button) findViewById(R.id.him);
him.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
himButtonClick.start();
}
});
herButtonClick = MediaPlayer.create(this, R.raw.her);
Button her = (Button) findViewById(R.id.her);
her.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
herButtonClick.start();
}
});
hellButtonClick = MediaPlayer.create(this, R.raw.hell);
Button hell = (Button) findViewById(R.id.hell);
hell.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
hellButtonClick.start();
}
});
mocButtonClick = MediaPlayer.create(this, R.raw.mocking);
Button mocking = (Button) findViewById(R.id.mocking);
mocking.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mocButtonClick.start();
}
});
godButtonClick = MediaPlayer.create(this, R.raw.god);
Button god = (Button) findViewById(R.id.god);
god.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
godButtonClick.start();
}
});
rasButtonClick = MediaPlayer.create(this, R.raw.rasengan);
Button rasengan = (Button) findViewById(R.id.rasengan);
rasengan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
rasButtonClick.start();
}
});
raseButtonClick = MediaPlayer.create(this, R.raw.rasen);
Button rasen = (Button) findViewById(R.id.rasen);
rasen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
raseButtonClick.start();
}
});
dalekButtonClick = MediaPlayer.create(this, R.raw.dalek);
Button dalek = (Button) findViewById(R.id.dalek);
dalek.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dalekButtonClick.start();
}
});
sharButtonClick = MediaPlayer.create(this, R.raw.sharingan);
Button sharingan = (Button) findViewById(R.id.sharingan);
sharingan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sharButtonClick.start();
}
});
}
}
最佳答案
没有工作的logcat输出或堆栈跟踪,我的猜测是,通过一次将那么多MediaPlayer
实例加载到内存中,您正在使大多数设备大呼过瘾!调用MediaPlayer.create()
不仅会创建另一个实例,还会在其上调用prepare()
并使它准备好滚动。也许尝试更简化的方法:
public class MyMain extends Activity implements MediaPlayer.OnCompletionListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set up the buttons
Button bmoney = (Button) findViewById(R.id.money);
bmoney.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.money);
}
});
Button bpants = (Button) findViewById(R.id.pants);
bpants.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.pants);
}
});
Button oath = (Button) findViewById(R.id.oath);
oath.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.oath);
}
});
Button tardis = (Button) findViewById(R.id.tardis);
tardis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.tardis);
}
});
Button screwdriver = (Button) findViewById(R.id.screwdriver);
screwdriver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.screwdriver);
}
});
Button alarm = (Button) findViewById(R.id.alarm);
alarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.alarm);
}
});
Button kame = (Button) findViewById(R.id.kame);
kame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.kame);
}
});
Button card = (Button) findViewById(R.id.card);
card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.card);
}
});
Button laugh = (Button) findViewById(R.id.laugh);
laugh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.laugh);
}
});
Button him = (Button) findViewById(R.id.him);
him.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.him);
}
});
Button her = (Button) findViewById(R.id.her);
her.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.her);
}
});
Button hell = (Button) findViewById(R.id.hell);
hell.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.hell);
}
});
Button mocking = (Button) findViewById(R.id.mocking);
mocking.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.mocking);
}
});
Button god = (Button) findViewById(R.id.god);
god.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.god);
}
});
Button rasengan = (Button) findViewById(R.id.rasengan);
rasengan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.rasengan);
}
});
Button rasen = (Button) findViewById(R.id.rasen);
rasen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.rasen);
}
});
Button dalek = (Button) findViewById(R.id.dalek);
dalek.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.dalek);
}
});
Button sharingan = (Button) findViewById(R.id.sharingan);
sharingan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playSound(R.raw.sharingan);
}
});
}
//Consolidate media playing to one location
private void playSound(int resId) {
MediaPlayer mp = MediaPlayer.create(this, resId);
mp.setOnCompletionListener(this);
mp.start();
}
//Completion listener method
public void onCompletion(MediaPlayer mp) {
mp.release();
}
}
MediaPlayer
实例,并仅在需要时播放声音。完成后,侦听器可以释放资源以保持合理的内存占用。
playSound()
(当然,除非您的定位目标是1.5)。
关于android - 一些Android的音板上的ForceClose问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4961725/
我遇到了障碍。我遇到了一系列强制关闭。当我在模拟器中运行我的程序时,它会在我放置小部件之前强制关闭安装。 本质上,我试图制作一个带有按钮的小部件。该按钮假定启动服务以将小部件更改为新布局(菜单)。还有
我有一个 SQLite 数据库,它存储所有坐标和描述,以便在点击标记时显示它们。我正在使用此代码来检索所有描述: protected boolean onTap(int index) {
我是一名优秀的程序员,十分优秀!