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

mp.prepare()の前に電話する必要がありますmp.start()MediaPlayerまた、を呼び出す前にをリセットすることをお勧めしmp.prepare()ます。

于 2011-08-23T21:52:51.737 に答える