2013-04-07 49 views
-2

我遇到了一些麻煩,這裏是gen。的想法,,我創建了一個簡單的遊戲般的應用程序的應用程序..但問題是,當我嘗試點擊標題屏幕中的按鈕,它不會播放點擊聲音..任何人都可以請幫助我..這裏是我的聲音代碼如何在單擊按鈕時添加soundeffects

public class Effects extends Activity implements MediaPlayer.OnCompletionListener { 

     Button mPlay; 
     MediaPlayer mPlayer; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      mPlay = (Button)findViewById(R.id.btnStart); 
      mPlay.setOnClickListener(playListener); 

      setContentView(R.layout.activity_main); 
     } 

     @Override 
     public void onDestroy() { 
      super.onDestroy(); 
      if(mPlayer != null) { 
       mPlayer.release(); 
      } 
     } 

     private View.OnClickListener playListener = new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if(mPlayer == null) { 
        try { 
         mPlayer = MediaPlayer.create(Effects.this, R.raw.button11); 
         mPlayer.start(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } else { 
        mPlayer.stop(); 
        mPlayer.release(); 
        mPlayer = null; 
       } 
      } 
     }; 
     @Override 
     public void onCompletion(MediaPlayer arg0) { 
      // TODO Auto-generated method stub 

     } 
    }  

,這裏是我的,我想,當我點擊這個按鈕

public class Main extends Activity implements OnClickListener 
{ 
Button about; 
Button Quit; 
Button start; 
protected void onCreate(Bundle onSavedInstanceState) { 
    super.onCreate(onSavedInstanceState); 
    setContentView(R.layout.activity_main); 
    about = (Button)findViewById(R.id.btnAbout); 
    about.setOnClickListener(this); 
    Quit = (Button)findViewById(R.id.btnQuit); 
    Quit.setOnClickListener(this); 
    start = (Button)findViewById(R.id.btnStart); 
    start.setOnClickListener(this); 

} 
public void onClick(View argo) 
{ 
    if(argo.getId()==R.id.btnAbout) 
    { 
     Intent i = new Intent(this,About.class); 
     this.startActivity(i); 
    } 
    if(argo.getId()==R.id.btnQuit) 
    { 
     Intent intent = new Intent(Intent.ACTION_MAIN); 
     intent.addCategory(Intent.CATEGORY_HOME); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(intent); 
    } 
    if(argo.getId()==R.id.btnStart) 
    { 
     Intent start = new Intent(this,Howto.class); 
     this.startActivity(start); 
     Intent svc=new Intent(this,Effects.class); 
     startService(svc); 
    } 
    } 
} 

回答

0

使用SoundPool發揮你的聲音要播放的聲音主要的Java代碼:http://developer.android.com/reference/android/media/SoundPool.html你應該初始化在您的onCreate()中發出聲音,然後在onClick()中播放它們

而不是做數千無意義的if(argo.getId()==R.id.btnQuit)(你應該至少使用else)熟悉switch/case語法)

相關問題