2016-07-13 2 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;} 
     } }); 

가 어떻게 함께 세 가지 미디어 플레이어를 중지 할 수 있습니다 : 여기

내 코드?

+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

개인 MediaPlayer mp, mp1, mp2; 및 mp = MediaPlayer.create (Page1Activity.this, R.raw.two); –

+0

@MonaK 그는 mediaplayer 객체를 두 번 선언했습니다. –

관련 문제