按下后退或主页按钮时如何停止声音

时间:2014-03-01 22:35:52

标签: button audio

我的问题是当用户按下后面或主页按钮时如何停止媒体播放器?我真的需要帮助。可以请一些代码告诉我在活动中将其纳入哪里。

这是我的代码:

MY JAVA CODE:

public class fbactivity extends Activity  {

    private MediaPlayer myMediaPlayer;
    private MediaPlayer my1MediaPlayer;

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



  myMediaPlayer = MediaPlayer.create(fbactivity.this, R.raw.milyonlarcataraftaryanyana);
  //Button related to play btn
  Button myButtonOne = (Button) findViewById(R.id.magnumsilah);
  myButtonOne.setOnClickListener(new View.OnClickListener() {



      @Override
      public void onClick(View v) {
          myMediaPlayer.start();
    }
  });

  //Button related to stop btn
  Button myButtonTwo = (Button) findViewById(R.id.Button03);
  myButtonTwo.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
          myMediaPlayer.pause();
      }
  });


 my1MediaPlayer = MediaPlayer.create(fbactivity.this, R.raw.sevmeksenideligibiyurekister);

  //Button related to play btn
  Button myButtonOne1 = (Button) findViewById(R.id.Button02);
  myButtonOne1.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
          my1MediaPlayer.start();
      }
  });

  //Button related to stop btn
  Button myButtonTwo2 = (Button) findViewById(R.id.Button01);
  myButtonTwo2.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
          my1MediaPlayer.pause();
      }
  });


  Button butongeridon=(Button) findViewById(R.id.geridon);
butongeridon.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         Intent i=new Intent(fbactivity.this,MainActivity.class); // the names of activity as per you program.
            startActivity(i);
        finish();
    }
});

}
}

1 个答案:

答案 0 :(得分:0)

你给两个按钮充气,一个来自R.id.magnumsilah,另一个来自R.id.Button03。但是,ClickListener仅绑定到其中一个,并且没有绑定器绑定到后退按钮。

It may well be worth it to be more thorough in you research before posting questions。只是一个想法。