0

我试图让 Android 媒体播放器停止在这个 onTouch 方法中播放声音,但它不会。相反,它将声音作为最后一个声音的补充,所以我可以听到它们重叠。如果我让它循环,循环就不会停止。即使我不使用 isPlaying 部分但插入并检查布尔值,它也不起作用。有什么建议吗?

   public boolean onTouch(View v, MotionEvent event){

   mp = MediaPlayer.create(getBaseContext(), R.raw.tara);

   if (event.getAction() == MotionEvent.ACTION_DOWN){
       Drawable myIcon = getResources().getDrawable( R.drawable.image1);
      getWindow().setBackgroundDrawable(myIcon);
      if (mp.isPlaying()){
          mp.stop();
      }
      else {
          mp.start();
      }


   }
   else if (event.getAction() == MotionEvent.ACTION_UP){

       Drawable myIconup = getResources().getDrawable( R.drawable.image2);
          getWindow().setBackgroundDrawable(myIconup);



    }
4

1 回答 1

1

mp = MediaPlayer.create(getBaseContext(), R.raw.tara);每次调用 onTouch 时都会创建,因此您没有使用之前用于开始播放的 mp 实例。

在onTouch开始时检查mp是否为null,如果不为null,则不创建新的,而是停止当前。

于 2011-04-09T16:14:58.620 回答