2016-07-13 1 views
-2

私は3つのメディアプレーヤーを持っています.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;} 
     } }); 

は、私は一緒にすべての3人のメディアプレーヤーをどのように停止することができますか?

+0

あなたは直面している問題と同じことをしていますか? 1つの提案は、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;} 
     } }); 

Dude ..あなたはその2つの時間を宣言しています。このコードを試してください。

+0

このコードで変更されたものBabul ?? – MonaK

+0

プライベートMediaPlayer mp、mp1、mp2; および mp = MediaPlayer.create(Page1Activity.this、R.raw.two); –

+0

@MonaK彼はmediaplayerオブジェクトを2回宣言しました。 –

関連する問題