I would like to add sound to an animation that I have created. Everytime the animation starts, supposedly a sound has to start as well, but I can't manage to start the sound.
All is ok with the animation, here's the code piece:
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 = Medi开发者_开发百科aPlayer.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();
}
});
}
}
Can anyone point out my mistake ? Thanks in advance for your time.
You should call mp.prepare()
before mp.start()
. Also it's suggested to reset the MediaPlayer
before calling mp.prepare()
.
精彩评论