2011-08-16 64 views
0

我一直在試圖爲android編程這個音板,但是當我用退格鍵退出程序時,以及在單擊其他聲音時,我仍然困惑並且困惑於如何停止播放聲音。我看遍了所有的地方,甚至在android開發者網站上,我仍然對做什麼感到困惑。如果有人能幫助我,我將不勝感激。我的MediaPlayer代碼即時發佈,但如果需要實際的音板代碼,我很樂意發佈。幫助/與MediaPlayer for Android混淆?

package com.cs.finalfantasysoundboard; 

import android.content.Context; 
import android.media.MediaPlayer; 

public class Music 
{ 
    public interface OnCompletionListener { 

    } 

    private static MediaPlayer mp = null; 

    /** Stop old song and start new one */ 
    public static void play (Context context, int resource) 
    { 
     stop(context); 
     mp = MediaPlayer.create(context, resource); 
     mp.stop(); 
     mp.start(); 
    } 

    /** Stop the music */ 
    public static void stop(Context context) 
    { 
     mp.reset(); 
     mp.release(); 
     mp = null; 
    } 
} 

回答

1

可以實現在播放一些標誌聲音或停止等,這裏是你怎麼可以阻止對返回鍵的聲音:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    if (keyCode == KeyEvent.KEYCODE_BACK /*&& event.getRepeatCount() == 0*/) 
    {   
     if(!isStopped) 
     { 
      stop(); 
     } 
     //System.exit(0); //if you want to exit directly or this.finish(); etc 
     return true; 
    } 

    return super.onKeyDown(keyCode, event); 
}