我想为我创建的动画添加声音。每次动画开始时,据说声音也必须开始,但我无法启动声音。
动画一切正常,这是代码片段:
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();
}
});
}
}
有谁可以指出我的错误?提前感谢您的时间。
答案 0 :(得分:1)
您应该在mp.prepare()
之前致电mp.start()
。此外,建议您在致电MediaPlayer
之前重置mp.prepare()
。