2016-07-13 57 views
-2

我有3個媒體播放器,我希望當我點擊圖像按鈕時,所有三個媒體播放器都停下來。當我點擊圖像按鈕時,如何停止3個媒體播放器?

這裏是我的代碼:

private MediaPlayer mp,mp1,mp2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.page1); 
    final TextView myText = (TextView) findViewById(R.id.textView1); 
     MediaPlayer mp = MediaPlayer.create(Page1Activity.this, R.raw.two); 
      mp.start(); 
      myText.setText("2"); 
    final TextView myText2 = (TextView) findViewById(R.id.textView2); 
     MediaPlayer mp1 = MediaPlayer.create(Page1Activity.this, R.raw.two); 
      mp1.start(); 
      myText2.setText("2"); 
    final TextView myText3 = (TextView) findViewById(R.id.textView3); 
     MediaPlayer mp2 = MediaPlayer.create(Page1Activity.this, R.raw.four); 
      mp2.start(); 
      myText3.setText("4"); 
    ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1); 
    btn1.setOnClickListener(new OnClickListener() 
    { public void onClick(View v) 
     { Intent i = new Intent(Page1Activity.this, Page2Activity.class);  
      startActivity(i); 

      if (mp != null) { mp.stop(); mp.release(); mp = null;} 
      if (mp1 != null) { mp1.stop(); mp1.release(); mp1 = null;} 
      if (mp2 != null) { mp2.stop(); mp2.release(); mp2 = null;} 
     } }); 

我怎麼能停止所有三個媒體播放器一起?

+0

做同樣的事你正面臨什麼問題?並且一個建議會在'startActivity(i)之前移動所有與停止媒體播放器相關的代碼;' –

+0

那麼,什麼問題? –

+0

@ρяσѕρєяK謝謝。我將它們移動到startActivity之前,但不能正常工作並且不會停止 – MonaK

回答

1
private MediaPlayer mp,mp1,mp2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.page1); 
    final TextView myText = (TextView) findViewById(R.id.textView1); 
     mp = MediaPlayer.create(Page1Activity.this, R.raw.two); 
      mp.start(); 
      myText.setText("2"); 
    final TextView myText2 = (TextView) findViewById(R.id.textView2); 
     mp1 = MediaPlayer.create(Page1Activity.this, R.raw.two); 
      mp1.start(); 
      myText2.setText("2"); 
    final TextView myText3 = (TextView) findViewById(R.id.textView3); 
     mp2 = MediaPlayer.create(Page1Activity.this, R.raw.four); 
      mp2.start(); 
      myText3.setText("4"); 
    ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton1); 
    btn1.setOnClickListener(new OnClickListener() 
    { public void onClick(View v) 
     { Intent i = new Intent(Page1Activity.this, Page2Activity.class);  
      startActivity(i); 

      if (mp != null) { mp.stop(); mp.release(); mp = null;} 
      if (mp1 != null) { mp1.stop(); mp1.release(); mp1 = null;} 
      if (mp2 != null) { mp2.stop(); mp2.release(); mp2 = null;} 
     } }); 

夥計..你宣佈了兩次。請試試這個代碼。

+0

此代碼中更改了什麼Babul? – MonaK

+0

private MediaPlayer mp,mp1,mp2; and mp = MediaPlayer.create(Page1Activity.this,R.raw.two); –

+0

@MonaK他宣佈mediaplayer對象兩次。 –

相關問題