0

我想为我创建的动画添加声音。每次动画开始时,据说声音也必须开始,但我无法启动声音。

动画一切正常,代码如下:

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();

         }
     });    
}   

}

谁能指出我的错误?在此先感谢您的时间。

4

1 回答 1

1

You should call mp.prepare() before mp.start(). Also it's suggested to reset the MediaPlayer before calling mp.prepare().

于 2011-08-23T21:52:51.737 回答