2011-04-09 173 views
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); 



    } 

回答

1

創建mp = MediaPlayer.create(getBaseContext(), R.raw.tara);每onTouch被調用的時候,所以你不使用你使用之前開始播放MP實例。

檢查mp是否在onTouch的開頭,如果不爲null,則不要創建一個新的,但停止當前。

+0

是的是的!這對我來說很愚蠢。我現在在onCreate部分創建它。謝謝! – Vincent 2011-04-09 16:17:11