我一直在尝试为我的alphabet android应用程序添加声音,但似乎没有成功。下面是我的代码。
public class Sound extends Activity implements OnCompletionListener {
/** Called when the activity is first created. */
private ImageView b;
private ImageView t;
private ImageView j;
private MediaPlayer mp;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.alphabet);
b=(ImageView)findViewById(R.drawable.b);
t=(ImageView)findViewById(R.drawable.t);
j=(ImageView)findViewById(R.drawable.j);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
b();
}
});
setup();
t.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
t();
}
});
setup2();
j.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
j();
}
});
setup3();
}
public void onCompletion(MediaPlayer mp) {
stop();
}
private void stop() {
mp.stop();
}
private void b() {
mp.stop();
loadClip();
mp.start();
b.setEnabled(true);
}
private void t() {
mp.stop();
loadClip2();
mp.start();
t.setEnabled(true);
}
private void j() {
mp.stop();
loadClip3();
mp.start();
j.setEnabled(true);
}
private void loadClip() {
try {
mp=MediaPlayer.create(this, R.raw.b);
mp.setOnCompletionListener(this);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void loadClip2() {
try {
mp=MediaPlayer.create(this, R.raw.t);
mp.setOnCompletionListener(this);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void loadClip3() {
try {
mp=MediaPlayer.create(this, R.raw.j);
mp.setOnCompletionListener(this);
}
catch (Throwable t) {
goBlooey(t);
}
}
private void setup() {
loadClip();
b.setEnabled(true);
}
private void setup2() {
loadClip2();
t.setEnabled(true);
}
private void setup3() {
loadClip3();
j.setEnabled(true);
}
private void goBlooey(Throwable t) {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder
.setTitle("Exception!")
.setMessage(t.toString())
.setPositiveButton("OK", null)
.show();
}
}请您看看我的代码,并让我知道哪里或什么遗漏了。所有的图像都在绘图中,所有的剪辑都是原始的。致以问候。
发布于 2012-11-25 18:59:59
快速浏览一下你的代码,我会说你的mp不存在,以便在第一次运行时加载另一个剪辑之前停止它。
最上面放的是列兵MediaPlayer mp=null;
然后在它说mp.stop()的每一行上;将它改为if(mp!=null) mp.stop();
https://stackoverflow.com/questions/13550262
复制相似问题