2011-12-19 5 views
0

작동하지 않습니다 내가 활동의 메뉴에서 이러한 두 가지 옵션이 있습니다미디어 플레이어 사운드 정지()

옵션 하나를 중지해야 음악 트랙과 두 번째 옵션을 시작하지만, 그것은 아닙니다.

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    MediaPlayer mpSoundTrack = MediaPlayer.create(this, R.raw.app_score); 

    switch (item.getItemId()) { 
     case R.id.icon:  Toast.makeText(this, "Music On!", Toast.LENGTH_LONG).show(); 
     mpSoundTrack.start(); 
          break; 
     case R.id.icontext: Toast.makeText(this, "Music Off!", Toast.LENGTH_LONG).show(); 
     mpSoundTrack.stop(); 
          break; 
    } 
    return true; 
    } 
+0

플레이어가 정지 버튼을 눌렀을 때 무언가를하고 있습니까? –

+0

예, 사용자가 스위치의 첫 번째 옵션을 클릭 할 때 시작되는 음악 파일을 재생하고있었습니다. 아래의 버디가 나에게 그걸 고치는 방법을 보여 줬어. 그래도 고마워. – TroothHertz

답변

3

새 mediaPlayer를 만들 때마다 이전 미디어가 아닌 새로운 미디어 플레이어를 중지 할 때마다 중지합니다. 당신은 그것에 대한 참조를 유지해야합니다 :

private MediaPlayer mpSoundTrack = null; 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.icon:  
      Toast.makeText(this, "Music On!", Toast.LENGTH_LONG).show(); 
      mpSoundTrack = MediaPlayer.create(this, R.raw.app_score); 
      mpSoundTrack.start(); 
      break; 
     case R.id.icontext: 
      Toast.makeText(this, "Music Off!", Toast.LENGTH_LONG).show(); 
      if(mpSoundTrack != null) 
       mpSoundTrack.stop(); 
      break; 
    } 
    return true; 
} 
+0

많은 의무가 있습니다. 나는 활동을 바꾸는 동안 사운드를 계속 연주하려고 애쓰는 것을 방해해야할지 모르겠다. 과제는 2 시간 안에 업로드해야합니다 :). 그러나 진실하게, 당신은 내 감사를 가지고있다. – TroothHertz

+0

미디어 플레이어에 대한 참조를 잃어 버리는 방식으로이 방법으로'onPause()'에서 멈출 수있다. – MByD