2013-08-22 77 views
0

我有這3個類即MainActivity,Options和Play。當用戶點擊選項按鈕時,用戶可以決定他/她是否想要播放音頻。但默認情況下,雖然用戶不會點擊選項上的按鈕,但音頻將播放。停止和播放活動之間的MediaPlayer

我在StackOverflow中發現了一些代碼,但是我不知道如何在用戶決定播放它後決定停止音頻時如何停止音頻。 (要的選項重新)

這裏是到目前爲止的代碼:

MainActivity.class(選項)

public class MainActivity extends Activity { 

    public MediaPlayer mp = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button b = (Button) findViewById(R.id.button1); 
     b.setOnClickListener(new OnClickListener() { 

       @Override 
      public void onClick(View v) { 
       Intent a = new Intent(MainActivity.this, Play.class); 
       startActivity(a); 



      } 

     }); 
    } 
} 

Play.java

public class Play extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.play); 

     Button back = (Button) findViewById (R.id.button1); 

     back.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent menu = new Intent (getApplicationContext(), Menu.class); 
       startActivity(menu); 
      } 

     }); 


    MainActivity z = new MainActivity(); 
    MediaPlayer mp = z.mp; 

    if (mp == null) { 
     mp = MediaPlayer.create(Play.this, R.raw.math_game); 
     mp.start(); 
    } 

    else if (mp.isPlaying()) { 
     mp.stop(); 
     mp = null; 
    } 
    } 

} 

主菜單

public class Menu extends Activity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.menu); 

     Button play, opt; 
     play = (Button) findViewById (R.id.play); 
     opt = (Button) findViewById (R.id.opt); 

     play.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent play = new Intent (Menu.this, Play.class); 
       startActivity(play); 
      } 


     }); 

     opt.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent play = new Intent (Menu.this, MainActivity.class); 
       startActivity(play); 
      } 


     });  
    } 

} 

三江源:))

回答

相關問題