0
我想为我创建的动画添加声音。每次动画开始时,都应该开始播放声音,但我无法开始播放声音。Android,无法让MediaPlayer类正常工作
一切正常与动画,这里的代码块:
public class TestActivity extends Activity {
AnimationDrawable anim;
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
playAnimation(R.id.frameLayout1,R.drawable.anim2,R.raw.bang);
}
public void playAnimation(int FrameLayoutAddress, int animationXMLAdress, int soundAddress)
{
mp = MediaPlayer.create(this.getApplicationContext(), soundAddress);
mp.start(); // error here
FrameLayout imgView = (FrameLayout)findViewById(FrameLayoutAddress);
imgView.setBackgroundResource(animationXMLAdress);
anim = (AnimationDrawable) imgView.getBackground();
imgView.post(new Runnable()
{
@Override
public void run()
{
anim.start();
}
});
}
}
任何人都可以指出我的错误?在此先感谢您的时间。
你得到了什么错误? – Asahi
@Pumpkin:如果'create(...)'失败,则mp将为'null' - 在尝试调用'start()'之前检查以确保它不为空。 R.raw.bang是有效声音文件的有效资源ID吗? – Squonk